node.js - 在 Ubuntu 18.04 上安装 ReactJS——npm 权限被拒绝

标签 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

最佳答案

使用 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 Abramov (@create-react-app 的合著者):

npm install -g create-react-app

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

Everything above runs just fine until the last command. Then I get the following error, regardless of working directory.

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

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

sudo npm install -g create-react-app

不用sudo安装,请引用这个问题:npm throws error without sudo

关于node.js - 在 Ubuntu 18.04 上安装 ReactJS——npm 权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56917216/

相关文章:

linux - 如何创建特定用途的命令 shell

node.js - APM 在公司代理后面不起作用

javascript - 使用任何 lodash 获取嵌套 JSON 对象的所有值

html - 解开 HTML 字符串中不必要的嵌套 div (NodeJs)

javascript - 无参数 props 方法不会从 react 中的子组件中调用

css - ReactJS 为样式选择所有相似的标签

json - React fetch post 请求中的空属性和值

javascript - 将对象推送到数组,该数组是数组 Mongoose 中对象的字段

linux - awk 输出到变量

java - Linux 上的 JDK 1.8 缺少 JNI 包含文件