Cli: [BUG] npm 7 workspace package script execution

Created on 6 Oct 2020  ·  5Comments  ·  Source: npm/cli

Current Behavior:

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).

Expected Behavior:

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:

  1. Execute 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)
  2. cd into a workspace package and directly run a script from there: cd packages/package1 && npm run my-script

Steps To Reproduce:

I have created a workspace playground to experiment with workspace packages and compare yarn and npm7:
https://github.com/fabb/npm7-workspaces

  1. In the repo, run npm install (succeeds)
  2. In the terminal cd applications/app1 && npm run build
  3. The build fails because tsc cannot be found

Now try with yarn:

  1. In the repo, run yarn (succeeds)
  2. In the terminal cd applications/app1 && yarn run build
  3. The build succeeds

Alternatively with yarn:

  1. In the repo, run yarn (succeeds)
  2. In the terminal yarn workspace @fabb/app1 build
  3. The build succeeds

Environment:

  • OS: macOS Catalina 10.15.7
  • Node: v12.18.0
  • npm: 7.0.0-rc.2

Related

RFC for workspaces: https://github.com/npm/rfcs/blob/latest/accepted/0026-workspaces.md

Bug Enhancement Release 7.x

Most helpful comment

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

All 5 comments

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

$ yarn workspace package-a add react

Lerna

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ahuglajbclajep picture ahuglajbclajep  ·  3Comments

Cohen-Carlisle picture Cohen-Carlisle  ·  4Comments

CliffS picture CliffS  ·  3Comments

1000i100 picture 1000i100  ·  3Comments

admosity picture admosity  ·  4Comments