【问题标题】:Not able to run my React App, Getting a few errors无法运行我的 React 应用程序,出现一些错误
【发布时间】:2020-02-24 15:16:14
【问题描述】:

Github 代码链接:https://github.com/bhatvikrant/IndecisionApp

我已经运行了 npm i,然后完成了 yarn run dev-server,我使用了 webpack。 我在 MacOs 机器上。 我也制作了 .babelrc 文件。

运行yarn run dev-server后出现的错误:

VIKRANTs-MacBook-Pro:IndecisionAPP VIKRANT$ yarn run dev-server
yarn run v1.19.1
$ webpack-dev-server
Project is running at http://localhost:8080/
webpack output is served from /
Content not from webpack is served from /Users/VIKRANT/Desktop/IndecisionAPP/public
Hash: 35d055107a6a0062196d
Version: webpack 3.1.0
Time: 1305ms
    Asset    Size  Chunks                    Chunk Names
bundle.js  867 kB       0  [emitted]  [big]  main
  [35] multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js 40 bytes {0} [built]
  [36] (webpack)-dev-server/client?http://localhost:8080 5.59 kB {0} [built]
  [37] ./node_modules/url/url.js 23.3 kB {0} [built]
  [38] ./node_modules/node-libs-browser/node_modules/punycode/punycode.js 14.7 kB {0} [built]
  [39] ./node_modules/url/util.js 314 bytes {0} [built]
  [40] ./node_modules/querystring-es3/index.js 127 bytes {0} [built]
  [43] ./node_modules/strip-ansi/index.js 161 bytes {0} [built]
  [44] ./node_modules/ansi-regex/index.js 135 bytes {0} [built]
  [45] (webpack)-dev-server/client/socket.js 856 bytes {0} [built]
  [46] ./node_modules/sockjs-client/lib/entry.js 244 bytes {0} [built]
  [77] (webpack)-dev-server/client/overlay.js 3.6 kB {0} [built]
  [78] ./node_modules/ansi-html/index.js 4.26 kB {0} [built]
  [79] ./node_modules/html-entities/index.js 231 bytes {0} [built]
  [82] (webpack)/hot/emitter.js 77 bytes {0} [built]
  [84] ./src/app.js 2.33 kB {0} [built] [failed] [1 error]
    + 70 hidden modules

ERROR in ./src/app.js
Module build failed: Error: Requires Babel "^7.0.0-0", but was loaded with "6.25.0". If you are sure you have a
 compatible version of @babel/core, it is likely that something in your build process is loading the wrong vers
ion. Inspect the stack trace of this error to look for the first entry that doesn't mention "@babel/core" or "b
abel-core" to see what is calling Babel. (While processing preset: "/Users/VIKRANT/Desktop/IndecisionAPP/node_m
odules/@babel/preset-env/lib/index.js")
    at throwVersionError (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/@babel/helper-plugin-utils/lib/inde
x.js:65:11)
    at Object.assertVersion (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/@babel/helper-plugin-utils/lib/i
ndex.js:13:11)
    at /Users/VIKRANT/Desktop/IndecisionAPP/node_modules/@babel/preset-env/lib/index.js:177:7
    at /Users/VIKRANT/Desktop/IndecisionAPP/node_modules/@babel/helper-plugin-utils/lib/index.js:19:12
    at /Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/options/option-mana
ger.js:317:46
    at Array.map (<anonymous>)
    at OptionManager.resolvePresets (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
    at OptionManager.mergePresets (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
    at OptionManager.mergeOptions (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
    at OptionManager.init (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
    at File.initOptions (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/index.js:212:65)
    at new File (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/file/index.js:135:24)
    at Pipeline.transform (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
    at transpile (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-loader/lib/index.js:49:20)
    at Object.module.exports (/Users/VIKRANT/Desktop/IndecisionAPP/node_modules/babel-loader/lib/index.js:174:20)
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js
webpack: Failed to compile.


请帮我纠正这个语法错误,我已经花了将近一天的时间来解决这个问题。

【问题讨论】:

  • Requires Babel "^7.0.0-0" 更改 babel 版本并使用 instal npm 命令重新安装 npm。
  • @AyushiKeshri 你能指定命令吗,真的很有帮助,谢谢
  • Yarn 不是“更好的做法”。
  • @VikrantBhat 下面的用户 Jim G. 仔细检查了下面的 package.json 修改。看起来它应该可以解决您的问题。基本上你的开发版本和依赖版本不匹配因此错误。
  • 真的推荐你read and learn 包/依赖是如何工作的。

标签: javascript reactjs npm webpack babeljs


【解决方案1】:

模块构建失败:错误:需要 Babel "^7.0.0-0",但已加载 与“6.25.0”。

出现此错误是因为需要 Babel 7 及更高版本。你可以使用 babel-upgrade 包升级它:https://www.npmjs.com/package/babel-upgrade

你可以在这里找到升级 babel 的正确方法:How to upgrade to babel 7

【讨论】:

  • 您确定这是问题所在吗?因为我尝试将 babel-cli 升级到 7.0.0,但我猜它不存在
  • 它为我修复了一次与 babel 相关的问题。我觉得值得一试,你可以克隆 repo 并测试一下。
【解决方案2】:

我认为你在课程中的某些步骤做错了,here 是你可能应该得到的 package.json。

我建议你尝试删除 node_modules、yarn.lock 和 package-lock.json 然后将以下内容放入你的 package.json 中

{
  "name": "indecision-app",
  "version": "1.0.0",
  "main": "index.js",
  "author": "Andrew Mead",
  "license": "MIT",
  "scripts": {
    "serve": "live-server public/",
    "build": "webpack",
    "dev-server": "webpack-dev-server"
  },
  "dependencies": {
    "babel-cli": "6.24.1",
    "babel-core": "6.25.0",
    "babel-loader": "7.1.1",
    "babel-plugin-transform-class-properties": "6.24.1",
    "babel-preset-env": "1.5.2",
    "babel-preset-react": "6.24.1",
    "css-loader": "0.28.4",
    "live-server": "^1.2.0",
    "node-sass": "4.5.3",
    "normalize.css": "7.0.0",
    "react": "15.6.1",
    "react-dom": "15.6.1",
    "react-modal": "2.2.2",
    "sass-loader": "6.0.6",
    "style-loader": "0.18.2",
    "validator": "8.0.0",
    "webpack": "3.1.0",
    "webpack-dev-server": "2.5.1"
  }
}

然后运行npm installyarn install

【讨论】:

  • 试过这个@HMR得到错误:在node-sass@4.5.3 postinstall脚本失败。
  • @VikrantBhat 我遇到了同样的错误,只是通过在 package.json 中将其更改为 "node-sass": "*", 解决了它
【解决方案3】:

首先,install and run ’rimraf’


那么,来自here

我们需要添加一些 babel 包到我们的项目中

npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node.

这些分别负责 babel 的一般工作,babel 在命令行中的使用,使用最新 JS 功能的能力以及 babel 与 node 的使用。


或者,您可以修改您的“package.json”文件(如下所示)并重新运行“yarn”:

{
  "name": "indecision-app",
  "version": "1.0.0",
  "main": "index.js",
  "author": "Andrew Mead",
  "license": "MIT",
  "scripts": {
    "serve": "live-server public/",
    "build": "webpack",
    "dev-server": "webpack-dev-server"
  },
  "dependencies": {
    "live-server": "^1.2.0",
    "node-sass": "4.5.3",
    "normalize.css": "7.0.0",
    "react": "15.6.1",
    "react-dom": "15.6.1",
    "react-modal": "2.2.2",
    "sass-loader": "6.0.6",
    "style-loader": "0.18.2",
    "validator": "8.0.0",
    "webpack": "3.1.0",
    "webpack-dev-server": "2.5.1"
  },
  "devDependencies": {
    "@babel/core": "^7.6.4",
    "@babel/preset-env": "^7.6.3",
    "@babel/preset-react": "^7.6.3",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-class-properties": "7.0.0", // upgrade this to 7.0.
    "css-loader": "0.28.4"
  }
}

【讨论】:

  • 不需要 devDependencies 中的任何 babel 包(至少不需要从 create-react-app 弹出的应用程序的 package.json 中)。
  • @Jim G. 我可以在编辑 package.json 后使用 npm install
  • 我没有得到 babel-cli@7.0.0 的匹配版本。
  • 使用 npm i babel-cli (虽然 v7 处于 beta 模式)自己去从 npm 包中搜索一次并尝试阅读官方文档。 npmjs.com/package/babel-cli
猜你喜欢
  • 2022-09-29
  • 1970-01-01
  • 2018-06-17
  • 2019-03-17
  • 2019-10-13
  • 2020-06-05
  • 1970-01-01
  • 2022-01-09
  • 2020-09-05
相关资源
最近更新 更多