Skip to content

[BUG] cannot run npm scripts as root as long as the directory's owner is a regular user #4095

@zimtsui

Description

@zimtsui

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

If a project directory belongs to a linux regular user, all npm scripts in it cannot be run as root.

This problem doesn't exist on npm v6.x

Expected Behavior

When logged in as root, or running npm script with sudo, the scripts should be run as root.

Steps To Reproduce

zim@desktop:~$ mkdir ./haha
zim@desktop:~$ cd ./haha
zim@desktop:~/haha$ emacs ./package.json

~/haha/package.json:

{
  "name": "haha",
  "scripts": {
    "test": "whoami"
  }
}
zim@desktop:~/haha$ sudo npm run test
[sudo] password for zim: 

> test
> whoami

zim

zim is a regular user.

Environment

  • npm: 8.1.3
  • Node: 16.13.0
  • OS: Ubuntu 20.04
  • platform: amd64
  • npm config:
; "user" config from /home/zim/.npmrc

registry = "https://repo.huaweicloud.com/repository/npm/" 

; node bin location = /usr/local/bin/node
; cwd = /home/zim/haha
; HOME = /home/zim
; Run `npm config ls -l` to show all defaults.

My npm and node are installed from n

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bugthing that needs fixingNeeds Triageneeds review for next stepsRelease 8.xwork is associated with a specific npm 8 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions