Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-react'

守給你的承諾、 提交于 2019-11-27 15:39:32

in your webpack config did you already try @babel/preset-react instead of just react?

Btw. you test for /\.js$/ Better test for /\.jsx?$/ (x? means x is optional), because you import a .jsx file in your index.js

Not

options: {
    presets: ['react']
}

but

options: {
    presets: ['@babel/preset-react']
}

place .babelrc file at root dir with this inside

{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}

and remove preset from babel-loader webpack cfg

options: {
  presets: ['react']
}

I did it at the end with this settings, (becouse es2015 has been changed https://www.npmjs.com/package/babel-preset-es2015) now!

And presets in package.json or .babelrc or babel.config ::

use: {
                  loader: 'babel-loader',
                  options: {
                      presets: ['@babel/react', '@babel/preset-env'],
                          plugins: ['@babel/plugin-proposal-class-properties']
                  }
              }

and..

"devDependencies": {
    "@babel/cli": "^7.1.5",
    "@babel/core": "^7.1.6",
    "@babel/plugin-proposal-class-properties": "^7.1.0",
    "@babel/plugin-transform-react-constant-elements": "^7.0.0",
    "@babel/plugin-transform-react-inline-elements": "^7.0.0",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "^7.0.0",
    "@babel/runtime": "^7.1.5",
    "babel-eslint": "^8.2.6",
    "babel-loader": "^8.0.4",
    "babel-node": "^6.5.3",
    "babel-plugin-react-transform": "^3.0.0",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "babel-plugin-transform-react-pure-class-to-function": "^1.0.1",
    "babel-plugin-transform-react-remove-prop-types": "^0.4.20",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.7.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "webpack": "^4.26.1"
  }

and the code that you call to preset example:

   "theme:build": "babel theme/src -d theme/dist --presets @babel/react --plugins transform-class-properties --quiet && npm run webpack:store",

presets @babel/react

in github very much forum about the subject!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!