【问题标题】:ERROR in Cannot find module 'babel-core'. using react.js, webpack, and express server找不到模块“babel-core”中的错误。使用 react.js、webpack 和 express 服务器
【发布时间】:2016-05-14 22:00:59
【问题描述】:

每当我在终端运行webpack 时,我都会得到:

Hash: efea76b1048c3a97b963
Version: webpack 1.12.13
Time: 33ms
    + 1 hidden modules

ERROR in Cannot find module 'babel-core'

这是我的 webpack.config.js 文件

module.exports = {
  entry: './app-client.js',
  output: {
    filename: 'public/bundle.js'
  },
  module: {
    loaders: [
      {
        exclude: /(node_modules|app-server.js)/,
        loader: 'babel'
      }
    ]
  }
}

package.json

{
  "name": "react",
  "version": "1.0.0",
  "description": "React polling app",
  "main": "app-client.js",
  "dependencies": {
    "babel-loader": "^6.2.2",
    "bootstrap": "^3.3.6",
    "express": "^4.13.4",
    "react": "^0.14.7"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

【问题讨论】:

    标签: javascript express reactjs webpack babeljs


    【解决方案1】:

    你应该在 npm install 时安装 babel-loader 和 babel-core 作为 dev-dependency。

    npm install babel-core babel-loader --save-dev
    

    【讨论】:

    • 工作就像一个魅力!请记住将其添加到您的packag.json 以避免再次遇到此问题。运行带有标志-Snpm install 将完成这项工作。
    • 我仍然收到:Error: Cannot find module '@babel/core' babel-loader@8 requires Babel 7.x (the package '@babel/core'). If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'
    • 看来 loader v8 需要 core v7,但 core v7 目前只有一个beta 版本,这似乎是维护者的疏忽。我手动为babel-loader 指定了7.1.5(最新版本现已发布),然后我开始运行
    【解决方案2】:

    对于那些想要使用 babel-loader 8+ 的人:它需要 Babel 7.x,它将作为 '@babel/core' 包而不是 'babel-core' 安装。 换句话说,运行:

    npm install --save-dev @babel/core
    

    【讨论】:

    • 那我猜你没有使用 babel-loader 8+。我为碰巧正在升级/开始一个新项目但不知道为什么由于包重命名而无法正常工作的人回答了这个问题。当它明确说明它适用于哪个版本时,无需投票。
    【解决方案3】:

    我刚遇到这个错误,通过安装 babel-core 解决了。 但有趣的是我发现 babel-core 确实存在于 babel-loader 的 peerDependencies 中。

    https://github.com/babel/babel-loader/blob/master/package.json

    为什么 peerDependecies 不能自动安装,经过几次搜索,我在 npm 博客中找到了this

    peerDependencies 将不再自动安装。

    【讨论】:

      【解决方案4】:

      添加到@Chetan 在此线程上的答案:

      我今天在阅读 Axel Rauschmayer 博士的书 here 时遇到了这个问题。每本书,babel-loader 也应该下载babel-core。然而,当我尝试它时,情况并非如此。我认为这与@theJian 的回答有关。

      由于原始 package.json 已将 babel-loader 列为依赖项,因此运行以下命令可解决该错误。

      npm install babel-core --save-dev
      

      【讨论】:

        【解决方案5】:
        npm install babel-register
        

        这可以解决您的问题。另外,添加 babelrc .babelrc { “预设”:[“es2015”,“反应”] }

        【讨论】:

        • 为什么?你能解释一下为什么会这样吗?
        猜你喜欢
        • 2017-02-20
        • 1970-01-01
        • 2020-12-17
        • 2017-06-02
        • 2017-10-13
        • 2019-09-08
        • 1970-01-01
        • 2019-02-03
        • 2016-10-01
        相关资源
        最近更新 更多