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 and Node have always been able to work well together. We have schematics for creating Node apps (Express, Nest) and Node libraries (TypeScript). 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:
npx create-nx-workspace --preset=nest
This will scaffold out a whole new Nx workspace, with a Nest 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:
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:
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:
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 apackages/
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.
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 thenx build
command for these libraries.
Hammer away at those date functions, add your tests, build and publish!
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:
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.
Summary
With the above examples, you can start building your Node focused project easily with 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!
Top comments (0)