模块构建失败(来自./node_modules/babel-loader/lib/index.js):错误:找不到模块'babel-preset-react'

Posted

技术标签:

【中文标题】模块构建失败(来自./node_modules/babel-loader/lib/index.js):错误:找不到模块\'babel-preset-react\'【英文标题】:Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: Cannot find module 'babel-preset-react'模块构建失败(来自./node_modules/babel-loader/lib/index.js):错误:找不到模块'babel-preset-react' 【发布时间】:2019-03-03 15:19:29 【问题描述】:

我在尝试使用 Webpack4 和 Babel7 构建 React 应用时遇到以下错误。

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-react' from '/Users/me/Desktop/reflask'
- If you want to resolve "react", use "module:react"
- Did you mean "@babel/react"?
    at Function.module.exports [as sync] (/Users/me/Desktop/reflask/node_modules/resolve/lib/sync.js:43:15)
    at resolveStandardizedName (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:101:31)
    at resolvePreset (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:58:10)
    at loadPreset (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:77:20)
    at createDescriptor (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:154:9)
    at items.map (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
    at Array.map (<anonymous>)
    at createDescriptors (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
    at createPresetDescriptors (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
    at passPerPreset (/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:58:96)
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src main[1]

我已尝试删除 node_modules 文件夹并使用以下内容重新安装依赖项。

终端

rm -rf node_modules/
npm install

配置

package.json


  "name": "reflask",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": 
    "start": "webpack-dev-server --open --mode development",
    "build": "webpack"
  ,
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": 
    "@babel/core": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.2",
    "html-loader": "^0.5.5",
    "html-webpack-plugin": "^3.2.0",
    "prop-types": "^15.6.2",
    "webpack": "^4.19.1",
    "webpack-cli": "^3.1.1",
    "webpack-dev-server": "^3.1.9"
  ,
  "dependencies": 
    "bootstrap": "^4.1.3",
    "react": "^16.5.2",
    "react-bootstrap": "^0.32.4",
    "react-dom": "^16.5.2",
    "react-router-dom": "^4.3.1"
  

webpack.config.js

const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = 
    module: 
        rules: [
            
                test: /\.js$/,
                exclude: /node_modules/,
                use: 
                    loader: "babel-loader",
                    options: 
                        presets: ['react']
                    
                
            ,
            
                test: /\.html$/,
                use: [
                    
                        loader: "html-loader"
                    
                ]
            
        ]
    ,
    plugins: [
        new HtmlWebPackPlugin(
            template: "./src/index.html",
            filename: "./index.html"
        )
    ]
;

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import App from './js/components/App.jsx';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

.babelrc


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

【问题讨论】:

【参考方案1】:

如果您要将 babel 从 6.x 升级到 7.x,它对我有用,我必须在 .bablerc 中进行更改:


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

【讨论】:

【参考方案2】:

它对我有用,我必须在 .bablerc 中将 ['react'] 更改为 ['@babel/preset-react'] 如果你正在将 babel 从 6.x 升级到 7.x

【讨论】:

从文件中发布实际代码而不是屏幕截图会更有帮助。【参考方案3】:

我之前也遇到过同样的错误,我只是在另一个端口上运行应用程序。

为此,请在您的终端中运行此命令

npm start

然后它会询问你是否想在另一个端口写上运行应用程序

y

【讨论】:

【参考方案4】:

我最后用这个设置做了,(因为es2015已经改了https://www.npmjs.com/package/babel-preset-es2015) 现在!

以及 package.json.babelrcbabel.config ::

中的预设
use: 
                  loader: 'babel-loader',
                  options: 
                      presets: ['@babel/react', '@babel/preset-env'],
                          plugins: ['@babel/plugin-proposal-class-properties']
                  
              

和..

"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"
  

以及您调用预设示例的代码:

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

预设@babel/react

在 github 上关于这个主题的论坛非常多!

【讨论】:

由于旧的 repo 没有很好地更新 babel 依赖项和 webpack 4 中的设置,一直出现此错误。第一部分确实对我有用,如上所述更改了我的 webpack【参考方案5】:

在您的 webpack 配置中,您是否已经尝试过 @babel/preset-react 而不仅仅是 react

顺便说一句。你测试/\.js$/ 更好地测试/\.jsx?$/(x? 表示 x 是可选的),因为您在 index.js 中导入了一个 .jsx 文件

没有

options: 
    presets: ['react']

但是

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

【讨论】:

【参考方案6】:

将 .babelrc 文件放在根目录下


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

并从 babel-loader webpack cfg 中删除预设

options: 
  presets: ['react']

【讨论】:

对不起,我应该提到 .babelrc 文件已经在根目录中,并且包含您上面的内容。我还添加了 presets: ['react'] 部分,因为我在 ./src/js/components/App.jsx 中收到错误 'Module parse failed: Unexpected token';引用该文件中的一些 html。 webpack 预设选项是 babelrc ***.com/questions/48476373/…的替代品 正如 zebnat 所说,webpack 预设选项是 babelrc 的完美替代品。任何有助于开发人员整合配置文件的事情都不应该被劝阻。

以上是关于模块构建失败(来自./node_modules/babel-loader/lib/index.js):错误:找不到模块'babel-preset-react'的主要内容,如果未能解决你的问题,请参考以下文章

错误:模块构建失败(来自 ./node_modules/mini-css-extract-plugin/dist/loader.js):

模块构建失败(来自 ./node_modules/sass-loader/dist/cjs.js)

如何修复“模块构建失败(来自 ./node_modules/postcss-loader/src/index.js)”

Rails Tailwind 设置:模块构建失败(来自 ./node_modules/postcss-loader/src/index.js):

模块构建失败(来自 ./node_modules/babel-loader/lib/index.js)Vue Js

带有 babel 的 webpack 显示错误模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):