8

我首先要说我几乎没有在终端或 node.js 工作的经验。

同事去度假了,我正在尝试按照他留下的说明在我们的演示服务器上设置他的应用程序。我可以让所有东西在本地运行,但是在安装 socket.io 模块的服务器上遇到了问题。

安装了python,安装了nodejs,都成功了。但随后我发出命令:

npm install -g socket.io

我得到的输出是:

npm http GET https://registry.npmjs.org/socket.io
npm http 304 https://registry.npmjs.org/socket.io
npm ERR! Error: EACCES, mkdir '/usr/local/lib/node_modules/socket.io'
npm ERR!  { [Error: EACCES, mkdir '/usr/local/lib/node_modules/socket.io']
npm ERR!   errno: 3,
npm ERR!   code: 'EACCES',
npm ERR!   path: '/usr/local/lib/node_modules/socket.io',
npm ERR!   fstream_type: 'Directory',
npm ERR!   fstream_path: '/usr/local/lib/node_modules/socket.io',
npm ERR!   fstream_class: 'DirWriter',
npm ERR!   fstream_stack:
npm ERR!    [ 'DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)',
npm ERR!      '/usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53',
npm ERR!      'Object.oncomplete (fs.js:297:15)' ] }
npm ERR!
npm ERR! Please try running this command again as root/Administrator.

npm ERR! System Linux 2.6.32-279.el6.x86_64
npm ERR! command "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "socket.io"
npm ERR! cwd /home/qa
npm ERR! node -v v0.8.16
npm ERR! npm -v 1.1.69
npm ERR! path /usr/local/lib/node_modules/socket.io
npm ERR! fstream_path /usr/local/lib/node_modules/socket.io
npm ERR! fstream_type Directory
npm ERR! fstream_class DirWriter
npm ERR! code EACCES
npm ERR! errno 3
npm ERR! stack Error: EACCES, mkdir '/usr/local/lib/node_modules/socket.io'
npm ERR! fstream_stack DirWriter._create (/usr/local/lib/node_modules/npm/node_modules/fstream/lib/dir-writer.js:36:23)
npm ERR! fstream_stack /usr/local/lib/node_modules/npm/node_modules/mkdirp/index.js:37:53
npm ERR! fstream_stack Object.oncomplete (fs.js:297:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /home/qa/npm-debug.log
npm ERR! not ok code 0

我使用的帐户应该具有管理员权限。谷歌搜索我发现了一些建议,但似乎没有一个对我有用。尝试使用 sudo 前置命令,但得到:

[qa@umr-demo ~]sudo npm install -g socket.io
[sudo] password for qa:
sudo: npm: command not found
4

2 回答 2

20

-g如果没有特权,我也会有同样的错误,所以在使用标志时我必须使用 sudo

如果 sudo 无法识别 npm,您可以尝试:

  • 通过npm的完整路线

    $ sudo $(which npm) install -g socket.io
    
  • preserving the environment with -E flag

    $ sudo -E npm install -g socket.io
    

Upd:

Note that is recommended to use the -g flag only for executables and install locally (without the flag) the libraries that are required in your code. Privileges are required for copying the executables to /usr/bin or, in your case /usr/local/bin

Note too that in the socket.io site, the -g flag is not included for the installation command ;)

Read more: http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

于 2012-12-20T02:18:34.143 回答
0

If you are on Arch Linux, check out the npm2arch package in the AUR. It will let you install npm packages using pacman.

Example:

npm2archinstall socket.io
于 2014-03-01T09:54:54.910 回答