尝试安装 gatsby 时出现安装错误

时间:2021-03-21 07:47:15

标签: node.js npm gatsby

尝试安装 gatsby 时,我得到以下信息:

npm WARN deprecated @hapi/hoek@8.5.1: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/joi@15.1.1: joi is leaving the @hapi organization and moving back to 'joi' (https://github.com/sideway/joi/issues/2411)
npm WARN deprecated @hapi/bourne@1.3.2: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/address@2.1.4: This version has been deprecated and is no longer supported or maintained
npm WARN deprecated @hapi/topo@3.1.6: This version has been deprecated and is no longer supported or maintained
npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm WARN express-graphql@0.9.0 requires a peer of graphql@^14.4.1 but none is installed. You must install peer dependencies yourself.

npm ERR! code EACCES
npm ERR! syscall access
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, access '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'access',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }
npm ERR! 
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR! 
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:

我安装了最新版本的节点。我试过看教程,但找不到任何东西来解决这个问题。我该如何解决?

1 个答案:

答案 0 :(得分:0)

您对安装所在的文件夹没有权限。您可以将自己添加到文件夹的权限中,它应该会运行。

您可以通过运行以下命令查看谁拥有错误消息中所述的“/usr/local/lib/node_modules”文件夹的权限:

ls -la /usr/local/lib/node_modules

在这里你可能会看到类似的东西:

drwxr-xr-x   3 root    wheel   96 Mar 23 13:57 .
drwxrwxr-x  24 someusr admin  768 Mar 23 16:12 ..
drwxr-xr-x  24 root    wheel  768 Feb 23 01:21 npm

我们看到,并非该文件夹中的所有内容都归“someusr”所有。

要解决这个问题,您可以为自己设置该文件夹中内容的权限。您可以使用带有 -R 标志的 chown 命令递归设置该文件夹的权限,并结合环境变量 $USER,它将输出当前用户(在本例中为“someusr”):

sudo chown -R $USER /usr/local/lib/node_modules

然后再次运行安装命令:

npm install -g gatsby-cli 

祝你好运!