Two Styles of Repo

There are two styles of repos: integrated and package-based. This tutorial shows the integrated style.

You can find more information on the difference between the two in our introduction.

Node Server Tutorial - Part 1: Code Generation

In this tutorial you'll create a backend-focused workspace with Nx.

Contents

Your Objective

For this tutorial, you'll create an Express API application, a library that the API can reference to handle authentication and a suite of e2e tests.

Creating an Nx Workspace

Run the command npx create-nx-workspace@latest and when prompted, provide the following responses:

~

npx create-nx-workspace@latest

1 2 > NX Let's create a new workspace [https://nx.dev/getting-started/intro] 3 4Where would you like to create your workspace? · products-api 5✔ Which stack do you want to use? · node 6✔ What framework should be used? · express 7Standalone project or integrated monorepo? · standalone 8✔ Would you like to generate a Dockerfile? [https://docs.docker.com/] · Yes 9Enable distributed caching to make your CI faster · Yes 10
Opting into Nx Cloud

You will also be prompted whether to add Nx Cloud to your workspace. We won't address this in this tutorial, but you can see the introduction to Nx Cloud for more details.

The node-standalone preset automatically creates a products-api application at the root of the workspace and an e2e project that runs against it.

Framework Options

This tutorial uses the express framework. The node-standalone preset also provides starter files for koa and fastify. For other frameworks, you can choose none and add a it yourself.

Generating Libraries

To create the auth library, use the @nx/node:lib generator:

Nx Generator Syntax

~/products-api

npx nx g @nx/node:lib auth --buildable

1> NX Generating @nx/node:library 2 3CREATE auth/README.md 4CREATE auth/.babelrc 5CREATE auth/package.json 6CREATE auth/src/index.ts 7CREATE auth/src/lib/auth.spec.ts 8CREATE auth/src/lib/auth.ts 9CREATE auth/tsconfig.json 10CREATE auth/tsconfig.lib.json 11UPDATE tsconfig.json 12UPDATE package.json 13CREATE auth/project.json 14CREATE .eslintrc.base.json 15UPDATE .eslintrc.json 16UPDATE e2e/.eslintrc.json 17CREATE auth/.eslintrc.json 18CREATE jest.config.app.ts 19UPDATE jest.config.ts 20UPDATE project.json 21CREATE auth/jest.config.ts 22CREATE auth/tsconfig.spec.json 23
Nx 15 and lower use @nrwl/ instead of @nx/

You have now created three projects:

  • products-api in /
  • e2e in /e2e
  • auth in /auth

What's Next