Webpack/Node.js Http模块:http.createServer不是函数
问题描述:
我在尝试使用webpack的第一步时偶然发现了这个错误。Webpack/Node.js Http模块:http.createServer不是函数
只是为了再现以非常基本的水平的影响,我设置此微文件夹是这样的:
节点试验-2
- main.js
- 的package.json
- webpack.config.js
具有以下内容:
的package.json
{
"name": "node-test-2",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack && node bundle.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^2.2.0"
}
}
webpack.config.js
var path = require('path');
module.exports = {
entry : './main.js',
output : {
path : path.resolve(__dirname),
filename : 'bundle.js'
}
}
main.js
var http = require('http');
console.log("Creating Server");
var server = http.createServer(function(req, res){
console.log('Connection Estabilished!');
res.write('HELLO!');
res.end();
});
console.log("Listening on port " + 8000);
server.listen(8000);
现在,如果我只是node main.js
一切按预期工作。相反,如果我npm start
,因此提示webpack捆绑bundles.js需要的所有东西,然后运行它,运行时会显示错误http.createServer is not a function
错误。
进一步检查显示该函数似乎根本没有在bundle.js文件中声明。
我在这里错过了什么?这是什么webpack实际上不是为了?
更多,也许毫无意义,资讯:
- 运行在Windows 10
- 测试与节点版本6.9和7.10
- 当时既的WebPack和的WebPack @进行beta测试写
答
默认情况下,Webpack以浏览器环境为目标,对此http.createServer()
没有多大意义。
您可以通过添加更改目标以下到您的WebPack配置:
entry : './main.js',
target : 'node',
...