如何在没有sudo的情况下运行NPM?

Mar*_*tyn 2 ubuntu node.js npm

我一直在阅读帖子,试图找出如何在npm install没有的情况下运行sudo.我还看到了以下文章,但我认为这是针对较旧版本的NPM.我完成了以下操作,产生的错误较少,但最后仍会抛出错误:

sudo chown -R $USER /usr/local
Run Code Online (Sandbox Code Playgroud)

我没有错误的唯一方法就是跑,sudo npm install但我读到这不是一个好主意.

以下是运行后我看到的内容.我的猜测是权限,但我真的不知道我应该知道哪些文件夹(如果有的话).对于大量数据感到抱歉,我真的不知道什么是相关的.我在日志中看不到任何有用的东西(/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
Run Code Online (Sandbox Code Playgroud)

Mic*_*man 5

如果你想运行npm install -g protractor不需要使用sudo的命令,你需要

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

首先需要确定当前配置节点以安装全局包的位置

基于 NPM官方配置文档 - 以及
运行的各种其他来源:

$ npm config get prefix
Run Code Online (Sandbox Code Playgroud)

要么

$ npm config list
Run Code Online (Sandbox Code Playgroud)

或(显示所有配置默认值)

$ npm config ls -l
Run Code Online (Sandbox Code Playgroud)

NPM 前缀值确定节点包的安装位置.

换句话说,如果运行结果 npm config list如下所示:

; userconfig /home/michael/.npmrc
prefix = "/home/michael/npm"
Run Code Online (Sandbox Code Playgroud)

它的意思是

  • 你有一个用户级配置/home/michael/.npmrc,NPM将以比全局/系统配置文件更高的优先级进行解析
  • 当您指定全局安装时,将安装npm install -g protractor量角器文件 /home/michael/npm
现在进行更改!

prefix使用以下方式直接设置:

npm config set prefix ~/npm
Run Code Online (Sandbox Code Playgroud)

将NPM包脚本文件放入BASH的路径中

附加(编辑).bashrc文件:

export PATH="$PATH:$HOME/npm/bin"
Run Code Online (Sandbox Code Playgroud)

并运行

$ source .bashrc
Run Code Online (Sandbox Code Playgroud)

重新解析.bashrc脚本