【问题标题】:Getting this error when using Babel and Webpack on React DnD project在 React DnD 项目上使用 Babel 和 Webpack 时出现此错误
【发布时间】:2017-09-22 09:51:48
【问题描述】:

这是问题所在,当使用 plain ReactJS(没有 React DnD)时,Babel 和 Webpack 编译我的 .js 文件完美无缺,但 尝试使用在我的项目中 React DnD,在使用 Webpack 和 Babel 编译 js 时发生了这个错误

ERROR in ./~/disposables/modules/index.js
Module build failed: ReferenceError: [BABEL] D:\MyProject\React_002\node_modules\disposables\modules\index.js: Using removed Babel 5 option: D:\MyProject\React_002\node_modules\disposables\.babelrc.stage - Check out the corresponding stage-x presets http://babeljs.io/docs/plugins/#presets
    at Logger.error (D:\MyProject\React_002\node_modules\babel-core\lib\transformation\file\logger.js:41:11)
    at OptionManager.mergeOptions (D:\MyProject\React_002\node_modules\babel-core\lib\transformation\file\options\option-manager.js:220:20)
    at OptionManager.init (D:\MyProject\React_002\node_modules\babel-core\lib\transformation\file\options\option-manager.js:368:12)
    at File.initOptions (D:\MyProject\React_002\node_modules\babel-core\lib\transformation\file\index.js:212:65)
    at new File (D:\MyProject\React_002\node_modules\babel-core\lib\transformation\file\index.js:135:24)
    at Pipeline.transform (D:\MyProject\React_002\node_modules\babel-core\lib\transformation\pipeline.js:46:16)
    at transpile (D:\MyProject\React_002\node_modules\babel-loader\lib\index.js:48:20)
    at Object.module.exports (D:\MyProject\React_002\node_modules\babel-loader\lib\index.js:163:20)
 @ ./~/react-dnd/lib/decorateHandler.js 41:19-41
 @ ./~/react-dnd/lib/DragSource.js
 @ ./~/react-dnd/lib/index.js
 @ ./src/js/Container.js
 @ ./src/js/script.js

这是我的 webpack.config.js 文件

var path = require('path');

module.exports = {
  entry: './src/js/script.js',
  output: {
    path: path.join(__dirname, 'dist/js'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: '/node_modules/'
      }
    ]
  }
};

这是我的 .babelrc 文件

{
  "presets" : ["es2015", "react"]
}

这是我的 package.json 文件

{
  "name": "React_002",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "babel": "babel",
    "webpack": "webpack",
    "build": "rimraf dist && webpack --watch",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.5.4",
    "react-dnd": "^2.3.0",
    "react-dnd-html5-backend": "^2.3.0",
    "react-dom": "^15.5.4"
  },
  "devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "rimraf": "^2.6.1",
    "webpack": "^2.4.1"
  }
}

如何解决这个问题?以及造成此问题的原因是什么? 谢谢

【问题讨论】:

    标签: reactjs webpack babeljs webpack-2 react-dnd


    【解决方案1】:

    您实际上并未将node_modules 从规则中排除。您传入了一个与绝对路径/node_modules/ 对应的字符串,即文件系统的根目录/ 中的node_modules 目录。它应该是一个正则表达式,/regex/ 是正则表达式文字语法,但是如果在它周围加上引号,它就会变成一个字符串(类似于在数组文字周围加上引号会发生的情况)。另见MDN - Regular Expressions

    你的规则应该是:

    {
      test: /\.jsx?$/,
      loader: 'babel-loader',
      exclude: /node_modules/
    }
    

    【讨论】:

    • 非常感谢,我遗漏了一些小细节。
    猜你喜欢
    • 2018-03-24
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2022-01-24
    • 2018-07-04
    • 1970-01-01
    • 2020-04-06
    • 2017-12-26
    相关资源
    最近更新 更多