@nx/js: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.

1npx nx g @nx/js:lib libs/mylib 2

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

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

1npx nx g @nx/js:lib libs/mylib 2

Usage

1nx generate library ... 2
1nx g lib ... #same 2

By default, Nx will search for library in the default collection provisioned in workspace.json.

You can specify the collection explicitly as follows:

1nx g @nx/js:library ... 2

Show what will be generated without writing to disk:

1nx g library ... --dry-run 2

Options

directory

Required
string

A directory where the lib is placed.

bundler

string
Default: tsc
Accepted values: swc, tsc, rollup, vite, esbuild, none

The bundler to use. Choosing 'none' means this library is not buildable.

importPath

string

The library name used to import it, like @myorg/my-awesome-lib. Required for publishable library.

linter

string
Accepted values: none, eslint

The tool to use for running lint checks.

name

string

Library name.

publishable

boolean
Default: false

Configure the library ready for use with nx release (https://nx.dev/core-features/manage-releases).

unitTestRunner

string
Accepted values: none, jest, vitest

Test runner to use for unit tests.

includeBabelRc

boolean

Include a .babelrc configuration to compile TypeScript files

js

boolean
Default: false

Generate JavaScript files rather than TypeScript files.

minimal

boolean
Default: false

Generate a library with a minimal setup. No README.md generated.

strict

boolean
Default: true

Whether to enable tsconfig strict mode or not.

setParserOptionsProject

boolean
Default: false

Whether or not to configure the ESLint parserOptions.project option. We do not do this by default for lint performance reasons.

skipTypeCheck

boolean
Default: false

Whether to skip TypeScript type checking for SWC compiler.

simpleName

boolean
Default: false

Don't include the directory in the generated file name.

tags

string

Add tags to the library (used for linting).

testEnvironment

string
Default: node
Accepted values: jsdom, node

The test environment to use if unitTestRunner is set to jest or vitest.

useProjectJson

boolean

Use a project.json configuration file instead of inlining the Nx configuration in the package.json file.

config

Internal
string
Default: project
Accepted values: workspace, project, npm-scripts

Determines whether the project's executors should be configured in workspace.json, project.json or as npm scripts.

skipFormat

Internal
boolean
Default: false

Skip formatting files.

skipPackageJson

Internal
boolean
Default: false

Do not add dependencies to package.json.

skipTsConfig

Internal
boolean
Default: false

Do not update tsconfig.json for development experience.

buildable

Deprecated
boolean
Default: true

Generate a buildable library.

Use the bundler option for greater control (swc, tsc, rollup, vite, esbuild, none).

compiler

Deprecated
string
Accepted values: tsc, swc

The compiler used by the build and test targets

Use the bundler option for greater control (swc, tsc, rollup, vite, esbuild, none).