Webpack 节点全局模块 [英] Webpack node global modules

查看:43
本文介绍了Webpack 节点全局模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在浏览器中配置 webpack.config.js 以支持全局节点模块?

How to configure webpack.config.js to global node modules support in browser?

我试过了:

{
    global : true,
    process: true,
},

发生构建错误:

找不到模块fs".

{
    fs: 'empty',
}

我尝试在 js 文件中调用 fs.existsSync(path).在浏览器中发生错误:

I tried call fs.existsSync(path) in js file. In browser error occured:

fs.existsSync 不是函数.

推荐答案

你应该尝试将 webpack 的目标设置为 node,如下所示:

you should try and set the target for webpack to node, with something like this:

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

这篇文章对我帮助很大:http://jlong​​ster.com/Backend-Apps-with-Webpack--Part-I

This post helped me alot: http://jlongster.com/Backend-Apps-with-Webpack--Part-I

target: 'node' 选项告诉 webpack 不要接触任何内置的fs 或 path 等模块.

The target: 'node' option tells webpack not to touch any built-in modules like fs or path.

这篇关于Webpack 节点全局模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆