npm fails while preparing a dependency installed from git
npm ERR! git dep preparation failedShort answer
When npm installs a package from a git URL, it may have to build it first. If that package has a prepare or build script, npm installs its dependencies and devDependencies and runs the script, and any error there ends the install. Read the output above the message, then pin a working commit or use a published release.
Fix it fast
1Find out which cause you have
Scroll up from the error. The lines before 'git dep preparation failed' hold the real failure from the dependency's own build.
npm install 2>&1 | tail -n 602Match what you see, then jump to its fix
What does the error look like?
npm ERR! git dep preparation failed
Cause 1 of 2
The dependency's prepare or build script fails
1Check
npm treats a git dependency as needing a build when its package.json has workspaces or scripts such as build, prepare, prepack, preinstall, install or postinstall. With a prepare script it installs the dependency's devDependencies and runs the script before packaging.
grep -n -A8 "scripts" node_modules/my-lib/package.json2Fix
Use a published release from the registry instead of the git URL if one exists. Otherwise fix the build problem the log shows (for example a Node.js version the package does not support).
npm uninstall my-lib
npm install my-lib@latest3Verify
npm install finishes with no preparation error.
npm install && npm ls my-libConfirmed by docs.npmjs.com
Cause 2 of 2
No ref given, so the default branch is installed
1Check
Without #commit-ish or #semver:, npm installs the repository's default branch, so a new commit there can break the install.
grep -n "git" package.json2Fix
Pin a commit or a semver tag in the dependency URL.
"dependencies": {
"my-lib": "git+https://github.com/example/my-lib.git#semver:^1.2.0"
}3Verify
Install from a clean node_modules.
rm -rf node_modules && npm installConfirmed by docs.npmjs.com
How do I stop it coming back?
- Prefer registry releases over git URLs for anything that needs a build.
- Always pin a commit-ish or semver tag on git dependencies.
- Commit the lockfile so every machine installs the same commit.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
npm install of a git dependency stops with code 128
npm install of a git dependency ends with code 128
Code 128 is git failing, not npm.
4 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fix