如何在不使用sudo的情况下运行NPM?

9

我一直在阅读文章,试图弄清楚如何在不使用sudo的情况下运行npm install。我也看到了以下文章,但我认为它是针对旧版本的NPM的。我尝试了以下步骤,少出现一些错误,但仍会在最后报错:

sudo chown -R $USER /usr/local

我能够得到无错误的唯一方法是运行sudo npm install,但我读到这不是一个好主意。
下面是我运行它后看到的输出。我的猜测是权限问题,但我不知道我应该chown哪些文件夹(如果有的话)。对于如此庞大的数据,我很抱歉,我不知道什么是相关的。我在日志中也没有看到任何有用的信息(/var/www/budget/npm-debug.log),尽管它非常庞大。还有什么需要chown吗?
$ npm install
npm WARN package.json budget@0.0.1 No description
npm WARN package.json budget@0.0.1 No repository field.
npm WARN package.json budget@0.0.1 No README data
npm http GET https://registry.npmjs.org/grunt-contrib-watch
npm http GET https://registry.npmjs.org/grunt-contrib-less
.
.
.
npm http 304 https://registry.npmjs.org/hawk/1.1.1
npm http GET https://registry.npmjs.org/delayed-stream/0.0.5
npm ERR! Error: ENOENT, chmod '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/bl/test/test.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/bl/test/test.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/bl/test/test.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! fstream_finish_call chmod
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:305:19
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! error rolling back Error: ENOTEMPTY, rmdir '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/node-uuid'
npm ERR! error rolling back  request@2.51.0 { [Error: ENOTEMPTY, rmdir '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/node-uuid']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/node-uuid' }
npm WARN optional dep failed, continuing request@2.51.0
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/qs/lib/utils.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/qs/lib/utils.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/qs/lib/utils.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/http-signature/lib/util.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/http-signature/lib/util.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/http-signature/lib/util.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm http 304 https://registry.npmjs.org/delayed-stream/0.0.5
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/tough-cookie/test.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/tough-cookie/test.js
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/tough-cookie/test.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
grunt-cache-breaker@1.0.1 node_modules/grunt-cache-breaker
└── cache-breaker@0.0.4 (lodash@2.2.1)

grunt-contrib-watch@0.5.3 node_modules/grunt-contrib-watch
├── tiny-lr@0.0.4 (debug@0.7.4, faye-websocket@0.4.4, noptify@0.0.3, qs@0.5.6)
└── gaze@0.4.3 (globule@0.1.0)

grunt@0.4.5 node_modules/grunt
├── which@1.0.8
├── dateformat@1.0.2-1.2.3
├── eventemitter2@0.4.14
├── getobject@0.1.0
├── rimraf@2.2.8
├── colors@0.6.2
├── hooker@0.2.3
├── async@0.1.22
├── grunt-legacy-util@0.2.0
├── exit@0.1.2
├── minimatch@0.2.14 (sigmund@1.0.0, lru-cache@2.5.0)
├── lodash@0.9.2
├── coffee-script@1.3.3
├── underscore.string@2.2.1
├── iconv-lite@0.2.11
├── glob@3.1.21 (inherits@1.0.0, graceful-fs@1.2.3)
├── nopt@1.0.10 (abbrev@1.0.5)
├── findup-sync@0.1.3 (lodash@2.4.1, glob@3.2.11)
├── grunt-legacy-log@0.1.1 (underscore.string@2.3.3, lodash@2.4.1)
└── js-yaml@2.0.5 (argparse@0.1.16, esprima@1.0.4)

grunt-contrib-cssmin@0.10.0 node_modules/grunt-contrib-cssmin
├── chalk@0.4.0 (has-color@0.1.7, ansi-styles@1.0.0, strip-ansi@0.1.1)
├── clean-css@2.2.22 (commander@2.2.0)
└── maxmin@0.2.2 (figures@1.3.5, pretty-bytes@0.1.2, chalk@0.5.1, gzip-size@0.2.0)

grunt-contrib-less@0.7.0 node_modules/grunt-contrib-less
├── grunt-lib-contrib@0.6.1 (zlib-browserify@0.0.1)
└── less@1.4.2 (mime@1.2.11, mkdirp@0.3.5, ycssmin@1.0.1)
npm ERR! Error: Callback called more than once.
npm ERR!     at errorHandler (/usr/share/npm/lib/utils/error-handler.js:80:16)
npm ERR!     at save (/usr/share/npm/lib/install.js:329:12)
npm ERR!     at /usr/share/npm/lib/install.js:98:7
npm ERR!     at /usr/share/npm/lib/install.js:188:5
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:119:5
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:233:14
npm ERR!     at cb (/usr/lib/nodejs/slide/lib/async-map.js:48:11)
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:233:14
npm ERR!     at cb (/usr/lib/nodejs/slide/lib/async-map.js:48:11)
npm ERR!     at /usr/share/npm/node_modules/read-installed/read-installed.js:233:14
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/mime-types/lib/mime.json'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/mime-types/lib/mime.json
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/mime-types/lib/mime.json
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! Error: ENOENT, lstat '/var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/hawk/images/logo.png'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Linux 3.13.0-40-generic
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install"
npm ERR! cwd /var/www/budget
npm ERR! node -v v0.10.25
npm ERR! npm -v 1.3.10
npm ERR! path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/hawk/images/logo.png
npm ERR! fstream_path /var/www/budget/node_modules/grunt-contrib-less/node_modules/less/node_modules/request/node_modules/hawk/images/logo.png
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack /usr/lib/nodejs/fstream/lib/writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:107:15)
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /var/www/budget/npm-debug.log
npm ERR! not ok code 0
4个回答

12

如果您想在不使用 sudo 的情况下运行像 npm install -g protractor 这样的命令,则需要执行以下操作:

  • 配置 NPM,将包文件复制到并从不需要 sudo 权限的位置执行(我认为 Ubuntu 中唯一默认允许此行为的位置是/home/user-name/
    .
  • 更新 $PATH 变量,以便 BASH 在确定可从命令提示符中运行的脚本/程序时查找该变量。

首先需要确定 Node 当前配置了哪个位置来安装全局包

根据NPM官方配置文档以及其他各种来源
执行:

$ npm config get prefix
或者
$ npm config list

或者(展示所有配置默认值)

$ npm config ls -l

NPM 前缀值 确定了 node 包的安装位置。

换句话说,如果运行 npm config list 的结果看起来像这样:

; userconfig /home/michael/.npmrc
prefix = "/home/michael/npm"

这意味着:

  • 您拥有一个用户级别的配置文件 /home/michael/.npmrc,NPM会以比全局/系统配置文件更高的优先级来解析它。
  • 当您指定全局安装 npm install -g protractor 时,protractor文件将被安装到 /home/michael/npm 中。
现在进行更改!

通过使用以下方式直接设置prefix

npm config set prefix ~/npm

将NPM软件包脚本文件添加到BASH的路径中

.bashrc文件末尾添加(编辑)以下内容:

export PATH="$PATH:$HOME/npm/bin"

并运行

$ source .bashrc

重新解析 .bashrc 脚本


搞定了,谢谢。我记得之前为了RVM也做过类似的事情,因为我已经有了PATH=$PATH:$HOME/.rvm/bin。现在我的.bashrc文件里有了PATH=$PATH:$HOME/.rvm/bin:$HOME/.npm/bin,并且我将prefix设置为“~/.npm”。 - Martyn
太棒了,是的~/.npm也是一个好方法 - 这样你就不会在主目录中看到npm文件夹。 - Michael Coleman

2
从日志中看,你的应用程序存储在/var/www/budget目录中。你进入此目录并运行npm install。然而,这个路径通常是Apache服务用户(apachehttpd,不确定具体是哪个,这也可能取决于你使用的软件)所拥有的。 我不建议将此文件夹的所有权转移到你的用户帐户。但是,将你的用户帐户添加到apache用户组中,并更改权限以使该组具有读/写访问权限(如果它还没有),可能是可以接受的:
(在执行之前,请确保该组确实是apache,否则你会给其他组赋予读/写权限)
sudo chmod -R g+r,g+w /var/www

通常情况下,您允许全局安装 npm 模块(使用 -g 标志安装到 /usr/local 目录),但是您的应用程序不驻留在该目录中,因此您必须决定修改该特定应用程序路径的权限设置的最佳方法。


1
在我看来,最好的做法是使用版本管理器,例如nvmn。这样,您可以安装不同版本的node,并轻松地在它们之间切换,而可执行文件将存储在不同的PATH下,在nvm的情况下为~/.nvm/v0.10.33/bin,因此不再需要sudo权限。

0

一种冒险的方法,前往 /etc/sudoers 并在末尾添加以下行:

USERNAME HOSTNAME = (root) NOPASSWD: /usr/bin/npm

USERNAME - 用户名,无需检查sudo权限的用户

HOSTNAME - 您的计算机域名

在编辑/etc/sudoers时出现任何错误都会导致您的sudo命令停止工作,您需要使用grub模式来恢复您的PC。


谢谢,但这样只是将我的用户名设置为以sudo身份运行,而不检查密码吗?我不希望那样。我想设置它,以便根本不需要sudo访问权限。 - Martyn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接