运行 webpack 后未定义“全局" [英] 'global' undefined after running webpack

查看:31
本文介绍了运行 webpack 后未定义“全局"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的包装器模块,它围绕由脚本运行的环境设置的全局对象.包装器模块只是做:

I have a very simple wrapper module around a global object set by the environment the scripts are run. The wrapper module simply does:

module.exports = global.foobar;

以前当我使用 browserify 这工作正常.在浏览器中,globalwindow 相同.

Previously when I used browserify this worked fine. When in the browser, global was the same as window.

然而,我正在切换到 webpack 并且在运行 webpack 之后 global 的含义发生了变化.在浏览器中,它不再是 window 的别名,而是 undefined,我得到 cannot read property foobar of undefined.

However, I'm switching to webpack and after running webpack the meaning of global has changed. In the browser it's no longer an alias to window, instead it's undefined, and I get cannot read property foobar of undefined.

现在,在我的包装器模块的情况下,我可以通过其他方式修复它,但我有其他依赖项,并且进一步向下使用 buffer 包.该包还使用 global(见这里) 并且在我运行 webpack 后也会崩溃:

Now, in the case of my wrapper module I can fix that in other ways, but I have other dependencies, and further down the chain the buffer package is used. That package also uses global (see here) and also crashes after I've run webpack:

Uncaught TypeError: Cannot read property 'TYPED_ARRAY_SUPPORT' of undefined

有什么办法可以让 webpack 像 browserify 一样处理 global,而 globalwindow 的别名?>

Is there some way I can make webpack treat global the same way browserify did, with global being an alias of window?

推荐答案

我终于找到了问题所在.我有两个 webpack 构建:第一个构建我的库,第二个构建演示.问题是两个配置都有:

I finally found the problem. I have two webpack builds: the first one building my library and the second building the demo. The problem was that both configs had:

{
  node: {
    global: true
  }
}

如果第一个(构建库的那个)有 global: false,第二个有 global: true,它工作正常.

It works fine if the first one (the one building the lib) has global: false, and the second one has global: true.

这篇关于运行 webpack 后未定义“全局"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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