在Ubuntu 18.04上安装ReactJS-拒绝npm权限

时间:2019-07-06 19:51:26

标签: node.js linux reactjs ubuntu npm

我是Linux的新手,我正在尝试使用nodejs进行设置,但是我无法弄清楚为什么我不能安装react。我正在按照网上看到的指示进行操作,但是我想知道为什么我似乎没有安装react所需的特权。这是一个个人环境,所以我没想到会有任何问题。以下是我所运行的内容以及最后一行给我的最终错误。

sudo apt update && sudo apt upgrade
sudo apt install nodejs
sudo npm install npm

node -v
v8.10.0

npm -v
3.5.2

sudo npm install npm@latest -g
npm -v
6.10.0

npm install -g create-react-app

上面的所有操作在最后一个命令之前都可以正常运行。不管工作目录是什么,我都会收到以下错误消息。

npm WARN checkPermissions Missing write access to /usr/local/lib/node_modules
npm ERR! path /usr/local/lib/node_modules
npm ERR! code EACCES
npm ERR! errno -13
npm ERR! syscall access
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!   stack: '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! 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 (though this is not recommended).

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/me/.npm/_logs/2019-07-06T19_33_32_971Z-debug.log

2 个答案:

答案 0 :(得分:0)

我也遇到了这个问题,这是怎么解决的

确保已安装或使用 npm:

sudo apt install npm

然后

sudo npm -g install create-react-app

为了创建应用程序,我没有在命令行之前使用“npm”,而是使用这个:

create-react-app nameofyourapp

这对我有用。

答案 1 :(得分:-1)

使用npx(npm 5.2及更高版本)

如果您使用的是npm 5.2+,则可以创建如下的ReactJS应用程序:

npx create-react-app my-app

现在,您的应用程序位于文件夹my-app中,您可以cd进入该文件夹并运行npm start

npm install上使用旧版npm

您也可以使用npm进行全局安装,对于较早的npm版本(<5.2)实际上是有效的(the official documentation for create-react-app链接到instructions by Dan Abramovcreate-react-app的共同作者)):< / p>

npm install -g create-react-app

但是,您可能需要sudo来进行全局-g的安装或解决方案

  

上面的所有操作在最后一个命令之前都可以正常运行。不管工作目录是什么,我都会收到以下错误消息。

这是因为您的最后一个命令是全局安装(-g)。

除非有反对的理由,否则您可以使用sudo安装它:

sudo npm install -g create-react-app

要不使用sudo进行安装,请参考以下问题:npm throws error without sudo

相关问题