0
我安裝了Node和NPM。無法使用sudo安裝全局程序包,只能運行
$ node -v
v0.10.25
$ npm -v
1.3.24
作爲一個普通用戶,如果我嘗試安裝一個全球性的包,我得到一個權限錯誤:
$ npm -g install grunt-cli
npm http GET https://registry.npmjs.org/grunt-cli
npm http 304 https://registry.npmjs.org/grunt-cli
npm ERR! error rolling back Error: EACCES, unlink '/usr/local/bin/grunt'
npm ERR! error rolling back [email protected] { [Error: EACCES, unlink '/usr/local/bin/grunt'] errno: 3, code: 'EACCES', path: '/usr/local/bin/grunt' }
npm ERR! Error: EACCES, unlink '/usr/local/bin/grunt'
npm ERR! { [Error: EACCES, unlink '/usr/local/bin/grunt'] errno: 3, code: 'EACCES', path: '/usr/local/bin/grunt' }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.
不夠公平,讓我用sudo
:
$ sudo npm -g install grunt-cli
/usr/local/bin/grunt -> /usr/local/lib/node_modules/grunt-cli/bin/grunt
[email protected] /usr/local/lib/node_modules/grunt-cli
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])
外貌好,但現在如果我嘗試使用我的新安裝的軟件包,我不能:
$ grunt
fs.js:695
return binding.readlink(pathModule._makeLong(path));
^
Error: EACCES, permission denied '/usr/local/bin/grunt'
at Object.fs.readlinkSync (fs.js:695:18)
at Object.realpathSync (fs.js:1283:25)
at tryFile (module.js:142:15)
at Function.Module._findPath (module.js:181:18)
at Function.Module._resolveFilename (module.js:336:25)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
令人沮喪。但是,如果我卸載sudo npm -g uninstall grunt-cli
,然後通過完全切換到root再試一次,它的工作原理:
$ sudo su -
root# npm -g install grunt-cli
/usr/local/bin/grunt -> /usr/local/lib/node_modules/grunt-cli/bin/grunt
[email protected] /usr/local/lib/node_modules/grunt-cli
├── [email protected]
├── [email protected] ([email protected])
└── [email protected] ([email protected], [email protected])
^D
$ grunt
grunt-cli: The grunt command line interface. (v0.1.13)
這裏面有全球所有包(咕嚕,監事等),是非常令人沮喪。爲什麼sudo
失敗,但su
工作?我能做些什麼來修復我的環境?
有趣。我的umask是'0027'。根umask是'0022'。通過'sudo'安裝: 根#ls -l命令在/ usr/local/bin目錄/咕嚕 lrwxr-XR-X 1根輪 並通過'su': $ ls -l命令在/ usr/local/bin目錄/ grunt lrwxr-x --- 1個根輪 所以我猜是因爲我不是輪組的成員,我無法運行腳本?解決這個問題最安全的方法是什麼? – Ryan
@Ryan:我認爲你把它們列在後面 - 用'sudo'它不是世界可讀的。所以你需要做一些像'(umask 022; sudo npm ...)',用不同的umask在子shell中運行npm。或者也許npm有一些配置選項來更改umask。 –
謝謝@chris,我確實讓他們落後了。顯然,NPM有一個umask配置選項,但我無法讓它堅持下來,所以我把我的umask改爲'022',這就解決了問題。 – Ryan