You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Node.js implemented WebAssembly.instantiateStreaming in v18.1.0 #16913
$ emcc -s ENVIRONMENT=node hello_world.c
$ node a.out.js
(node:50298) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
…/a.out.js:146
throw ex;
^
TypeError: Failed to parse URL from …/a.out.wasm
at new Request (node:internal/deps/undici/undici:4816:19)
at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
... 4 lines matching cause stack trace ...
at Object.<anonymous> (…/a.out.js:1914:11)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32) {
[cause]: TypeError [ERR_INVALID_URL]: Invalid URL
at new NodeError (node:internal/errors:377:5)
at URL.onParseError (node:internal/url:563:9)
at new URL (node:internal/url:643:5)
at new Request (node:internal/deps/undici/undici:4814:25)
at Agent2.fetch2 (node:internal/deps/undici/undici:5544:29)
at Object.fetch (node:internal/deps/undici/undici:6372:20)
at fetch (node:internal/bootstrap/pre_execution:199:25)
at instantiateAsync (…/a.out.js:1670:14)
at createWasm (…/a.out.js:1707:3)
at Object.<anonymous> (…/a.out.js:1914:11) {
input: '…/a.out.wasm',
code: 'ERR_INVALID_URL'
}
}
Node.js v18.1.0
Ideally instantiateStreaming should be called only on HTTP URIs.
Hmm, on the web fetch("page.wasm", ..) works (without http[s]://). So I think it would be best overall if node.js implemented it the same as on the web. Maybe worth looking if there is already a node bug for that, and if not, filing one.
Ideally instantiateStreaming should be called only on HTTP URIs.
Hmm, on the web fetch("page.wasm", ..) works (without http[s]://). So I think it would be best overall if node.js implemented it the same as on the web. Maybe worth looking if there is already a node bug for that, and if not, filing one.
There are at two tricky problems that would need to be addressed in Emscripten:
Emscripten is passing a path rather than an actual URL when ENVIRONMENT_IS_NODE is true.
Emscripten only support CJS for Node.js, and CJS doesn't really have a URL to resolve a relative URL (on ESM context, we could use import.meta.url)
FWIW I've tried to see if it would work using from an HTTP context using ESM over HTTP, but it looks like Emscripten doesn't produce a valid JS file (it expect a global require to be present instead of creating it):
$ emcc -s ENVIRONMENT=node -o hello.mjs hello.c
$ node --input-type=module -e 'import hello from "./hello.mjs";await hello()'file://…/hello.mjs:118 if (!(typeof process == 'object' && typeof require == 'function')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); ^Error: not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?) at file://…/hello.mjs:118:76 at file://…/[eval1]:1:52 at ModuleJob.run (node:internal/modules/esm/module_job:198:25) at async ESMLoader.eval (node:internal/modules/esm/loader:319:24) at async loadESM (node:internal/process/esm_loader:83:5) at async handleMainPromise (node:internal/modules/run_main:65:12)Node.js v18.1.0
$ python3 -m http.server &
$ node --experimental-network-imports --input-type=module -e 'import hello from "http://localhost:8000/hello.mjs";await hello()'(node:61848) ExperimentalWarning: Network Imports is an experimental feature. This feature could change at any time(Use `node --trace-warnings ...` to show where the warning was created)http://localhost:8000/hello.mjs:118 if (!(typeof process == 'object' && typeof require == 'function')) throw new Error('not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?)'); ^Error: not compiled for this environment (did you build to HTML and try to run it not on the web, or set ENVIRONMENT to something - like node - and run it someplace else - like on the web?) at http://localhost:8000/hello.mjs:118:76 at file://…/[eval1]:1:59 at ModuleJob.run (node:internal/modules/esm/module_job:198:25) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ESMLoader.eval (node:internal/modules/esm/loader:319:24) at async loadESM (node:internal/process/esm_loader:83:5) at async handleMainPromise (node:internal/modules/run_main:65:12)Node.js v18.1.0
If I try to patch the resulted file so it can run on Node.js, it still fails (with a warning saying that file: URIs should not be trusted), and it also tries to use the URL as a path which of course doesn't work:
$ cat - hello.mjs > hello2.mjs <<EOFimport {fileURLToPath}from'node:url';import {createRequire}from'node:module';const require=createRequire(import.meta.url);const __dirname = fileURLToPath(new URL('./', import.meta.url));EOF
$ node --input-type=module -e 'import hello from "./hello2.mjs";await hello()'(node:61983) ExperimentalWarning: Network Imports is an experimental feature. This feature could change at any time(Use `node --trace-warnings ...` to show where the warning was created)Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm')failed to asynchronously prepare wasm: RuntimeError: Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm')warning: Loading from a file URI (file://…/hello.wasm) is not supported in most browsers. See https://emscripten.org/docs/getting_started/FAQ.html#how-do-i-run-a-local-webserver-for-testing-why-does-my-program-stall-in-downloading-or-preparingAborted(RuntimeError: Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm'))file://…/hello.mjs:180 throw ex; ^RuntimeError: Aborted(Error: ENOENT: no such file or directory, open 'file:…/hello.wasm') at abort (file://…/hello.mjs:1512:11) at getBinary (file://…/hello.mjs:1604:5) at file://…/hello.mjs:1638:53Node.js v18.1.0
Activity
preamble.js: Don't use fetch() in Node.js environment
simnalamburt commentedon May 8, 2022
I've opened a PR to fix this issue: #16917
kripken commentedon May 9, 2022
Hmm, on the web
fetch("page.wasm", ..)works (withouthttp[s]://). So I think it would be best overall if node.js implemented it the same as on the web. Maybe worth looking if there is already a node bug for that, and if not, filing one.aduh95 commentedon May 9, 2022
There are at two tricky problems that would need to be addressed in Emscripten:
ENVIRONMENT_IS_NODEis true.import.meta.url)FWIW I've tried to see if it would work using from an HTTP context using ESM over HTTP, but it looks like Emscripten doesn't produce a valid JS file (it expect a global
requireto be present instead of creating it):If I try to patch the resulted file so it can run on Node.js, it still fails (with a warning saying that
file:URIs should not be trusted), and it also tries to use the URL as a path which of course doesn't work:preamble.js: Don't use fetch() in Node.js environment
preamble.js: Don't use fetch() in Node.js environment (#16917)
fix: update to latest emscripten to avoid issues with newer version o…
Fix wasm node 18 incompatibility issue
Changes to allow using node latest