node.js - Webpack 失败,babel 已移至 babel-core

标签 node.js reactjs webpack

我遵循了不同开发人员发布的 stackoverflow 帖子上的解决方案。但是,它们都没有帮助解决我遇到的问题。我做的几件事是

  1. 卸载babel
  2. 安装babel-core、babel-cli
  3. 升级到 Node v6.2.2
  4. 重新安装 webpack
  5. 炸毁 node_modules 并再次运行 npm install

以上所有都没有修复这个错误

ERROR in The node API for `babel` has been moved to `babel-core`.
 @ (webpack)-dev-server/client?http://localhost:3333 1:10-24

ERROR in The node API for `babel` has been moved to `babel-core`.
 @ (webpack)-dev-server/client?http://localhost:3333 3:16-37

ERROR in The node API for `babel` has been moved to `babel-core`.
 @ (webpack)-dev-server/client?http://localhost:3333 2:13-37

ERROR in (webpack)/~/process/browser.js
Module build failed: Error: Couldn't find preset "react" relative to directory "/Users/admin/.nvm/versions/node/v6.0.0/lib/node_modules/webpack/node_modules/process"
    at /Users/admin/repos/ReactJSApps/react-es6-setup/node_modules/babel-core/lib/transformation/file/options/option-manager.js:395:17
    at Array.map (native)
.
.
.
webpack: bundle is now VALID.

这是我的 package.json

{
  "name": "react-es6-setup",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "react": "^15.2.0",
    "react-dom": "^15.2.0"
  },
  "devDependencies": {
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "babel-preset-react": "^6.11.1"
  }
}

和 webpack.config.js

module.exports = {
  entry: './main.js',
  output: {
    path: './',
    filename: 'index.js'
  },
  devServer: {
    inline: true,
    port: 3333
  },
  module: {
    loaders: [
        {
          test: /\.js$/,
          exclude: 'node_modules',
          loader: 'babel',
          query: {
            presets: [ 'es2015', 'react' ]
          }
        }
    ]
  }
}

和 node_modules 的目录列表

$ ls -l node_modules/ |grep react
drwxr-xr-x    6 admim  207825898    204 Jul  6 02:02 babel-helper-builder-react-jsx/
drwxr-xr-x    6 admim  207825898    204 Jul  6 02:02 babel-plugin-transform-react-display-name/
drwxr-xr-x    6 admim  207825898    204 Jul  6 02:02 babel-plugin-transform-react-jsx/
drwxr-xr-x    6 admim  207825898    204 Jul  6 02:02 babel-plugin-transform-react-jsx-self/
drwxr-xr-x    6 admim  207825898    204 Jul  6 02:02 babel-plugin-transform-react-jsx-source/
drwxr-xr-x    6 admim  207825898    204 Jul  6 02:02 babel-preset-react/
drwxr-xr-x    9 admim  207825898    306 Jul  6 02:03 react/
drwxr-xr-x    9 admim  207825898    306 Jul  6 02:03 react-dom/

最佳答案

webpack.config.js 中使用 loaders: 'babel-loader' 为我解决了这个问题-

使用 npm 安装这些依赖项

"babel-core": "^6.1.2",
"babel-loader": "^6.1.0",
"babel-plugin-transform-runtime": "^6.1.2",
"babel-preset-es2015": "^6.1.2",
"babel-preset-stage-0": "^6.1.2",
"babel-runtime": "^5.8.0",

webpack.config.js文件中,有这样的代码:

const path = require('path');
module.exports = {
  entry: './src/main.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'dist.js',
    publicPath: "js"
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
}

关于node.js - Webpack 失败,babel 已移至 babel-core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38227896/

相关文章:

json数据存入postgresql顺序不同

javascript - 为什么依赖 useEffect 是数组状态而不是无限循环

javascript - 我如何停止流路由器中的订阅

webpack - 将变量从 html-webpack-plugin 传递给 pug 模板

javascript - Node JS 多个并发请求到后端 API

c++ - 无法将 C++ 字符串 vector 转换为 V8 数组

javascript - NodeJS clearTimeout() 似乎无法正常工作?

javascript - 什么时候使用类组件什么时候使用函数式组件

javascript - 使用 Webpack Encore 在 Angular 数据表中找不到数据表

javascript - Nextjs 从父目录导入外部组件