You may encounter this issue when you run npm with root permission.
In my case, I created a postinstall command to fix a particular node module’s problem:
{
"scripts": {
"postinstall": "node ./semantic-fix.js"
}
}
If works good on my local Mac, but failed on my server, during the build process, it failed to run the postinstall
command:
npm WARN lifecycle dashboard@1.0.0~postinstall: cannot run in wd %s %s (wd=%s) dashboard@1.0.0 node ./semantic-fix.js /app
The problem is caused by that npm uses an incorrect privilege to invoke the command(see npm documentation):
If npm was invoked with root privileges, then it will change the uid to the user account or uid specified by the user config, which defaults to nobody. Set the unsafe-perm flag to run scripts with root privileges.
The solution is simple:
npm install --unsafe-perm
BTW, the method below does not work in this case (see this issue):
{
"scripts": {
"postinstall": "node ./semantic-fix.js"
},
"config": {
"unsafe-perm": true
}
}
References:
- https://docs.npmjs.com/misc/scripts#user
- Npm install failed with “cannot run in wd”
- How to fix Npm install failed with “cannot run in wd”
- NodeJS unsafe-perm not working on package.json
1 Comment
Great one! Thanks for the solution.