简体   繁体   English

webpack配置不适用于node.js中的require()

[英]webpack config doesn't work with require() in node.js

I have a file call abc.jsx like this 我有一个像这样的文件调用abc.jsx

var MyTitle = require('./MyTitle')

but I have to do require('./MyTitle)

coz I run webpack it throw me error. 因为运行webpack会抛出错误。

ERROR in ./js/MyTitle.jsx
Module not found: Error: Cannot resolve 'file' or 'directory' ./MyTitle in /Users/username/Documents/intro-to-react/js
 @ ./js/MyTitle.jsx 5:14-34

You should have set webpack config to resolve jsx file using babel-loader. 您应该设置webpack config来使用babel-loader解析jsx文件。 The following line test: /\\.js?$/, in module loader tells webpack to resolve both .js and .jsx file types. 以下代码行test: /\\.js?$/,在模块加载器中告诉webpack解析.js.jsx文件类型。

var webpack = require('webpack');
var path = require('path');

module.exports = {
  context: path.join(__dirname, "src"),
  devtool: "inline-sourcemap",
  entry: "./js/client.js",
  module: {
    loaders: [
      {
        test: /\.js?$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        query: {
          presets: ['react', 'es2015', 'stage-0'],
          plugins: ['react-html-attrs', 'transform-decorators-legacy'], 
        }
      }
    ]
  },
  output: {
    path: __dirname + "/src/",
    filename: "client.min.js"
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM