简体   繁体   English

Webpack节点全局模块

[英]Webpack node global modules

How to configure webpack.config.js to global node modules support in browser? 如何将webpack.config.js配置为浏览器中的全局节点模块支持?

I tried: 我试过了:

{
    global : true,
    process: true,
},

On build error occured: 生成时发生错误:

can't find module 'fs'. 找不到模块“ fs”。

{
    fs: 'empty',
}

I tried call fs.existsSync(path) in js file. 我尝试在js文件中调用fs.existsSync(path) In browser error occured: 在浏览器中发生错误:

fs.existsSync is not a function. fs.existsSync不是函数。

you should try and set the target for webpack to node, with something like this: 您应该尝试将webpack的目标设置为node,如下所示:

module.exports = {
 devtool: "source-map",
 target: "node",
 ...
}

This post helped me alot: http://jlongster.com/Backend-Apps-with-Webpack--Part-I 这篇文章对我有很大帮助: http : //jlong​​ster.com/Backend-Apps-with-Webpack--Part-I

The target: 'node' option tells webpack not to touch any built-in modules like fs or path. target:'node'选项告诉webpack不要触摸任何内置模块,例如fs或path。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM