Support ES module import in browser
See original GitHub issueHi again,
I tested the library with angular and it is running into an error:
universalModuleDefinition:1 Uncaught ReferenceError: global is not defined
at universalModuleDefinition:1:1
at Object.9424 (universalModuleDefinition:1:1)
at __webpack_require__ (bootstrap:19:1)
at Module.1896 (home.component.html:14:263)
at __webpack_require__ (bootstrap:19:1)
at Module.721 (universalModuleDefinition:1:1)
at __webpack_require__ (bootstrap:19:1)
at Module.23 (app.component.html:6:8)
at __webpack_require__ (bootstrap:19:1)
at Module.8835 (environment.ts:16:71)
Which is expected as the angular team came to the conclusion to not support global in their webpack build for broad compatibility. https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814
I think this library should not rely on some other process to supply the variable. So going forward you could use:
- globalThis: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis
- polyfill it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#search_for_the_global_across_environments
Workaround is adding (window as any).global = window;
to polyfill.ts
Issue Analytics
- State:
- Created a year ago
- Comments:64 (33 by maintainers)
Top Results From Across the Web
JavaScript modules - MDN Web Docs
Use of native JavaScript modules is dependent on the import and export statements; these are supported in browsers as shown in the ...
Read more >JavaScript modules via script tag | Can I use... Support tables ...
Loading JavaScript module scripts (aka ES6 modules) using <script type="module"> Includes support for the nomodule attribute. Usage % of. all users, all tracked ......
Read more >Using ES modules in browsers with import-maps
ES modules have wide browser support currently. Some of the features around ES modules such as dynamic import (allowing function based imports) ...
Read more >Super Simple Start to ESModules in the Browser
All major browsers support ESModules now. Here's how to get started using them.
Read more >ES modules in service workers - web.dev
Static imports only # ... ES modules can be imported in one of two ways: either statically, using the import ... from '...'...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Elringus @Aloento do you know why we sometimes get the
bodyJs is not defined
while we do not get this using CRA?The “problem” with web modules (ES) is that they are incompatible with older package runtimes as they add the
export
keyword which may not be supported.So, I support @Aloento view of generating an ES module first and then using rollup to create UMD/CommonJs, because generating them from ES modules is easy whereas generating ES modules from UMD is not feasible.
But in the end this is way more work than any of us should invest for this problem. The great thing is I found this PR: https://github.com/umdjs/umd/issues/124 Which suggests the second edit:
Making it work in angular, and at least loading as web module:
But running into an error: