Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't resolve 'global/window' (webpack) #21

Open
fregante opened this issue Jun 3, 2021 · 2 comments
Open

Can't resolve 'global/window' (webpack) #21

fregante opened this issue Jun 3, 2021 · 2 comments

Comments

@fregante
Copy link

fregante commented Jun 3, 2021

ERROR in ./node_modules/my-module/index.js 2:28-34
Module not found: Error: Can't resolve 'global/window' in './node_modules/my-module'
Did you mean 'window.js'?
BREAKING CHANGE: The request 'global/window' failed to resolve only because it was resolved as fully specified
(probably because the origin is a '*.mjs' file or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

This appears to be because global/window is not a valid identifier in ES modules so every single example on the web fails:

    new webpack.ProvidePlugin({
      window: "global/window",
      document: "global/document",
    }),

Thankfully in this case the solution was:

    new webpack.ProvidePlugin({
      window: "global/window.js",
      document: "global/document.js",
    }),

This might be fixable with export maps in package.json, but I'm not entirely sure.

fregante added a commit to pixiebrix/pixiebrix-extension that referenced this issue Jun 3, 2021
@Raynos
Copy link
Owner

Raynos commented Jun 4, 2021

This module is commonJS and not ESM. This sounds like a bug in webpack.

I would not recommend using webpack configuration to expose window & document as globals.

I've used this module in the past to explicitly import document & window as packages instead of global variables.

fregante added a commit to fregante/webext-detect that referenced this issue Jun 4, 2021
@fregante
Copy link
Author

fregante commented Jun 4, 2021

Technically it's not anyone's bug. Webpack is doing what my code asks, it's just that the provided URI happens to be incompatible with ESM.

A solution and ideal upgrade would be to offer global as an ES module as well as CJS, especially because more and more packages will begin being published as ES modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants