Building Node Apps with Nx Dev Tools
Tags: nx, node, javascript
Published: 2020-10-26 18:13:24

---
title: Building Node Apps with Nx Dev Tools
published: true
description: 
tags: nx, node, javascript
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/1e6x98d1t541oy8w0ri0.jpeg
canonical_url: https://blog.nrwl.io/building-node-applications-with-nx-dev-tools-f41035f771ae
---

In this article we will go through different tools and techniques that we can use to build applications and libraries for use with Node. 

---

[Nx](https://nx.dev) and [Node](https://nodejs.org) have always been able to work well together.  We have [schematics for creating Node apps (Express, Nest) and Node libraries (TypeScript)](https://nx.dev/latest/node/plugins/overview). Then we have builders to run those specific apps, or build those libraries for publishing. 

With these tools, we're able to achieve a lot of things that are possible with Node. With the examples below we can discuss some simple use cases.  

## Creating Node Applications (fun with APIs)
If you're starting out with a new project, and you know that you'll be focusing on the API side of things, get started with the following command:
```shell
npx create-nx-workspace --preset=nest
```

This will scaffold out a whole new Nx workspace, with a [Nest](https://nestjs.com/) application already configured and ready to roll!

Alternatively, if you already have an Nx workspace configured for your organization, you can add support for Nest (or even Express!) with the following command:
```shell
yarn add @nrwl/nest
yarn nx generate @nrwl/nest:application my-nest-application
```

> Hint - if a Nx frontend project already exists (either Angular or React), you can also include the `--frontendProject` with the name of the project you wish to target. This flag will automatically set up a proxy configuration for that project, perfect for a microservice use case. 😉 

And start plugging away at creating your Node app! 

Once some APIs have been made you can start your application with:
```shell
yarn nx serve my-nest-application
```

## Building Node Libraries (sharing is caring)
Let's say that rather than build some API, you're working on an open source project that provides date functions. Nx + Node is perfect for this!

Get started by creating your new Nx workspace with the following command:
```shell
npx create-nx-workspace --preset=oss awesome-dates
```

> The `--preset=oss` flag sets up the Nx workspace to have generated applications and libraries be placed into a `packages/` folder. This is similar to how the Nx repo is structured. 😄
> 
> 
> We also named our new project `awesome-dates` 🗓

After your workspace has been generated, we should add the `@nrwl/node` package.
```shell
yarn add @nrwl/node
yarn nx generate @nrwl/node:library add-months --publishable --importPath=@awesome-dates/add-months
yarn nx generate @nrwl/node:library add-days --publishable --importPath=@awesome-dates/add-days
```

> We generated a couple of Node libraries so that when we publish these libraries they're independent, small and scalable.
> 
> We also provided the `--publishable` and `--importPath` flags so that we can use the `nx build` command for these libraries. 

Hammer away at those date functions, add your tests, build and publish! 
```shell
yarn nx run-many --target=test --all
yarn nx run-many --target=build --all
yarn publish ./dist/add-months
yarn publish ./dist/add-days
```
🎉

## Building Nx Plugins (Nx is taking over the world)

You've been playing with Nx for some time, and you always seem to be duplicating some build steps, or some templates, and you think, "These build steps and templates can be shared with everyone who does this!" 

Perfect! Nx can help you with this! 

Nx Plugins are built on top of the Node builder, and we can quick start a Nx Plugin workspace with the following:

```shell
npx create-nx-plugin awesome-nx-plugins
```

> This command functions similarly with `create-nx-workspace`, but the command will scaffold out a folder directory thats more suitable for plugins, which include e2es. 

You can read a full walk through of [creating Nx Plugins on the nx.dev site](https://nx.dev/latest/node/plugins/nx-plugin/overview).

## Summary

With the above examples, you can **[start building your Node focused project easily with Nx](https://nx.dev/latest/node/getting-started/why-nx)**.

These examples are just the beginning. Be on lookout for more in depth guides which will focus on:

* Building, serving, and publishing microservices
* Building and publishing CLIs
* Deploying serverless functions
* and more!