安装 npm 模块时权限被拒绝

Permission denied when installing npm module

我在尝试安装 npm 模块时遇到奇怪的权限错误。我从 sudo 开始,所以我确定我可以访问,但由于某种原因它一直抱怨错误:

stack Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/joplin/node_modules/sqlite3/build'

我已经尝试重新启动我的计算机,并使用 chmod 777 创建一个目录 /usr/lib/node_modules/joplin,但它仍然不起作用。

$ sudo npm install -g joplin
/usr/bin/joplin -> /usr/lib/node_modules/joplin/main.js

> sqlite3@3.1.9 install /usr/lib/node_modules/joplin/node_modules/sqlite3
> node-pre-gyp install --fallback-to-build

node-pre-gyp ERR! Tried to download(undefined): https://mapbox-node-binary.s3.amazonaws.com/sqlite3/v3.1.9/node-v48-linux-x64.tar.gz
node-pre-gyp ERR! Pre-built binaries not found for sqlite3@3.1.9 and node@6.11.2 (node-v48 ABI) (falling back to source compile with node-gyp)
gyp ERR! configure error
gyp ERR! stack Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/joplin/node_modules/sqlite3/build'
gyp ERR! stack     at Error (native)
gyp ERR! System Linux 4.4.0-43-Microsoft
gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node" "--module_name=node_sqlite3" "--module_path=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64"
gyp ERR! cwd /usr/lib/node_modules/joplin/node_modules/sqlite3
gyp ERR! node -v v6.11.2
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/bin/nodejs /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64/node_sqlite3.node --module_name=node_sqlite3 --module_path=/usr/lib/node_modules/joplin/node_modules/sqlite3/lib/binding/node-v48-linux-x64' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/usr/lib/node_modules/joplin/node_modules/sqlite3/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at emitTwo (events.js:106:13)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:191:7)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:891:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
node-pre-gyp ERR! System Linux 4.4.0-43-Microsoft
node-pre-gyp ERR! command "/usr/bin/nodejs" "/usr/lib/node_modules/joplin/node_modules/sqlite3/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/lib/node_modules/joplin/node_modules/sqlite3
node-pre-gyp ERR! node -v v6.11.2
node-pre-gyp ERR! node-pre-gyp -v v0.6.36
node-pre-gyp ERR! not ok

编辑

我也试过 sudo npm i sqlite3 -g --build-from-source 但得到了同样的错误 Error: EACCES: permission denied, mkdir '/usr/lib/node_modules/sqlite3/build' 所以这似乎与 sqlite3 有关。

您需要收回 .npm 目录的所有权。

sudo chown -R $(whoami) ~/.npm

并且需要 node_modules 目录的写入权限:

sudo chown -R $(whoami) /usr/lib/node_modules

授予对节点模块文件夹的完全权限

命令

sudo chmod 777 -R node_modules/

根据我的经验,永远不要 运行 npm install 使用 sudo。这会创建以 root 为所有者而不是您的用户名的项目。

最好将目录的所有权更改为您的用户名。 $(whoami)