@nrwl/js

The JS plugin contains executors and generators that are useful for JavaScript/TypeScript projects in an Nx workspace.

Setting Up JS

Installation

In any Nx workspace, you can install @nrwl/js by running the following commands if @nrwl/js package is not installed:

1npm i --save-dev @nrwl/js
2
1yarn add --dev @nrwl/js
2

ts Preset

When initializing a new Nx workspace, specifying --preset=ts will generate a workspace with @nrwl/js pre-installed.

1npx create-nx-workspace my-org --preset=ts
2
1yarn create nx-workspace my-org --preset=ts
2

Create Libraries

You can add a new JS/TS library with the following command:

1nx g @nrwl/js:lib my-lib
2

Build

You can build libraries that are generated with --buildable flag.

1nx g @nrwl/js:lib my-buildable-lib --buildable
2

Generating a library with --buildable will add a build target to the library's project.json file allows the library to be built.

1nx build my-buildable-lib
2

Test

You can test a library with the following command:

1nx test my-lib
2

Lint

You can lint a library with the following command:

1nx lint my-lib
2

Compiler

By default, @nrwl/js uses TypeScript Compiler (TSC), via @nrwl/js:tsc executor, to compile your libraries. Optionally, you can switch tsc out for a different compiler with --compiler flag when executing the generators.

Currently, @nrwl/js supports the following compilers:

SWC

  • Create a buildable library with swc
1nx g @nrwl/js:lib my-swc-lib --compiler=swc --buildable
2
  • Convert a tsc library to use swc
1nx g @nrwl/js:convert-to-swc my-buildable-lib
2

Now the build command will use @nrwl/js:swc executor to compile your libraries.

The first time you generate a swc library or convert a tsc library over to swc, @nrwl/js will install the necessary dependencies to use swc.

Package reference

Here is a list of all the executors and generators available from this package.

Executors

  • tsc

    Build a project using TypeScript.

  • swc

    Build a project using SWC.

Generators