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

npm install --workspaces runs scripts in parent package #3598

Closed
1 task done
fasttime opened this issue Aug 2, 2021 · 5 comments
Closed
1 task done

npm install --workspaces runs scripts in parent package #3598

fasttime opened this issue Aug 2, 2021 · 5 comments
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release

Comments

@fasttime
Copy link

fasttime commented Aug 2, 2021

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

npm install --workspaces runs scripts defined in the package.json file of the parent package and ignores scripts in the package.json files of the workspace packages. The scripts are in order:

  • preinstall
  • install
  • postinstall
  • prepublish
  • preprepare
  • prepare
  • postprepare

Expected Behavior

npm install --workspaces should run scripts defined in the package.json files of the workspace packages, not those defined in the parent package.json.

Steps To Reproduce

Create a package with these two files:

./package.json

{
    "scripts": {
        "install": "echo INSTALLING PARENT"
    },
    "workspaces": [
        "./child"
    ]
}

./child/package.json

{
    "scripts": {
        "install": "echo INSTALLING CHILD"
    }
}

Run npm install --workspaces. This will output "INSTALLING PARENT" instead of "INSTALLING CHILD".

Environment

  • OS: ubuntu-latest
  • Node: 16.6.0
  • npm: 7.19.1
@fasttime fasttime 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 Aug 2, 2021
@alvis
Copy link

alvis commented Aug 6, 2021

Not sure if it's a feature or bug. Ironically, I'm here for the opposite problem.
I just want npm i runs the parent scripts only as I have lerna for bootstrapping.
It works for me up to npm 7.19. But since npm 7.20, the behaviour has changed and scripts defined in all child packages are run upon npm i but not the parent ones.

@hpohlmeyer
Copy link

hpohlmeyer commented Feb 24, 2022

This is not only a problem if you run npm install --workspaces and traget all workspaces, but for specific workspaces as well (npm i -w child) will only run the root lifecycle methods as well.

Not sure if it's a feature or bug.

It should be a bug, it is written down in the RFC, but the implementation does not match the RFC:

Install: In a npm workspaces setup users expect to be able to install all nested packages and perform the associated lifecycle scripts from the Top-level workspace, it should also be aware of workspaces that have a dependency on one another and symlink them appropriately.

https://github.com/npm/rfcs/blob/main/implemented/0026-workspaces.md#detailed-explanation


And to add some more info to the issue, here is our problem with the current behavior:

We have a monorepo with three workspaces:

  • frontend
  • backend
  • shared

Our backend uses prisma and generates the prisma client in a postinstall hook. So prisma is a dependency of the backend.

Locally everthing works fine, but we the problems start in our CI scripts. If we want to deploy the frontend we runnpm ci -w frontend, but that fails because the postinstall script inside backend will not be executed. Instead we can move it to the root. But that also fails, because npm ci -w frontend only installs the dependencies of the frontend, which do not include prisma.

@hpohlmeyer
Copy link

I just found a comment from @fritzy in another issue:

Since npm install does not do workspace specific installs (they're global and hoisted by default), the postinstall script of a workspace will never be invoked, and the root one will.

– #4412 (comment)

I am not sure if this comment is aimed at running npm install without any workspace parameters, or if it is aimed at all npm install calls, including the ones with a specified workspace. Maybe you could elaborate on that @fritzy?

@Sogomn
Copy link

Sogomn commented Mar 11, 2022

Lifecycle scripts aren't even run when calling npm install from within the workspace package.
Right now the only way to run them is to move the scripts into root or run them one by one manually, kind of annoying.

@fritzy
Copy link
Contributor

fritzy commented Jun 30, 2022

@hpohlmeyer as you quoted in the RFC, it performs the lifecycles scripts of the top-level workspace, not the children. This is working as designed, but if you'd like to change this behavior, please open a discussion in the RFCs repo. Can you create an RRFC in that repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug thing that needs fixing Needs Triage needs review for next steps Release 7.x work is associated with a specific npm 7 release
Projects
None yet
Development

No branches or pull requests

5 participants