[WASM] Module not found: Error: Can't resolve

See original GitHub issue

🐞 bug report

Description

wasm-pack creates a bundle which ends up doing this: import * as wasm from './my_wasm' (my_wasm is in reality my_wasm.wasm, notice the absence of the .wasm extension.)

When I import the package in my angular project I get the following error during ng build: Module not found: Error: Can't resolve './my_wasm' in 'my_package'.

While it is supposed to work with Webpack. As far as I can tell one solution is to add resolve.extensions: ['.wasm'] to the webpack configuration, but Angular doesn’t expose that option.

🌍 Your Environment

Angular Version: 8.0.3

Anything else relevant?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
filipesilvacommented, Jul 15, 2019

I think you’re giving Webpack a bit too much importance in this topic. Webpack is very popular, but that doesn’t mean everyone uses Webpack.

For instance, a lot of toolchains use Rollup instead. The WASM rollup plugin shows an example of importing a .wasm file with the extension. Parcel is popular as well, and their WASM support also lists an example of using the extension. I couldn’t actually find a page in the Webpack docs showing how to use WASM, but there is an example in the repository that also shows imports with the extension. Aside from bundlers, Both browsers and NodeJS show examples of loading WASM files with an extension. So from this admittedly short survey of the package and runtime landscape, I saw none that directed users to import WASM without extensions.

If you have a library and you package it in a way that assumes Webpack, then you are limiting how consumers can use your library. The limitations are both that consumers must use webpack, and that webpack must be configured in a certain way to use the library. This is a serious limitation because it gets passed on transitively to both your direct and indirect consumers. A user might not use a library directly and still run into these problems because of an indirect dependency.

The specific case of changing the an extension resolver also has performance implications. That’s the list of extensions the resolver will try to load before it gives up on loading a file. So each extension added there increases total file system operations by a factor relative to its position in the priority array. In Webpack’s case, .wasm is the first one, followed by .mjs, then .js, then .json. If I ask Webpack to bundle a .js file without an extension, it will first try to load .wasm and .mjs. So for the case of .js files, this triples file reads for existing files. For nonexistent files, it adds 1/4 more reads. This cost is incurred for all resolutions, so a large project that uses a single library with a .wasm import must pay the cost on all attempted resolutions.

I’m sure Webpack has great reasons for listing .wasm on their default extension resolver. But like I said before, it was never a goal for Angular CLI to expose Webpack semantics. Another example of not exposing Webpack semantics is not using require in user code for .html and .css files, or not exposing Node globals. We avoid Webpack-specific semantics because it creates coupling of the CLI and user code to Webpack. If in the future we want to migrate to another setup, this couple would create problems. If a user wants to move away from the CLI to another bundler, the same coupling will also create problems for them.

If importing .wasm files without an extension was indeed a standard in platforms (like NodeJS or Chrome), it would make sense for Angular CLI to do it. If it was common practice among bundlers, it would make sense to consider the pros and cons and perhaps do it as well. But as far as I can tell it’s neither.

If you have a library that assumes the .wasm extension will automatically be attempted by the consumers toolchain, I urge you to adopt a more consumer friendly packaging approach. Coupling your library to the semantics of a specific tool (be it Webpack, or any other) is harmful for the JS ecosystem.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 9, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'env' Β· Issue #1907
I have this issue as well, but I have no clue from which crate. I have a working Rust lib that I compiled...
Read more >
Rust/Wasm: Module not found: Error: Can't resolve 'env' in
In my rust crate (which is a --lib crate), I only call wasm-pack build --target browser . This will create a pkg folder...
Read more >
Module not found: Error: Can't resolve 'whatwg-fetch'
I'm trying to merge Cornerstone 4.8.0 into my theme and I get this error when I try to bundle or start stencil. Does...
Read more >
How to fix 'Module not found: Can't resolve 'http' in ... - YouTube
Basically, just change 'react-scripts' to 4.0.2 in your package.json and run `npm install` again :D Follow me on Twitter:Β ...
Read more >
module not found error can't resolve 'fs' in react js
Your quick fix is to take react scripts down to v4 until a fix for v5 is in place unless you are comfortable...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found