【问题标题】:webpack config doesn't work with require() in node.jswebpack 配置不适用于 node.js 中的 require()
【发布时间】:2016-09-27 18:45:04
【问题描述】:

我有一个像这样调用 abc.jsx 的文件

var MyTitle = require('./MyTitle')

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

因为我运行 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

【问题讨论】:

    标签: javascript node.js reactjs webpack


    【解决方案1】:

    你应该已经设置了 webpack 配置来使用 babel-loader 解析 jsx 文件。模块加载器中的以下行 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"
      }
    };
    

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 2017-03-03
      • 1970-01-01
      • 2017-11-06
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多