Given a package with several workspaces, npm install installs all packages of the root and of the workspace packages to the root node_modules. Unlike yarn, no dependencies of the workspace packages are installed into the respective workspace package node_modules. That means, it is not possible to directly execute scripts when the current working directory is a subpackage.
Also there does not yet seem to exist a npm workspace command to execute scripts from workspace packages (compare to yarn workspace).
This blog post mentioned that the npm workspaces feature should work similar to the one of yarn:
npm v7 will have at least the Workspace feature support of Yarn, and will set the stage for more advanced workspace features in v8.
https://blog.npmjs.org/post/186983646370/npm-cli-roadmap-summer-2019
I would expect to be able to either:
npm workspace @me/workspace-package1 my-script to run the my-script script in the workspace package @me/workspace-package1 (e.g. located in packages/package1)cd packages/package1 && npm run my-scriptI have created a workspace playground to experiment with workspace packages and compare yarn and npm7:
https://github.com/fabb/npm7-workspaces
npm install (succeeds)cd applications/app1 && npm run buildtsc cannot be foundNow try with yarn:
yarn (succeeds)cd applications/app1 && yarn run buildAlternatively with yarn:
yarn (succeeds)yarn workspace @fabb/app1 buildRFC for workspaces: https://github.com/npm/rfcs/blob/latest/accepted/0026-workspaces.md
Link to the workspaces RFC is https://github.com/npm/rfcs/blob/latest/implemented/0026-workspaces.md
I had a question related to the npm7 workspaces feature. If I had to install a package only to a specific workspace, what is the npm command to do that. Yarn and Lerna both support this.
Assume the following workspaces.
{
"workspaces": ["packages/*"]
}
root
+ -- packages
+ -- package-a
+ -- package-b
$ yarn workspace package-a add react
lerna add supports lerna's filter options
$ lerna add react --scope=package-a
A question. Does npm workspaces, like yarn, also require the root package to have "private": true? The documentation should include information around this.
While not ideal; until this issue is resolved, a workaround is to use the --prefix option (see docs).
For example, using @fabb's steps to reproduce with the repo npm7-workspaces and replacing step 2's command with the following:
npm run --prefix applications/app1 build
The build succeeds:
npm7-workspaces % npm -v
7.0.2
npm7-workspaces % npm run --prefix applications/app1 build
> @fabb/[email protected] build
> tsc index.ts
npm7-workspaces % echo $?
0
A question. Does npm workspaces, like yarn, also require the root package to have
"private": true? The documentation should include information around this.
@detj no, as far as I've tested it does not.
Most helpful comment
While not ideal; until this issue is resolved, a workaround is to use the
--prefixoption (see docs).For example, using @fabb's steps to reproduce with the repo npm7-workspaces and replacing step 2's command with the following:
The build succeeds: