如何删除本地 npm 安装的 sudo 要求?

How to remove sudo requirement for local npm install?

我最近将 npm 版本更新为 2.5.1,当我在 node_modules 下的项目中 express locally 使用 npm install express --save 它出现了这个错误:

npm WARN locking Error: EACCES, open '/Users/patrick/.npm/_locks/express-44e9a2fe3cb5b729.lock'
npm WARN locking     at Error (native)
npm WARN locking  /Users/patrick/.npm/_locks/express-44e9a2fe3cb5b729.lock failed { [Error: EACCES, open '/Users/patrick/.npm/_locks/express-44e9a2fe3cb5b729.lock']
npm WARN locking   errno: -13,
npm WARN locking   code: 'EACCES',
npm WARN locking   path: '/Users/patrick/.npm/_locks/express-44e9a2fe3cb5b729.lock' }
npm ERR! Darwin 13.4.0
npm ERR! argv "node" "/usr/local/bin/npm" "install" "express" "--save"
npm ERR! node v0.12.0
npm ERR! npm  v2.5.1

npm ERR! Attempt to unlock (path to project here)/node_modules/express, which hasn't been locked

当我用 sudo 再次 运行 时它工作正常,但现在我的文本编辑器要求我每次覆盖项目文件夹中的文件时都输入密码。为什么会发生这种情况,我该如何解决?

注意,我不仅想防止本地安装需要 sudo,而且我还想解锁项目文件夹,这样我就不必一直在文本编辑器中输入密码。

我进入~/.npm和运行sudo rm -R ./*删除了缓存,然后我通过运行修改了项目文件夹的权限回到我的用户cd (path to project)sudo chmod -R patrick ./*

正在做 sudo npm is not recommended by the npm creator Isaac Schlueter:

I strongly encourage you not to do package management with sudo! Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it’s fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there.

如果权限和所有权设置正确,您永远不需要它:

chown -R $USER <directory>

即使之后,如果您再次 运行 sudo npm,每个创建的子目录都将归超级用户所有,当您尝试以普通用户身份访问它们时,您将遇到错误。

所以抵制sudo诱惑:)