Skip to content

The @nx/js plugin provides various generators to help you create and configure js projects within your Nx workspace. Below is a complete reference for all available generators and their options.

convert-to-swc

Convert a TSC library to SWC.

Usage:

Terminal window
nx generate @nx/js:convert-to-swc [options]

Aliases: swc

Arguments:

Terminal window
nx generate @nx/js:convert-to-swc <project> [options]

Options

OptionTypeDescriptionDefault
--targetsarrayList of targets to convert.["build"]

library

Create a TypeScript Library.

The @nx/js:lib generator will generate a library for you, and it will configure it according to the options you provide.

Terminal window
npx nx g @nx/js:lib libs/mylib

By default, the library that is generated when you use this executor without passing any options, like the example above, will be a buildable library, using the @nx/js:tsc executor as a builder.

You may configure the tools you want to use to build your library, or bundle it too, by passing the --bundler flag. The --bundler flag controls the compiler and/or the bundler that will be used to build your library. If you choose tsc or swc, the result will be a buildable library using either tsc or swc as the compiler. If you choose rollup or vite, the result will be a buildable library using rollup or vite as the bundler. In the case of rollup, it will default to the tsc compiler. If you choose esbuild, you may use the esbuildOptions property in your project.json under the build target options to specify whether you wish to bundle your library or not.

Examples

Buildable with default compiler (tsc)

Generate a buildable library using the @nx/js:tsc executor. This uses tsc as the compiler.

Terminal window
npx nx g @nx/js:lib libs/mylib
Buildable with SWC compiler

Generate a buildable library using SWC as the compiler. This will use the @nx/js:swc executor.

Terminal window
npx nx g @nx/js:lib libs/mylib --bundler=swc
Buildable with tsc

Generate a buildable library using tsc as the compiler. This will use the @nx/js:tsc executor.

Terminal window
npx nx g @nx/js:lib libs/mylib --bundler=tsc
Buildable, with Rollup as a bundler

Generate a buildable library using Rollup as the bundler. This will use the @nx/rollup:rollup executor. It will also use SWC as the compiler.

Terminal window
npx nx g @nx/js:lib libs/mylib --bundler=rollup

If you do not want to use swc as the compiler, and want to use the default babel compiler, you can do so in your project.json under the build target options, using the compiler property:

libs/mylib/project.json
"build": {
"executor": "@nx/rollup:rollup",
"options": {
//...
"compiler": "babel"
}
}
Buildable, with Vite as a bundler

Generate a buildable library using Vite as the bundler. This will use the @nx/vite:build executor.

Terminal window
npx nx g @nx/js:lib libs/mylib --bundler=vite
Using ESBuild

Generate a buildable library using ESBuild as the bundler. This will use the @nx/esbuild:esbuild executor.

Terminal window
npx nx g @nx/js:lib libs/mylib --bundler=esbuild

If you want to specify whether you want to bundle your library or not, you can do so in your project.json under the build target options, using the esbuildOptions property:

libs/mylib/project.json
"build": {
"executor": "@nx/esbuild:esbuild",
"options": {
//...
"esbuildOptions": {
"bundle": true
}
}
}
Minimal publishing target

Generate a publishable library with a minimal publishing target. The result will be a buildable library using the @nx/js:tsc executor, using tsc as the compiler. You can change the compiler or the bundler by passing the --bundler flag.

Terminal window
npx nx g lib libs/mylib --publishable
In a nested directory

Generate a library named mylib and put it under a directory named nested (libs/nested/mylib).

Terminal window
npx nx g lib libs/nested/mylib
Non-buildable library

Generate a non-buildable library.

Terminal window
npx nx g @nx/js:lib libs/mylib --bundler=none

Usage:

Terminal window
nx generate @nx/js:library [options]

Aliases: lib

Arguments:

Terminal window
nx generate @nx/js:library <directory> [options]

Options

OptionTypeDescriptionDefault
--buildablebooleanGenerate a buildable library.true
--bundlerstringThe bundler to use. Choosing ‘none’ means this library is not buildable."tsc"
--compilerstringThe compiler used by the build and test targets
--configstringDetermines whether the project’s executors should be configured in workspace.json, project.json or as npm scripts."project"
--importPathstringThe library name used to import it, like @myorg/my-awesome-lib. Required for publishable library.
--includeBabelRcbooleanInclude a .babelrc configuration to compile TypeScript files
--jsbooleanGenerate JavaScript files rather than TypeScript files.false
--linterstringThe tool to use for running lint checks.
--minimalbooleanGenerate a library with a minimal setup. No README.md generated.false
--namestringLibrary name.
--publishablebooleanConfigure the library ready for use with nx release (https://nx.dev/core-features/manage-releases).false
--setParserOptionsProjectbooleanWhether or not to configure the ESLint parserOptions.project option. We do not do this by default for lint performance reasons.false
--skipFormatbooleanSkip formatting files.false
--skipPackageJsonbooleanDo not add dependencies to package.json.false
--skipTsConfigbooleanDo not update tsconfig.json for development experience.false
--skipTypeCheckbooleanWhether to skip TypeScript type checking for SWC compiler.false
--strictbooleanWhether to enable tsconfig strict mode or not.true
--tagsstringAdd tags to the library (used for linting).
--testEnvironmentstringThe test environment to use if unitTestRunner is set to jest or vitest."node"
--unitTestRunnerstringTest runner to use for unit tests.
--useProjectJsonbooleanUse a project.json configuration file instead of inlining the Nx configuration in the package.json file.

setup-build

Sets up build target for a project.

Usage:

Terminal window
nx generate @nx/js:setup-build [options]

Arguments:

Terminal window
nx generate @nx/js:setup-build <project> [options]

Options

OptionTypeDescriptionDefault
--bundlerstring [required]The bundler to use to build the project."tsc"
--buildTargetstringThe build target to add."build"
--mainstringThe path to the main entry file, relative to workspace root. Defaults to /src/index.ts or /src/main.ts.
--tsConfigstringThe path to the tsConfig file, relative to workspace root. Defaults to /tsconfig.lib.json or /tsconfig.app.json depending on project type.

setup-prettier

Setup Prettier as the formatting tool.

Usage:

Terminal window
nx generate @nx/js:setup-prettier [options]

Options

OptionTypeDescriptionDefault
--skipFormatbooleanSkip formatting files.false
--skipPackageJsonbooleanDo not add dependencies to package.json.false

setup-verdaccio

Setup Verdaccio local-registry.

Usage:

Terminal window
nx generate @nx/js:setup-verdaccio [options]

Options

OptionTypeDescriptionDefault
--skipFormatbooleanSkip formatting files.false

Getting Help

You can get help for any generator by adding the --help flag:

Terminal window
nx generate @nx/js:<generator> --help