Skip to content

NPM

Workspace (i.e. nested package)

Ref: https://docs.npmjs.com/cli/v8/using-npm/workspaces

You can have a file structure like this:

.
+-- package.json
`-- packages
   +-- a
   |   `-- package.json
   `-- b
       `-- package.json

So when you run npm i in root directory, it install packages like this:

+-- node_modules
|  `-- packages/a -> ../packages/a
+-- package-lock.json
+-- package.json
`-- packages
   +-- a
   |   `-- package.json
# Init a workspace (NOTE: assume package.json already created)
npm init -w ./packages/a

# Install all packages defined in package.json of each workspaces
npm i

# Other command specific to a workspace, e.g. install, uninstall...etc
npm install abbrev -w a

# Run a command defined in workspace a
npm run test --workspace=a

# Run the commands defined in ALL workspaces
npm run test --workspaces

Comments