Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] npm 7 workspace package script execution #1904

Closed
fabb opened this issue Oct 6, 2020 · 8 comments
Closed

[BUG] npm 7 workspace package script execution #1904

fabb opened this issue Oct 6, 2020 · 8 comments
Labels
Enhancement new feature or improvement Release 7.x work is associated with a specific npm 7 release

Comments

@fabb
Copy link

fabb commented Oct 6, 2020

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

@fabb fabb added Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release labels Oct 6, 2020
@darcyclarke darcyclarke modified the milestone: OSS - Sprint 17 Oct 6, 2020
@darcyclarke darcyclarke added Enhancement new feature or improvement and removed Needs Triage needs review for next steps Bug thing that needs fixing labels Oct 6, 2020
@detj
Copy link

detj commented Oct 10, 2020

@detj
Copy link

detj commented Oct 10, 2020

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

@detj
Copy link

detj commented Oct 10, 2020

A question. Does npm workspaces, like yarn, also require the root package to have "private": true? The documentation should include information around this.

@virtuoushub
Copy link

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/app1@1.0.0 build
> tsc index.ts

npm7-workspaces % echo $?

0

@Ymirke
Copy link

Ymirke commented Oct 25, 2020

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.

@darcyclarke darcyclarke added the Bug thing that needs fixing label Oct 30, 2020
@uptonking
Copy link

I've tested the repo fabb/npm7-workspaces.
cd applications/app1 && npm run build is building successfully.

@shrinktofit
Copy link

I think running unit tests within individual workspace(cd into it) is not possible if workspaces do not have their own node_modules(even with symbol links). 🙁

yukisato added a commit to yukisato/express-react that referenced this issue Dec 30, 2020
@darcyclarke darcyclarke removed the Bug thing that needs fixing label Feb 2, 2021
@ruyadorno
Copy link
Collaborator

I bring good news!

Workspaces support for both npm run and npm exec landed today in npm@7.7.0, it adds syntax for running scripts in workspaces from the top-level folder, e.g:

assuming a file structure:

.
├── package.json ->  { "workspaces": ["packages/*"] }
└── packages
    ├── a
    │   ├── index.js
    │   └── package.json
    ├── b
    │   ├── index.js
    │   └── package.json
    └── c
        ├── index.js
        └── package.json

It's now possible to run a script in a given workspace. In order to run one of the scripts available in the workspace a at the location ./packages/a/ you may run from the root, either of the following:

  • npm run <script-name> -w a
  • npm run <script-name> -w ./packages/a
  • npm run <script-name> --workspace=a
  • npm run <script-name> --workspace=./packages/a

It also supports test|start|stop|restart top level comands, so if you're using any of these you can just add -w <workspaces-name> and it should work as expected, e.g:

  • npm test -w a

In case you want to run a script against all the configured workspaces, there's a workspaces configuration option that enables just that:

  • npm test -ws
  • npm test --workspaces

Lastly it's also possible to run scripts in a group of workspaces by using the path to their parent folder as the workspace config value. That means running npm test -w ./packages will run tests in all workspaces nested at the ./packages location.

Want to learn more about it?

We updated the docs, see:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement new feature or improvement Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

No branches or pull requests

8 participants