The JS plugin contains executors and generators that are useful for JavaScript/TypeScript projects in an Nx workspace.
Setting Up @nx/js
Installation
Make sure to install the @nx/js
version that matches the version of nx
in your repository. If the version numbers get out of sync, you can encounter some difficult to debug errors. You can fix Nx version mismatches with this recipe.
In any Nx workspace, you can install @nx/js
by running the following command:
โฏ
nx add @nx/js
This will install the correct version of @nx/js
.
ts
Preset
When initializing a new Nx workspace, specifying --preset=ts
will generate a workspace with @nx/js
pre-installed.
โฏ
npx create-nx-workspace my-org --preset=ts
Nx 20 updates the TS monorepo setup when using --preset=ts
. The workspace is set up with TypeScript Project References along with the Workspaces feature from npm, yarn, pnpm, and bun.
To create with the older setup for TS monorepo with compilerOptions.paths
, use create-nx-workspace --preset=apps
.
How @nx/js Infers Tasks
The @nx/js/typescript
plugin will add a typecheck
task to projects that have a tsconfig.json
.
This plugin adds a build
task for projects that:
- Have a runtime tsconfig file (defaults to
tsconfig.lib.json
). - Have a
package.json
file containing entry points that are not source files.
For example, this project is buildable and will have a build
task.
1{
2 "name": "@acme/pkg1",
3 "exports": {
4 "./package.json": "./package.json",
5 ".": {
6 "types": "./dist/index.d.ts",
7 "default": "./dist/index.js"
8 }
9 }
10}
11
Whereas this project points to source files and will not have a build
task.
1{
2 "name": "@acme/pkg1",
3 "exports": {
4 "./package.json": "./package.json",
5 ".": "./src/index.ts"
6 }
7}
8
View Inferred Tasks
To view inferred tasks for a project, open the project details view in Nx Console or run nx show project my-project
in the command line.
@nx/js Configuration
The @nx/js/typescript
plugin is configured in the plugins
array in nx.json
.
1{
2 "plugins": [
3 {
4 "plugin": "@nx/js/typescript",
5 "options": {
6 "typecheck": {
7 "targetName": "typecheck"
8 },
9 "build": {
10 "targetName": "build",
11 "configName": "tsconfig.lib.json"
12 }
13 }
14 }
15 ]
16}
17
You can also set typecheck
and build
options to false
to not infer the corresponding tasks.
1{
2 "plugins": [
3 {
4 "plugin": "@nx/js/typescript",
5 "options": {
6 "build": false
7 }
8 }
9 ]
10}
11
Disable Typechecking
To disable typecheck
task for a specific project, set the nx.addTypecheckTarget
property to false
in tsconfig.json
.
1{
2 "extends": "../../tsconfig.base.json",
3 "files": [],
4 "include": [],
5 "references": [
6 {
7 "path": "./tsconfig.lib.json"
8 }
9 ],
10 "nx": {
11 "addTypecheckTarget": false
12 }
13}
14
Create Libraries
You can add a new JS/TS library with the following command:
โฏ
nx g @nx/js:lib libs/my-lib
Build
You can build
libraries that are generated with a bundler specified.
โฏ
nx g @nx/js:lib libs/my-buildable-lib --bundler=rollup
Generating a library with --bundler
specified will add a build
target to the library's project.json
file allows the library to be built.
โฏ
nx build my-buildable-lib
Test
You can test a library with the following command:
โฏ
nx test my-lib
Lint
You can lint a library with the following command:
โฏ
nx lint my-lib
Compiler
By default, @nx/js
uses TypeScript Compiler (TSC), via @nx/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, @nx/js
supports the following compilers:
SWC
- Create a buildable library with
swc
โฏ
nx g @nx/js:lib libs/my-swc-lib --bundler=swc
- Convert a
tsc
library to useswc
โฏ
nx g @nx/js:convert-to-swc my-buildable-lib
Now the build
command will use @nx/js:swc
executor to compile your libraries.
The first time you generate a
swc
library or convert atsc
library over toswc
,@nx/js
will install the necessary dependencies to useswc
.