node.js 无法解析“babel-loader”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44490481/
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-09-02 18:17:11  来源:igfitidea点击:

Can't resolve 'babel-loader'

node.jswebpackbabeljsnode-moduleswebpack-2

提问by Mel

I am trying to configure my first node react app.

我正在尝试配置我的第一个节点反应应用程序。

I keep getting an error that says "Can't resolve babel-loader".

我不断收到一条错误消息,提示“无法解析 babel-loader”。

Googling that error, I found a couple of suggestions which do not work.

谷歌搜索那个错误,我发现了一些不起作用的建议。

The first is to add the following to my webpack.config.js

首先是在我的webpack.config.js中添加以下内容

// resolveLoader: {
  //       modulesDirectories: '/usr/local/lib/node_modules'
  //   },

Trying that produces an error that says:

尝试产生一个错误,说:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.resolveLoader has an unknown property 'modulesDirectories'. These properties are valid:
   object { alias?, aliasFields?, cachePredicate?, descriptionFiles?, enforceExtension?, enforceModuleExtension?, extensions?, fileSystem?, mainFields?, mainFiles?, moduleExtensions?, modules?, plugins?, resolver?, symlinks?, unsafeCache?, useSyncFileSystemCalls? }

The next suggestion is to try:

下一个建议是尝试:

resolveLoader: {
        fallback: '/usr/local/lib/node_modules'
    },

That produces a similar error.

这会产生类似的错误。

Does anyone have any advice on how to get started with this configuration. Understanding the documentation is proving difficult - every second word is jargon and I can't find a reference point to find fundamental understanding of what needs to be done to get started in this setup.

有没有人对如何开始使用此配置有任何建议。事实证明,理解文档很困难——每一个单词都是行话,我找不到一个参考点来基本理解需要做什么才能开始这个设置。

Webpack.config.js is:

Webpack.config.js 是:

module.exports = {
  entry: './app/app.jsx',
  output: {
    path: __dirname,
    filename: './public/bundle.js'
  },

  resolve: {
    modules: [__dirname, 'node_modules'],
    alias: {
      Greeter: 'app/components/Greeter.jsx',
      GreeterMessage: 'app/components/GreeterMessage.jsx',
      GreeterForm: 'app/components/GreeterForm.jsx',

    },
    extensions: ['.js', '.jsx']
  },

  // resolveLoader: {
  //       fallback: '/usr/local/lib/node_modules'
  //   },

  module :{
    rules:[{
      // use : 'babel-loader',
      loader: 'babel-loader',
      query :{
        presets:['react','es2015']
        // ,'es2017'
      },
      test: /\.jsx?$/,
      exclude: /(node_modules|bower_components)/
    }
   ]
 }


};

回答by JESii

I had the same problem; I fixed it by installing the babel-loader module:

我有同样的问题; 我通过安装 babel-loader 模块修复了它:

yarn add babel-loader

yarn add babel-loader

or

或者

npm install babel-loader

npm install babel-loader