在react-create-app中babel加载器出现问题

35

问题

create-react-app 初始化的仓库中,安装的 babel-loader 版本比所需版本旧。

日志:

There might be a problem with the project dependency tree.
It is likely not a bug in Create React App,
but something you need to fix locally.

The react-scripts package provided by Create React App requires a dependency:

  "babel-loader": "8.0.4"

Don't try to install it manually: your package manager does it automatically.
However, a different version of babel-loader was detected higher up in the tree:

  C:\Users\user\node_modules\babel-loader (version: 8.0.2)

Manually installing incompatible versions is known to cause hard-to-debug issues.

If prefer to ignore this check, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
That will permanently disable this message but you might encounter other issues.
    To fix the dependency tree, try following the steps below in the exact order:

      1. Delete package-lock.json (not package.json!) and/or yarn.lock in your project folder.
      2. Delete node_modules in your project folder.
      3. Remove "babel-loader" from dependencies and/or devDependencies in the package.json
    file in your project folder.
      4. Run npm install or yarn, depending on the package manager you use.

    In most cases, this should be enough to fix
    the problem.
    If this has not helped, there are a few other things you can try:

      5. If you used npm, install yarn (http://yarnpkg.com/) and repeat the above steps with it instead.
         This may help because npm has known issues with package hoisting which may get resolved in future versions.

      6. Check if C:\Users\User\node_modules\babel-loader is outside your project directory.
         For example, you might have accidentally installed something in your home folder.

      7. Try running npm ls babel-loader in your project folder.
         This will tell you which other package
    (apart from the expected react-scripts) installed babel-loader.

    If nothing else helps, add SKIP_PREFLIGHT_CHECK=true to an .env file in your project.
    That would permanently disable this preflight check in case you want to proceed anyway.

    P.S. We know this message is long but please read the steps above :-) We hope you find them helpful!

    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! reg@0.1.0 start: `react-scripts start`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the reg@0.1.0 start script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

    npm ERR! A complete log of this run can be found in:
    npm ERR!     C:\Users\User\AppData\Roaming\npm-cache\_logs\2018-12-13T18_22_47_802Z-debug.log

发生了什么之后

$ npx create-react-app reg && cd reg && npm start

我尝试过的方法

我尝试删除node_modules文件夹和package-lock.json文件,然后运行npm install

还安装了yarn并运行了yarn && yarn start

我已经重新创建了应用程序3次,并获得了相同的结果。

可能有用的信息

Node版本: 10.8.0
NPM版本: 6.5.0
create-react-app / react-scripts: 2.1.1
babel-loader: 8.0.4
全局软件包列表 (npm ls --depth=0 -g):

+-- create-react-app@2.1.1
+-- node-gyp@3.6.2
+-- npm@6.5.0
+-- yarn@1.12.3

package.json 文件:

{
  "name": "reg",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.6.3",
    "react-dom": "^16.6.3",
    "react-scripts": "2.1.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

错误信息本身中有一系列修复指令,你尝试过它们了吗? - Agney
7
好的,我会尽力做到最好。请看“我尝试过的”部分。 - Paul Losev
第六步还是第七步? - Agney
@BoyWithSilverWings 从1到7 - Paul Losev
15个回答

41

我遇到了同样的问题,但是经过一个小时的努力终于找到了解决方案。 当我们运行 npm start 时,你会看到这种错误,是版本相关的问题。 前往 node_modules 文件夹:

Project->node_modules->react-scripts->package.json

检查 package.json 文件

在那里你会找到:"babel-loader": "8.0.4" 首先删除:Project->node_modules->babel-loader 文件夹 然后运行 npm i babel-loader@8.0.4(它取决于你的版本,你可以像这样更改它:npm i babel-loader@8.0.5),之后可能会出现 webpack 问题,然后按照同样的方式从 "Project->node_modules->webpack" 中移除 webpack, 然后重新安装npm i webpack@4.19.1

4.19.1 *webpack 版本号可能会有所更改。


5
两年后,即使我的问题相反:babel-loader和webpack版本过新而不是过旧,但这仍然是最佳解决方案。然而,明确安装所需的版本使得npm start可以工作。 - SilverSideDown
你好,来自2021年的感谢。 - codenamezero
1
我就是不明白为什么package-lock不能解决这个问题。 - Oliver Watkins

22
在您的项目目录中创建一个名为.env的文件,并在文件中添加SKIP_PREFLIGHT_CHECK=true

1
已经为我解决了。问题是...为什么会发生这种情况?这个解决方案到底是做什么的? - Idan Levi
10
不是解决版本冲突的最佳选择。我需要它不仅能够运行,还需要它正常工作。 - Paul Losev
为什么这个有效?什么是PREFLIGHT_CHECK? - Oliver Watkins

10

在Mac上:

/Users/<你的用户名>/node_modules中移除冲突的包。


可以在Linux上运行,只是与某些软件包冲突了...谢谢 +rep - w411 3
很酷,它对我来说简单易用且运行良好。谢谢。 - Wisnu
1
你如何识别冲突的软件包?比如,你是删除“更高级”的那个还是第一个被提到的那个? - chris Frisina

4
然而,树上方检测到了不同版本的babel-loader:
您应该删除在树上方检测到的babel-loader包。输出显示有一个带有babel-loader的node_modules文件夹。
C:\ Users \ user \ node_modules \ babel-loader(版本:8.0.2)
除非您在/ Users / {user}内有一个根项目,否则您也可以完全删除C:\ Users \ user \ node_modules目录。

1
几年后,同样的问题。
对我来说,解决方案在我的 c:/users/myName/node_modules 中。 完全删除那个 node_modules 然后再试一次。

0

您可以在package.json文件中暂时将react-scripts降级到1.1.5,然后删除node_modules文件夹,并在使用NPM的情况下删除package-lock.json文件,在使用Yarn的情况下删除yarn.lock文件。之后,重新安装依赖项并使用yarn startnpm run start命令启动开发服务器。


0

这意味着您需要安装两个babel-loader

rm yarn.lock
run npm uninstall babel-loader@8.0.4

0

我遇到了同样的问题。 我通过删除用户/文件夹中的node_modules节点和package-lock.json文件(如果存在)来解决它。 然后它应该完美地工作。


0
我曾经遇到过完全相同的问题。对于任何通过Google找到这个帖子并像我一样是初学者的人,以下是在终端(Mac用户)中输入的确切内容。
假设你正在遇到与Paul相同的错误:
"The react-scripts package provided by Create React App requires a dependency: "babel-loader": "8.0.6"
以下是我在终端中输入的内容。我可能错了,但这对我有效(我也是初学者):
步骤1:删除node_modules文件夹以及package_lock.json文件。
步骤2:在终端中,键入cd-(我们只是离开项目文件夹并进入您的主文件夹)
步骤3:在终端中键入npm i“dependency-name@number”,因此对于上面的示例,它将是:npm i babel-loader@8.0.6
步骤4:现在在终端中,导航到您的项目文件夹并键入:npm install或yarn install
步骤5:尝试运行 npm start,如果所有问题都已解决,则应该启动开发服务器。
注意:我不得不为 babel-loader@8.0.6Webpack 重复执行上述过程。

cd - doesn't go into your home folder. You probably meant cd ~ - Joel Peltonen

0
 C:\Users\user\node_modules\babel-loader

从 \user\node_modules 中删除 node_modules 不要从您的项目中删除

如果您正在使用后端(nodejs),请检查您是否在后端中使用了 webpack 确保您的 create-react-app webpack 和后端 webpack 版本相同


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