You are not logged in.

#1 2017-06-02 05:33:40

perqin
Member
Registered: 2017-06-02
Posts: 8

[SOLVED]Module owned by sudo user when installing npm module with sudo

When I was trying to upgrade package "npm", error occurred:

$ sudo pacman -S npm                                                                                                                                                                                                                  
resolving dependencies...
looking for conflicting packages...

Packages (1) npm-4.6.1-1

Total Installed Size:  14.02 MiB

:: Proceed with installation? [Y/n] Y
(1/1) checking keys in keyring                                                                                                             [####################################################################################] 100%
(1/1) checking package integrity                                                                                                           [####################################################################################] 100%
(1/1) loading package files                                                                                                                [####################################################################################] 100%
(1/1) checking for file conflicts                                                                                                          [####################################################################################] 100%
error: failed to commit transaction (conflicting files)
npm: /usr/lib/node_modules/npm/lib/utils/perf.js exists in filesystem
npm: /usr/lib/node_modules/npm/node_modules/readable-stream/duplex-browser.js exists in filesystem
npm: /usr/lib/node_modules/npm/node_modules/readable-stream/lib/internal/streams/stream-browser.js exists in filesystem
npm: /usr/lib/node_modules/npm/node_modules/readable-stream/lib/internal/streams/stream.js exists in filesystem
npm: /usr/lib/node_modules/npm/node_modules/readable-stream/node_modules/string_decoder/lib/string_decoder.js exists in filesystem
npm: /usr/lib/node_modules/npm/node_modules/readable-stream/readable-browser.js exists in filesystem
npm: /usr/lib/node_modules/npm/node_modules/readable-stream/writable-browser.js exists in filesystem
npm: /usr/lib/node_modules/npm/test/tap/version-allow-same-version.js exists in filesystem
Errors occurred, no packages were upgraded.

Then I checked the permission of the files above:

$ ls -l                                                                                                                                                                                                                               
total 20
drwxr-xr-x 4 nobody perqin 4096 May 13 15:48 bower
drwxr-xr-x 6 nobody perqin 4096 May 13 15:49 gulp
drwxr-xr-x 7 nobody root   4096 May 13 15:57 hexo-cli
drwxr-xr-x 5 nobody perqin 4096 Jun  2 13:00 npm
drwxr-xr-x 3 root   root   4096 Jul 15  2016 semver

I found that some global npm modules were owned by me (perqin). So I tried to install a global modules by:
(1)

$ sudo npm install -g forever

(2)

$ su
# npm install -g forever

And I found that only the later method set the permission properly (owned by nobody:root).

So my question is: how to install npm modules globally with sudo, but setting the permission to root, not the sudo user, me?

Last edited by perqin (2017-06-02 13:14:54)

Offline

#2 2017-06-02 06:54:36

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,309

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

This is not a problem with ownership. You added some files (likely via "npm install") that are (now) also provided by the package. You need to remove them/uninstall them with npm before installing the package or (dangerous, you need to contain the impact, don't use this as a global flag on all packages!) --force the installation.

Also "I was trying to upgrade package "npm"" is NOT supported or suggested - you're supposed to run full upgrades. Otherwise you may ecounter dependency issues and you frankly do sound like you could handle those in doubt.

https://bbs.archlinux.org/viewtopic.php?id=130138
https://wiki.archlinux.org/index.php/Sy … nsupported
https://bbs.archlinux.org/viewtopic.php?id=89328

Online

#3 2017-06-02 08:45:58

perqin
Member
Registered: 2017-06-02
Posts: 8

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

seth wrote:

This is not a problem with ownership. You added some files (likely via "npm install") that are (now) also provided by the package. You need to remove them/uninstall them with npm before installing the package or (dangerous, you need to contain the impact, don't use this as a global flag on all packages!) --force the installation.

Also "I was trying to upgrade package "npm"" is NOT supported or suggested - you're supposed to run full upgrades. Otherwise you may ecounter dependency issues and you frankly do sound like you could handle those in doubt.

https://bbs.archlinux.org/viewtopic.php?id=130138
https://wiki.archlinux.org/index.php/Sy … nsupported
https://bbs.archlinux.org/viewtopic.php?id=89328

I tried to upgrade my system including npm and it failed with that error so I turned to upgrade npm only. Actually I couldn't even remove npm and nodejs packages due to this ownership issue.

I think it is a sudo-related issue, because now I found that I can't install any npm modules globally using sudo. Here's what I've tried.

Firstly I tried to install a module with sudo:

$ sudo npm install -g hexo-cli

and got an error:

$ sudo npm -g install hexo-cli                                                  
/usr/bin/hexo -> /usr/lib/node_modules/hexo-cli/bin/hexo

> dtrace-provider@0.8.2 install /usr/lib/node_modules/hexo-cli/node_modules/dtrace-provider
> node scripts/install.js


> hexo-util@0.6.0 postinstall /usr/lib/node_modules/hexo-cli/node_modules/hexo-util
> npm run build:highlight

npm ERR! path /root/.npm/_logs
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall scandir
npm ERR! Error: EACCES: permission denied, scandir '/root/.npm/_logs'
npm ERR!  { Error: EACCES: permission denied, scandir '/root/.npm/_logs'
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'scandir',
npm ERR!   path: '/root/.npm/_logs' }
npm ERR! 
npm ERR! Please try running this command again as root/Administrator.
glob error { Error: EACCES: permission denied, scandir '/root/.npm/_logs'
  errno: -13,
  code: 'EACCES',
  syscall: 'scandir',
  path: '/root/.npm/_logs' }

> hexo-util@0.6.0 build:highlight /usr/lib/node_modules/hexo-cli/node_modules/hexo-util
> node scripts/build_highlight_alias.js > highlight_alias.json

npm ERR! Callback called more than once.

┌───────────────────────────────────────────────────┐
│              npm update check failed              │
│        Try running with sudo or get access        │
│       to the local update config store via        │
│ sudo chown -R $USER:$(id -gn $USER) /root/.config │
└───────────────────────────────────────────────────┘
/usr/lib
└── (empty)

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@^1.0.0 (node_modules/hexo-cli/node_modules/chokidar/node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.1: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})
npm ERR! code ELIFECYCLE
npm ERR! errno 243
npm ERR! hexo-util@0.6.0 postinstall: `npm run build:highlight`
npm ERR! Exit status 243
npm ERR! 
npm ERR! Failed at the hexo-util@0.6.0 postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2017-06-02T08_26_37_774Z-debug.log

Then I check the "/root/.npm" directory:

# ls -la | grep .npm
drwxr-xr-x 244 perqin perqin 12288 Jun  2 13:06 .npm

Hey! Why is it owned by me? So I chown it:

# chown -R root:root /root/.npm

And tried installing again:

$ sudo npm install -g hexo-cli

I got the same error and found that "/root/.npm" was set to owned by perqin again!!
Now I use su:

$ su
# npm install -g hexo-cli

Everything works!

Now, I think that it should be an issue related to the sudo command. It seems that a command run with sudo will cause some files' ownership set to the sudo user.

And unfortunately, I don't know how to solve the problem sad

Offline

#4 2017-06-02 09:33:22

HiImTye
Member
From: Halifax, NS, Canada
Registered: 2012-05-09
Posts: 1,072

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

the owner of a file using sudo depends entirely on the execution chain involved with the command sudo uses. for example, you can see a clear ownership difference in the following:

sudo sh -c "echo test>test1"
sudo echo test>test2
sudo vim test3
ls -l test*

if the file creation takes place inside your original shell, rather than a root shell, the file permissions will remain as your regular user. if they take place in a root shell, root will be owner.

edit: yes, I know it's more complex than this but I just wanted to make it simple enough to explain easily

Last edited by HiImTye (2017-06-02 09:41:40)

Offline

#5 2017-06-02 12:28:07

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,309

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

the problem is rather the npm post-install hook which is not executed as root/sudo, so this is a problem of npm and not sudo (while executing it in a subshell might or not help on this)

also this is *entirely* unrelated to your pacman updating related issues. pacman simply complains about the untracked files in place (which you likely installed via npm) - that's not a permission problem but a "why is this there and should this be overridden" conflict/question.

Online

#6 2017-06-02 13:13:51

perqin
Member
Registered: 2017-06-02
Posts: 8

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

seth wrote:

the problem is rather the npm post-install hook which is not executed as root/sudo, so this is a problem of npm and not sudo (while executing it in a subshell might or not help on this)

also this is *entirely* unrelated to your pacman updating related issues. pacman simply complains about the untracked files in place (which you likely installed via npm) - that's not a permission problem but a "why is this there and should this be overridden" conflict/question.

Yes you are right. The reason I failed to upgrade npm is that npm itself is considered an node module located in /usr/lib/node_modules/. And due to npm's problem, the "npm" module's directory is owned by me, so pacman cannot update the file. So this post is considered solved. Thanks for everyone replying to this post smile

Offline

#7 2017-06-02 13:20:51

seth
Member
From: Don't DM me only for attention
Registered: 2012-09-03
Posts: 74,309

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

pacman can override the files because it runs sudo/as root - UID0 can do everything, any permission restriction is just ignored (ie. UID0 can even enter non-executable directories)

sudo pacman -S --force npm

This overrides the untracked files with the package content.
Be aware that this *can* cause dependency problems (if eg another untracked file expects a version that you just replaced, this depends on why and what you installed there)
Also *never* use the --force flag w/o being sure about what you're doing there and ESPECIALLY NOT as a general "pacman -Syu --force" or such, that's begging for trouble.

Online

#8 2017-06-02 13:23:20

ayekat
Member
Registered: 2011-01-17
Posts: 1,632

Re: [SOLVED]Module owned by sudo user when installing npm module with sudo

perqin wrote:

The reason I failed to upgrade npm is that npm itself is considered an node module located in /usr/lib/node_modules/. And due to npm's problem, the "npm" module's directory is owned by me, so pacman cannot update the file.

Not quite. Pacman has no notion of what can be "considered a node module". And ownership also has nothing to do with this.
As seth has noted above, pacman simply detects that the files exist, and thus stops there (rather than overwriting the files).


pkgshackscfgblag

Offline

Board footer

Powered by FluxBB