Repository navigation
[BUG] prepare and prepack scripts are not handled as documented when installing a git dependency #1865
Copy link
Copy link
Closed
Labels
Bugthing that needs fixingthing that needs fixingRelease 7.xwork is associated with a specific npm 7 releasework is associated with a specific npm 7 release
Milestone
Description
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
preparescript:devDependenciesare NOT installed but thepreparescript is executed - a package with a
prepackscript:devDependenciesare NOT installed and theprepackscript is NOT executed
Expected Behavior:
- a package with a
preparescript:devDependenciesare installed and thepreparescript is executed - a package with a
prepackscript:devDependenciesare installed and theprepackscript is executed- Though I've not found any document saying that
devDependenciesare installed when installing a git dependency with aprepackscript and actually they are not installed even when npm CLI v6 is used, I believe npm should install them since running aprepackscript without installingdevDependenciesdoes not make sense.
- Though I've not found any document saying that
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.logprepack:
// 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Bugthing that needs fixingthing that needs fixingRelease 7.xwork is associated with a specific npm 7 releasework is associated with a specific npm 7 release
Activity
darcyclarke commentedon Oct 1, 2020
@kimamula just to confirm, does this behavior reflect how
npm@6.xworks today? Or, have we changed how that works?kimamula commentedon Oct 2, 2020
@darcyclarke The behavior of
npm@6.xis different (see #1229).preparescript: working as expected (devDependenciesare installed and thepreparescript is executed)prepackscript:devDependenciesare NOT installed but theprepackscript is executed18 remaining items
fwcd commentedon Jan 12, 2023
I can confirm that the issue that
devDependenciesare not installed beforeprepareis executed is not solved, I have a TypeScript package with apackage.jsonlike this:{ "scripts": { "prepare": "tsc ..." }, "devDependencies": { "typescript": "..." } }Running
npm installwith npm 9.2.0 errors withwhereas yarn handles this installation perfectly well. As a temporary workaround the required
devDependenciescan be installed globally, in this case the TypeScript compiler, but as a solution this isn't great.Run build on prepare again
aarondill commentedon Feb 4, 2023
I can confirm that on npm version 9.4.1,
prepackis still not being called when installing git dependencies.the test in #1865 (comment) demonstrate this issue perfectly.
prepareis run, butprepackis never called.aarondill commentedon Feb 4, 2023
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 handleprepack(pacote/blob/main/lib/dir.js#L39-L41), yet it's only handled onnpm packandnpm publishA new issue in npm/pacote has been created to correspond with this one until the problem is solved: npm/pacote#257
ljharb commentedon Feb 4, 2023
Why would prepack run with git deps, when they’re never packed or published?
fregante commentedon Feb 4, 2023
That makes sense, but the lifecycle of npm scripts still seems alien to me. I recall
prepackshortly being the preferred way to execute scripts before publishing a few years back.Installing from
gitbypasses the entire packaging and publishing sequence that repos go through when runningnpm publish, in a way I'd expect the same sequence to be repeated when runningnpm install user/repo. Otherwise we get these weird lifecycle events that vary depending on how the package is installed.ljharb commentedon Feb 4, 2023
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.
fregante commentedon Feb 4, 2023
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
gitDo 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 installsupport installing from git or not? These half-implementations really cause a lot of confusion. Why should anyone useprepackifprepareis the right one to support both? I wish there was one way to do things in npm land.ljharb commentedon Feb 4, 2023
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.
aarondill commentedon Feb 5, 2023
prepareruns on localnpm installwith 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,
prepackshould run everywherepreparedoes, except for localnpm install, thus still allowing usage ofnpm packand installation from git repos, without auto-building when installing dependencies.jakebailey commentedon Mar 30, 2023
Not to pile on here, but, we were actually considering adding a
prepackto TypeScript to make bisecting checker changes simpler, only to find out thatprepackdoesn't actually work in v7+ anymore.prepareis the wrong script to be using for our needs, as we'd incur a full build at a plainnpm installor evennpm ciat 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?
aovchinn commentedon May 9, 2023
is this happening again ?
I tested it on npm 7 and 9
created #6435
jakebailey commentedon May 9, 2023
@aovchinn Not sure your linked issue is relavent to this issue; this issue is about
npmnot executing theprepackscript on installs via git in v7+, not about missing things onPATH.Er, no, sorry. This issue contains multiple issues all in one.
Update npmInstallCmd, run prepare scripts properly
cstrahan commentedon Jul 17, 2025
This was fixed for non-global installs, but it's still broken when passing
-g; opened an issue here: #8440