Riv*_*ler 3 javascript node.js webpack
在尝试使用webpack的第一步时,我偶然发现了这个错误.
只是为了在一个非常基础的层面上重现效果,我设置了这样的微文件夹,如下所示:
节点试验-2
具有以下内容:
的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"
}
}
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
var path = require('path');
module.exports = {
entry : './main.js',
output : {
path : path.resolve(__dirname),
filename : 'bundle.js'
}
}
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
现在,如果我只是node main.js一切按预期工作.
相反,如果我npm start,因此提示webpack捆绑bundle.js中所需的所有内容然后运行它,运行时会出现http.createServer is not a function错误错误.
进一步检查表明该函数似乎根本没有在bundle.js文件中声明.
我在这里错过了什么?这是webpack实际上不适合的吗?
更多,也许毫无意义的信息:
rob*_*lep 10
默认情况下,Webpack以浏览器环境为目标,对此http.createServer()没有多大意义.
您可以通过在Webpack配置中添加以下内容来更改目标:
entry : './main.js',
target : 'node',
...
Run Code Online (Sandbox Code Playgroud)
https://webpack.js.org/configuration/target/
| 归档时间: |
|
| 查看次数: |
1755 次 |
| 最近记录: |