"npm run build" = "react-scripts: 权限被拒绝"

36

我正在尝试在Ubuntu 18.04上部署我的工作中的Windows 10 Spring-Boot/React应用程序,但无论如何都会收到"react-scripts: Permission denied"错误,尽管我尝试了许多方法来解决。希望你们中的某个React专家能够找到我哪里出了问题。

我的package.json文件看起来像这样:

{
  "name": "medaverter-front",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "axios": "^0.19.2",
    "bootstrap": "^4.4.1",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.0",
    "react-table-6": "^6.11.0",
    "react-validation": "^3.0.7",
    "reactstrap": "^6.5.0",
    "validator": "^12.2.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

我以root身份登录,并使用nvm安装了node和lts。我是这样安装nvm的:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.0/install.sh | bash

然后执行了这个操作:

nvm install node
nvm use node
nvm install --lts
nvm use --lts

然后我通过 cd 命令切换到目录 /var/lib/jenkins/workspace/MedAverter/medaverter-front 并按照如下方式安装 node_modules:

npm install -g

然后递归地将权限更改为777,像这样:

chmod -R 777 node_modules

我还递归地将所有/root/.nvm的权限更改为777,就像这样:

chmod -R 777 /root/.nvm

我可以使用以下命令构建

npm run build
但是我在Jenkins上运行“立即构建”时却遇到了相同的失败情况。
[[1;34mINFO[m] Running 'npm run build' in /var/lib/jenkins/workspace/MedAverter/medaverter-front
[[1;34mINFO[m] [[1;34mINFO[m] > medaverter-front@0.1.0 build /var/lib/jenkins/workspace/MedAverter/medaverter-front
[[1;34mINFO[m] > react-scripts build [[1;34mINFO[m] 
[[1;31mERROR[m] sh: 1: **react-scripts: Permission denied**
[[1;31mERROR[m] npm ERR! code ELIFECYCLE
[[1;31mERROR[m] npm ERR! errno 126
[[1;31mERROR[m] npm ERR! medaverter-front@0.1.0 build: `react-scripts build` 
[[1;31mERROR[m] npm ERR! Exit status 126
然后我使用命令cd进入/var/lib/jenkins/workspace/MedAverter/medaverter-front目录并运行。
npm run build

同时再次出现相同的错误:

> root@ubuntu-s-1vcpu-1gb-nyc1-01:/var/lib/jenkins/workspace/MedAverter/medaverter-front#
> npm run build
> 
> > medaverter-front@0.1.0 build /var/lib/jenkins/workspace/MedAverter/medaverter-front
> > react-scripts build
> 
> sh: 1: **react-scripts: Permission denied** npm ERR! code ELIFECYCLE
> npm ERR! errno 126 npm ERR! medaverter-front@0.1.0 build:
> `react-scripts build` npm ERR! Exit status 126
我已经花了好几天时间试图弄清楚这个问题。有什么建议吗?

检查你使用的node在Ubuntu用户权限。 - midugh
你为什么要在全局安装后更改node_modules这个非全局目录的权限呢?你尝试过去掉-g吗?另外,你的chmod操作是不必要的,而且实际上非常危险。请不要这样做。通常情况下,Permission denied意味着可执行标志未设置,或者它被分配给了另一个用户,并且位没有设置为除用户/组之外的任何人。但是,你安装模块的方式非常奇怪。 - Qix - MONICA WAS MISTREATED
是的,我最初尝试了没有-g选项,但无法使其工作。后来添加了-g选项并尝试了不同的解决方案。我知道最终的解决方案不应涉及设置777权限,但我只是想先让某些东西工作起来,然后一旦真正的解决方案被揭示出来,再回到正确的方式。你知道那是什么吗? - user3217883
当我使用sudo安装npm和nvm时,我遇到了类似的问题。卸载后,以常规用户权限重新安装解决了我的问题。 - pritam
12个回答

101

解决方案 1:

我认为你已经全局安装了react-script。因此,请尝试使用以下命令

npm install react-scripts --save

解决方案2:

尝试运行以下命令,然后再次运行应用程序。

sudo chmod +x node_modules/.bin/react-scripts

然后再次运行该应用程序。

解决方法3:

我认为你的npm没有权限。你可以尝试使用sudo运行。

sudo npm run build

你可以这样解决这个问题:

步骤1:

检查使用 npm 的路径,方法如下:

which npm

你需要使用 "/usr/local/bin/npm" 这种路径类型。

如果你正在使用 yarn,请检查 yarn 的路径。

which yarn

你需要使用"/usr/local/bin/npm"这种路径类型

步骤2:

给予此路径权限777并尝试运行项目

sudo chmod -R 777 /usr/local/bin/npm

我在通过npm运行节点脚本时遇到了同样的问题,解决方案2对我有用。谢谢! - bildungsroman
解决方案2对我来说就是最好的选择。 - fabpico

27

macOS的解决方案

从您的项目根目录运行以下命令:

chmod +x node_modules/.bin/react-scripts

8
以下方法对我有效:
rm -rf node_modules 

npm install // or yarn install

npm start // or yarn start 


7

package.json 文件中默认的启动命令 "start": "react-scripts start" 改为以下配置,然后再试一下:"scripts": { "start": "node ./node_modules/react-scripts/bin/react-scripts.js start" }


尝试过了,但是出现了相同的错误。我需要在修改后关闭 PuTTY 会话并启动新的会话吗? - user3217883

5

我之前修改了文件夹的权限,现在已经恢复到原来的设置了。我认为问题应该已经解决了。

我刚刚删除了node_modules文件夹,并使用yarn命令重新安装了依赖项。

sudo rm -rf node_modules
yarn
yarn start

5

在我的情况下,它是在运行npm时出现的问题。以下是我解决的方法:

  1. 删除 /node_modules 文件夹
  2. 运行 npm i 命令
  3. 运行 npm run android 命令

1
在终端上运行第一步: sudo chmod +x node_modules/.bin/react-scripts 然后运行第二步的命令: npm start

0

我遇到了同样的问题,我通过以下方式解决了它:在 package.json 中指定版本并运行 npm i -g react-scripts

"react-scripts": "5.0.0"

原始代码是:"react-scripts": "5.0.1",我将其更改为:"react-scripts": "5.0.0"

然后再次运行 npm i.

如果问题仍未解决,请检查您所使用的 React 当前版本中的 react-scripts 版本。


0
这是因为缺少 package.json 文件导致的。 只需运行此命令即可解决问题:
npm audit fix

0
有时候npm需要root权限。尝试在命令中使用sudo。这对我解决了类似的问题。这可能会有所帮助。
sudo npm run build

在所有地方都使用“sudo”是安全的吗? - nima

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接