【问题标题】:Nodejs - Getting Error in babel loader when i initiate npm startNodejs - 当我启动 npm start 时,在 babel 加载器中出现错误
【发布时间】:2016-07-29 14:42:16
【问题描述】:

得到这个错误:

多主程序中的错误 找不到模块:错误:无法解析 C:\Users\username\Deskto 中的模块“反应” p\reactApp @ 多主

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

节点版本 - 4.4.7

npm 版本 - 3.10.5

Babel 全局加载 npm install -g babel npm install -g babel-preset-react

【问题讨论】:

  • 你是怎么做到的?

标签: node.js reactjs


【解决方案1】:

您可以尝试将 loader 更改为 'babel'

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

这就是我的设置方式,它对我有用。

编辑:

这是我完整的 webpack 设置:

module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname,
    publicPath: '/',
    filename: 'bundle.js'
  },
  module: {
    loaders: [{
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['react', 'es2015', 'stage-1']
      }
    },{
      include: /\.json$/,
      loaders: ["json-loader"]
    },{
      test: /(\.css)$/, loaders: ['style', 'css']
    }]
  },
  resolve: {
    extensions: ['', '.json', '.js', '.jsx']
  },
  devServer: {
    historyApiFallback: true,
    contentBase: './'
  }
};

具有以下与 babel 和 webpack 相关的依赖项(不是完整列表)

  "devDependencies": {
    "babel-core": "^6.2.1",
    "babel-loader": "^6.2.0",
    "babel-preset-es2015": "^6.1.18",
    "babel-preset-react": "^6.1.18",
    "webpack": "^1.12.9",
    "webpack-dev-server": "^1.14.0"
  },
  "dependencies": {
    "babel-preset-stage-1": "^6.1.18",
    "json-loader": "^0.5.4",
  }

【讨论】:

  • 是的。我按照你的代码,我又得到了错误。找不到多主模块中的错误:错误:无法解析 C:\Users\553150\Deskto p\reactApp @ multi main webpack 中的模块“babel”:捆绑现在有效。
  • 看看我的依赖和 webpack。此外,您应该使用 --save 或 --save-dev 安装依赖项,具体取决于它们是依赖项还是 devDependencies。您不需要全局安装它们。
猜你喜欢
  • 2018-02-24
  • 2020-12-21
  • 2015-08-19
  • 2021-12-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 2014-11-29
相关资源
最近更新 更多