2017-05-11 106 views
1

我在尝试使用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测试写

回答