简体   繁体   English

错误:在将Webpack与Express一起使用时无法解析模块“ Json-loader”

[英]Error: Cannot resolve module 'Json-loader' whilst using webpack with express

I am trying to use webpack with express but have encountered some errors. 我正在尝试将Webpack与Express一起使用,但是遇到了一些错误。 This is the error I am currently getting: Error: Cannot resolve module 'json-loader' 这是我当前遇到的Error: Cannot resolve module 'json-loader'

package.json: (json loader in both, as is webpack and config etc) package.json :(两个json加载器,webpack和config等)

"devDependencies": {
    "json-loader": "^0.5.4",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.15.1"
  },
  "dependencies": {
    "express": "^4.14.0",
    "firebase": "^3.3.0",
    "json-loader": "^0.5.4",
    "webpack-config": "^6.1.2"
  }

webpack.config.js: webpack.config.js:

module.exports = {
  entry: "./app.js",
  target: 'node',
  output: {
    path: __dirname,
    filename: "bundle.js"
  },
    module: {
      loaders: [
        { test: /\.json$/, loader: 'json-loader'},
      ]
    }

}

As can be seen above, I have json loader there and have entry pointing to my app.js that looks like so: 从上面可以看到,我在那里有json加载器,并且有指向我的app.js的条目,如下所示:

var express = require('express');
var app = express();
var firebase = require('firebase');

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});

app.get('/welcome', function (req, res) {
  res.sendFile(__dirname + '/welcome.html');
});

What am i doing wrong? 我究竟做错了什么? ie how do I get rid of the error and make express work with webpack? 即我如何摆脱错误并使webpack快速工作?

I was facing a similar problem, and this is how I fixed it. 我遇到了类似的问题,这就是我解决的方法。 Note that this solution might not fix @Waterman1's issue, unless my guess that we're not seeing the entire webpack.config.js is correct. 请注意,除非我猜测我们没有看到整个webpack.config.js是正确的,否则此解决方案可能无法解决@ Waterman1的问题。

(1) (1)
install / add to package.json json-loader 安装/添加到package.json json-loader

(2) (2)
edit webpackage.json.js changing (this may vary slightly for you) 编辑webpackage.json.js更改(这可能会稍微有所不同)

module: {
  preLoaders: [
  {
    test: /\.js|\.jsx$/,
    loaders: ['eslint-loader'],
    exclude: ['node_modules']
  }
  ],
  loaders: [
  {
    test: /\.js|\.jsx$/,
    loaders: ['babel'],
    include: path.join(__dirname, 'src')
  }
  ]
}

to

module: {
  preLoaders: [
  {
    test: /\.js|\.jsx$/,
    loaders: ['eslint-loader'],
    exclude: ['node_modules',/\.json$/]
  }
  ],
  loaders: [
  {
    test: /\.json$/,
    loader: "json-loader"
  },
  {
    test: /\.js|\.jsx$/,
    loaders: ['babel'],
    include: path.join(__dirname, 'src')
  }
  ]
}

In particularly noting the changes: 特别要注意的变化:

  • adding loader-json first to loaders 首先将loader-json添加到loaders
  • adding /\\.json$/ to exclude in the preLoaders 添加/\\.json$/excludepreLoaders

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

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