Skip to content

[BUG] prepare and prepack scripts are not handled as documented when installing a git dependency #1865

Closed

Description

@kimamula

Current Behavior:

I tested installing a git dependency which has either prepare or prepack script with npm CLI v7.0.0-beta.12 and confirmed the following behaviors.

  • a package with a prepare script: devDependencies are NOT installed but the prepare script is executed
  • a package with a prepack script: devDependencies are NOT installed and the prepack script is NOT executed

Expected Behavior:

  • a package with a prepare script: devDependencies are installed and the prepare script is executed
  • a package with a prepack script: devDependencies are installed and the prepack script is executed
    • Though I've not found any document saying that devDependencies are installed when installing a git dependency with a prepack script and actually they are not installed even when npm CLI v6 is used, I believe npm should install them since running a prepack script without installing devDependencies does not make sense.

Steps To Reproduce:

prepare:

// https://github.com/kimamula/prepare-test/blob/master/package.json
{
  "name": "prepare-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepare": "tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "^4.0.3"
  }
}
$ npm install github:kimamula/prepare-test
npm ERR! code 127
npm ERR! git dep preparation failed
npm ERR! command /Users/kenji/.nodenv/versions/12.18.3/bin/node /Users/kenji/.nodenv/versions/12.18.3/lib/node_modules/npm/bin/npm-cli.js install --only=dev --prod --ignore-prepublish --no-progress --no-save --cache=/Users/kenji/.npm/_cacache --prefer-offline=false --prefer-online=false --offline=false --before=
npm ERR! > prepare-test@1.0.0 prepare
npm ERR! > tsc
npm ERR! npm WARN invalid config before="" set in command line options
npm ERR! npm WARN invalid config Must be one of: null, valid Date string
npm ERR! sh: tsc: command not found
npm ERR! npm ERR! code 127
npm ERR! npm ERR! path /Users/kenji/.npm/_cacache/tmp/git-clone-a73f300b
npm ERR! npm ERR! command failed
npm ERR! npm ERR! command sh -c tsc
npm ERR! 
npm ERR! npm ERR! A complete log of this run can be found in:
npm ERR! npm ERR!     /Users/kenji/.npm/_cacache/_logs/2020-09-27T06_13_19_941Z-debug.log

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/kenji/.npm/_logs/2020-09-27T06_13_19_972Z-debug.log

prepack:

// https://github.com/kimamula/prepack-test/blob/master/package.json
{
  "name": "prepack-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "prepack": "tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "typescript": "^4.0.3"
  }
}
$ npm install github:kimamula/prepack-test

changed 1 package, and audited 1 package in 6s

found 0 vulnerabilities

-> tsc should generate index.js file, but it's not generated.

Environment:

  • OS: macOS 10.15.6
  • Node: 12.18.3
  • npm: 7.0.0-beta.12

Activity

  1. added
    Bugthing that needs fixing
    Needs Triageneeds review for next steps
    Release 7.xwork is associated with a specific npm 7 release
    on Sep 27, 2020
  2. added and removed
    Needs Triageneeds review for next steps
    on Oct 1, 2020
  3. added this to the OSS - Sprint 16 milestone on Oct 1, 2020
  4. darcyclarke commented on Oct 1, 2020

    @darcyclarke
    Contributor

    @kimamula just to confirm, does this behavior reflect how npm@6.x works today? Or, have we changed how that works?

  5. kimamula commented on Oct 2, 2020

    @kimamula
    Author

    @darcyclarke The behavior of npm@6.x is different (see #1229).

    • a package with a prepare script: working as expected (devDependencies are installed and the prepare script is executed)
    • a package with a prepack script: devDependencies are NOT installed but the prepack script is executed
  6. 18 remaining items

  7. fwcd commented on Jan 12, 2023

    @fwcd

    I can confirm that the issue that devDependencies are not installed before prepare is executed is not solved, I have a TypeScript package with a package.json like this:

    {
      "scripts": {
        "prepare": "tsc ..."
      },
      "devDependencies": {
        "typescript": "..."
      }
    }

    Running npm install with npm 9.2.0 errors with

    npm ERR! sh: tsc: command not found
    

    whereas yarn handles this installation perfectly well. As a temporary workaround the required devDependencies can be installed globally, in this case the TypeScript compiler, but as a solution this isn't great.

  8. added a commit that references this issue on Jan 12, 2023
  9. aarondill commented on Feb 4, 2023

    @aarondill

    I can confirm that on npm version 9.4.1, prepack is still not being called when installing git dependencies.
    the test in #1865 (comment) demonstrate this issue perfectly. prepare is run, but prepack is never called.

  10. aarondill commented on Feb 4, 2023

    @aarondill

    workspaces/libnpmpack/lib/index.js is the only place in this codebase which calls prepack, it seems like the dependency pacote anticipates the cli to handle prepack(pacote/blob/main/lib/dir.js#L39-L41), yet it's only handled on npm pack and npm publish
    A new issue in npm/pacote has been created to correspond with this one until the problem is solved: npm/pacote#257

  11. ljharb commented on Feb 4, 2023

    @ljharb
    Contributor

    Why would prepack run with git deps, when they’re never packed or published?

  12. fregante commented on Feb 4, 2023

    @fregante
    Contributor

    That makes sense, but the lifecycle of npm scripts still seems alien to me. I recall prepack shortly being the preferred way to execute scripts before publishing a few years back.

    Installing from git bypasses the entire packaging and publishing sequence that repos go through when running npm publish, in a way I'd expect the same sequence to be repeated when running npm install user/repo. Otherwise we get these weird lifecycle events that vary depending on how the package is installed.

  13. ljharb commented on Feb 4, 2023

    @ljharb
    Contributor

    Prepack works great for a “before publish” step; i use it - but git deps are never published. prepare is used when you want to support installing from git (which i have no interest in), and it should run pre publish and pre install.

  14. fregante commented on Feb 4, 2023

    @fregante
    Contributor

    Prepack works great for a “before publish” step; i use it -

    Exactly, but why do you use it? Is it to run a build? Then it'd make sense to have a script that runs when installing from git

    to support installing from git (which i have no interest in)

    Do you say that as a module author? Then yeah, that's not great, is it. As a module author myself, I shouldn't need to bother about that either.

    Does npm install support installing from git or not? These half-implementations really cause a lot of confusion. Why should anyone use prepack if prepare is the right one to support both? I wish there was one way to do things in npm land.

  15. ljharb commented on Feb 4, 2023

    @ljharb
    Contributor

    I use it to autogenerate an npmignore file, which isn’t a relevant task unless it’s being published.

    As a module author, i always need to bother with the way my users install things - anything i support is something i have to support, and since i believe nothing should ever be consumed except from an npm registry, that’s what i wish to support.

    npm install supports that, but the thing you’re installing may not.

  16. aarondill commented on Feb 5, 2023

    @aarondill

    Why should anyone use prepack if prepare is the right one to support both?

    prepare runs on local npm install with no arguments, thus when cloning a repo and installing dependencies, the prepare script is run, which would likely build the project, as well as filling the terminal with lots of information that's only relevant to building(as many build steps output debug information)

    From the docs, prepack should run everywhere prepare does, except for local npm install, thus still allowing usage of npm pack and installation from git repos, without auto-building when installing dependencies.

  17. jakebailey commented on Mar 30, 2023

    @jakebailey

    Not to pile on here, but, we were actually considering adding a prepack to TypeScript to make bisecting checker changes simpler, only to find out that prepack doesn't actually work in v7+ anymore.

    prepare is the wrong script to be using for our needs, as we'd incur a full build at a plain npm install or even npm ci at the repo root, wheras prepack is much better and used by yarn. But, given npm doesn't seem to support it anymore, we just gave up on it.

    Does this need to be filed as a brand new issue? Reopened? Is npm/pacote#257 the right place for this now?

  18. aovchinn commented on May 9, 2023

    @aovchinn

    is this happening again ?
    I tested it on npm 7 and 9
    created #6435

  19. jakebailey commented on May 9, 2023

    @jakebailey

    @aovchinn Not sure your linked issue is relavent to this issue; this issue is about npm not executing the prepack script on installs via git in v7+, not about missing things on PATH.

    Er, no, sorry. This issue contains multiple issues all in one.

  20. cstrahan commented on Jul 17, 2025

    @cstrahan

    This was fixed for non-global installs, but it's still broken when passing -g; opened an issue here: #8440

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingRelease 7.xwork is associated with a specific npm 7 release

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions