I am having problems building an express.js app using webpack. I suspect that I am not handling the node.js core module 'http' correctly.
To be more specific: My package.json looks like
{
"name": "basic-express-example",
"version": "1.0.0",
"description": "description here",
"main": "index.js",
"author": "",
"license": "ISC",
"scripts": {
"build": "webpack -d"
},
"dependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"express": "^4.14.1",
"webpack": "^2.2.1"
}
}
My webpack.config.js looks like
var webpack = require('webpack');
var path = require('path');
var BUILD_DIR = path.resolve(__dirname, 'build');
var APP_DIR = path.resolve(__dirname, 'app');
var config = {
entry: APP_DIR + '/index.jsx',
output: {
path: BUILD_DIR,
filename: 'index.js'
},
module : {
loaders : [
{
test : /\.jsx?/,
include : APP_DIR,
exclude : [
/node_modules/,
/build/
],
loader : 'babel-loader'
}
]
},
node: {
fs: 'empty',
net: 'empty'
}
};
module.exports = config;
The only file in my app folder is index.jsx :
import express from 'express';
const app = express();
Now, if I build the app using npm build
and then try to run the resulting file using node build/index.js
, I get a type error saying
undefined:31
__proto__: http.IncomingMessage.prototype
^
TypeError: Cannot read property 'prototype' of undefined
How do I solve this problem, ie how do I make the http module available?
Thanks for any help in advance!
I solved it using this link by not bundling the node modules by adding the following to the webpack config:
var fs = require('fs');
var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
...
externals: nodeModules,
...
I also tried to solve it by providing the http module externally and bundling index.jsx as a commonjs module that is then used in the same file which also provides the http import. Okay, that sounds a bit vague... Anyway, I ran into other runtime errors there...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.