繁体   English   中英

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

[英]Install ReactJS on Ubuntu 18.04 -- npm permission denied

我是 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

使用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) create-react-app的官方文档链接到Dan Abramov 的说明(@ create-react-app共同作者):

npm install -g create-react-app

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

上面的一切都运行得很好,直到最后一个命令。 然后,无论工作目录如何,我都会收到以下错误。

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

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

sudo npm install -g create-react-app

要不使用 sudo 安装,请参考这个问题: npm throws error without sudo

我也遇到了这个问题,解决方法如下

确保已安装 npm 或使用:

sudo apt install npm

然后

sudo npm -g install create-react-app

要创建应用程序,我没有在命令行之前使用“npm”,而是使用以下命令:

create-react-app nameofyourapp

那对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM