Can we import all node.js
modules in webpack and create a bundle.js
? Example what if I use http
module and webpack application and bundle it and run in browser?
我们可以在webpack中导入所有node.js模块并创建一个bundle.js吗?例如,如果我使用http模块和webpack应用程序并将其捆绑并在浏览器中运行,该怎么办?
main.js
var http = require('http');
var server = http.createServer(function (req, res) {
// send output
});
server.listen(8080);
1 个解决方案
#1
1
When using Node's built-in libraries with Webpack, it will automatically import a browser-compatible version when available.
将Node的内置库与Webpack一起使用时,它会在可用时自动导入与浏览器兼容的版本。
You can see the entire list and matching packages in this file.
您可以在此文件中查看整个列表和匹配的包。
For http
, you'll end up with http-browserify
instead. Not everything is supported, so creating a HTTP server will not work (as this isn't possible in a browser). You can still use http
to do requests as shown in the documentation.
对于http,您最终会使用http-browserify。并非一切都受支持,因此创建HTTP服务器将无法工作(因为这在浏览器中是不可能的)。您仍然可以使用http来执行请求,如文档中所示。
#1
1
When using Node's built-in libraries with Webpack, it will automatically import a browser-compatible version when available.
将Node的内置库与Webpack一起使用时,它会在可用时自动导入与浏览器兼容的版本。
You can see the entire list and matching packages in this file.
您可以在此文件中查看整个列表和匹配的包。
For http
, you'll end up with http-browserify
instead. Not everything is supported, so creating a HTTP server will not work (as this isn't possible in a browser). You can still use http
to do requests as shown in the documentation.
对于http,您最终会使用http-browserify。并非一切都受支持,因此创建HTTP服务器将无法工作(因为这在浏览器中是不可能的)。您仍然可以使用http来执行请求,如文档中所示。