Javascript 找不到模块“babel-core”中的错误。使用 react.js、webpack 和 express 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35215461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:25:02  来源:igfitidea点击:

ERROR in Cannot find module 'babel-core'. using react.js, webpack, and express server

javascriptexpressreactjswebpackbabeljs

提问by Richard Bustos

Whenever I run webpackin the terminal I get:

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

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

ERROR in Cannot find module 'babel-core'

Here is my webpack.config.jsfile

这是我的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

包.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"
}

回答by Chetan

You should install babel-loader and babel-core as dev-dependency while npm install.

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

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

回答by CounterFlame

For those wanting to use babel-loader 8+: it requires Babel 7.x,, which is to be installed as the '@babel/core' package instead of 'babel-core'. In other words, run:

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

npm install --save-dev @babel/core

回答by theJian

I just meet this error, and solved by installing babel-core. But the interesting is I found babel-core does exist in babel-loader's peerDependencies.

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

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

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

Why peerDependecies not install automatically, after a few searching work I found thisin npm blog.

为什么 peerDependcies 没有自动安装,经过一些搜索工作,我在 npm 博客中找到了这个

peerDependencies will not automatically install anymore.

peerDependencies 将不再自动安装。

回答by Bhanuprakash D

Adding to @Chetan's answer on this thread:

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

I ran into this issue today while following through Dr. Axel Rauschmayer's book here. Per book, babel-loadershould download babel-coreas well. However this is not the case when I tried it out. I think this relates to @theJian's answer.

我同时通过阿克塞尔Rauschmayer先生博士的书下面今天就遇到了这个问题在这里。每本书,也babel-loader应该下载babel-core。然而,当我尝试时,情况并非如此。我认为这与@theJian 的回答有关。

Since the original package.json already lists babel-loaderas dependency, running the following command resolved the error.

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

npm install babel-core --save-dev

回答by patilnitin

npm install babel-register

This can solve your issue. Additionally, add babelrc .babelrc { "presets" : ["es2015", "react"] }

这可以解决您的问题。此外,添加 babelrc .babelrc { "presets" : ["es2015", "react"] }