How to Fix: npm WARN cannot run in wd

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:

1 Comment

Leave a Reply to Bayu Hafiz Cancel reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.