簡體   English   中英

如何在沒有 sudo 的情況下運行 NPM?

[英]How can I run NPM without sudo?

我一直在閱讀帖子,試圖弄清楚如何在沒有sudo情況下運行npm install 我還看到了以下文章,但我認為它適用於舊版本的 NPM。 我做了以下產生較少錯誤的操作,但最后仍然拋出錯誤:

sudo chown -R $USER /usr/local

我不會出錯的唯一方法是運行sudo npm install但我讀到這不是一個好主意。

下面是我運行后看到的結果。 我的猜測是權限,但我真的不知道我應該 chown 哪些文件夾(如果有的話)。 抱歉,數據量很大,我真的不知道什么是相關的。 我在日志中也看不到任何有用的東西(/var/www/budget/npm-debug.log),盡管它很大。 有什么我還需要吃的嗎?

$ 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

如果你想在不使用 sudo 的情況下運行像npm install -g protractor命令,你需要

  • 配置 NPM 以將包文件復制到不需要sudo權限的位置並從該位置執行(我相信 Ubuntu 中唯一允許這樣做的地方是/home/user-name/
    .
  • 在確定可以從命令提示符運行哪些腳本/程序時,更新 BASH 查看的 $PATH 變量

首先需要確定node當前配置在哪里安裝全局包

基於NPM 官方配置文檔- 以及其他各種來源
跑:

$ npm config get prefix

或者

$ npm config list

或(顯示所有配置默認值)

$ npm config ls -l

NPM前綴值確定安裝節點包的位置。

換句話說,如果運行npm config list的結果如下所示:

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

它的意思是

  • 你有一個用戶級別的配置/home/michael/.npmrc以比全局/系統配置文件更高的優先級來解析
  • 當您指定全局安裝npm install -g protractor量角器文件將安裝到/home/michael/npm
現在進行更改!

直接使用設置prefix

 npm config set prefix ~/npm

將 NPM 包腳本文件放入 BASH 的路徑中

追加(編輯) .bashrc文件:

$ source .bashrc

並運行

$ source .bashrc

重新解析.bashrc腳本

從日志中,似乎你有你的應用程序存儲在/var/www/budget ,你cwd進入這個目錄並運行npm install 但是,此路徑最常由 Apache 服務用戶擁有( apachehttpd ,不確定究竟是哪一個,這也可能取決於您使用的軟件)。

我不建議將此文件夾的所有權歸於您的用戶帳戶。 但是,將您的用戶帳戶放入apache用戶組並更改權限以便該組具有讀/寫訪問權限(如果已經沒有)可能是可以的:

(在執行之前確保該組確實是apache ,以免您將 r/w 權限授予其他組)

sudo chmod -R g+r,g+w /var/www

通常,您允許自己全局安裝 npm 模塊(到/usr/local 中,使用-g標志),但您的應用程序並不駐留在該目錄中,因此您必須決定修改權限設置的最佳行動方案對於該特定應用程序的路徑。

我認為最好的方法是使用版本管理器,例如nvmn 通過這種方式,您可以安裝不同版本的 node 並輕松地在它們之間切換,並且可執行文件將存儲在不同的 PATH 下,以防 nvm 在~/.nvm/v0.10.33/bin ,因此不再需要 sudo 。

一種冒險的方式,轉到 /etc/sudoers 並在最后添加這一行:

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

USERNAME - 不需要檢查 sudo 權限的用戶

HOSTNAME - 您的計算機域名

您在編輯 /etc/sudoers 時犯的任何錯誤都會使您的 sudo 命令停止工作,您需要使用 grub 模式來恢復您的 PC。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM