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

时间:2018-09-27 16:37:59

标签: javascript node.js reactjs webpack babel

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

./src/index.js中的

错误 模块构建失败(来自./node_modules/babel-loader/lib/index.js): 错误:无法从'/ Users / me / Desktop / reflask'中找到模块'babel-preset-react' -如果您要解析“反应”,请使用“模块:反应” -您的意思是“ @ babel /反应”吗?     在Function.module.exports [作为同步](/Users/me/Desktop/reflask/node_modules/resolve/lib/sync.js:43:15)     在resolveStandardizedName(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:101:31)     在resolvePreset(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:58:10)     在loadPreset(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/files/plugins.js:77:20)     在createDescriptor(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:154:9)     在items.map(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:109:50)     在Array.map()     在createDescriptors(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:109:29)     在createPresetDescriptors(/Users/me/Desktop/reflask/node_modules/@babel/core/lib/config/config-descriptors.js:101:10)     在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"]
}

5 个答案:

答案 0 :(得分:14)

在您的webpack配置中,您是否已经尝试过$filename = WWW_ROOT.'ufiles/medium/'.$img['image_1']; $cFile = curl_file_create($filename); $request = [ '_operation'=>'uploadFile', 'id'=>$ticket_id, '_session'=>$session, 'file' => $cFile ]; try{ $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_URL,$this->crm['endpoint']); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $request); $response=curl_exec($ch); curl_close ($ch); }catch(\Exception $e){ $response = null; } 而不只是// Initialize Button popover, whose contents comes from a DIV $('#delegatorInfoButton').popover({ trigger: 'manual', html: true, title: '<span style="font-weight: bold;">TITLE</span>', content: function() { return $('#delegatorInfoButtonPanel').html(); } }); // Manual handling of Button popover: dismiss on (1) Outside click as well as (2) next Button click $('#delegatorInfoButton').click(function() { $(this).popover('toggle'); }).blur(function() { $(this).popover('hide'); });

顺便说一句。您测试@babel/preset-reactreact进行更好的测试(x?表示x是可选的),因为您在index.js中导入了.jsx文件

不是

/\.js$/

但是

/\.jsx?$/

答案 1 :(得分:2)

将.babelrc文件放在根目录中

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

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

options: {
  presets: ['react']
}

答案 2 :(得分:1)

我最终使用此设置完成了此操作(因为es2015已更改,https://www.npmjs.com/package/babel-preset-es2015) 现在!

package.json .babelrc babel.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 /反应

在github上有关该主题的论坛很多!

答案 3 :(得分:0)

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

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

npm start

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

y

答案 4 :(得分:0)

它对我有用,我不得不将 .bablerc 中的 ['react'] 更改为 ['@babel/preset-react']
如果您要将 babel 从 6.x 升级到 7.x

enter image description here