Repository navigation
import(data:text/javascript,) throw SyntaxError #42504
Description
Activity
A simpler example is here:
const c = "const a = '?' === false;"; import(`data:text/javascript,${c}`).then((res) => { console.log(res); });
Yep, it looks like Node.js is acting differently than other JS runtimes. //cc @nodejs/loaders
FWIW, you can workaround this by encoding special URL characters:
const c = "const a = '?' === false;"; import(`data:text/javascript,${encodeURIComponent(c)}`).then((res) => { console.log(res); });
Please encode special components and/or use base64 support in your
data:URIs for cross environment stability, Firefox also fails in this case:This failure is caused by a disagreement on if query parameters are valid on
data:URIs. I'd note that according tonew url()everything after the?is a query parameter and is the main reason this behavior is as Node implemented it.Reacted by tingyuanYep, it looks like Node.js is acting differently than other JS runtimes. //cc @nodejs/loaders
FWIW, you can workaround this by encoding special URL characters:
const c = "const a = '?' === false;"; import(`data:text/javascript,${encodeURIComponent(c)}`).then((res) => { console.log(res); });
Thanks, It works on node16, but still failed on node14, but with base64 format, both will work well, like this:
const c = "const a = '?' === false;"; import(`data:text/javascript;base64,${Buffer.from(c).toString('base64')}`).then((res) => { console.log(res); });
Yep, it looks like Node.js is acting differently than other JS runtimes. //cc @nodejs/loaders
FWIW, you can workaround this by encoding special URL characters:const c = "const a = '?' === false;"; import(`data:text/javascript,${encodeURIComponent(c)}`).then((res) => { console.log(res); });
Thanks, It works on node16, but still failed on node14, but with base64 format, both will work well, like this:
const c = "const a = '?' === false;"; import(`data:text/javascript;base64,${Buffer.from(c).toString('base64')}`).then((res) => { console.log(res); });
I think the fix was already backported to v14.18.0 (98259dc). Are you testing on an up-to-date version?
Yep, it looks like Node.js is acting differently than other JS runtimes. //cc @nodejs/loaders
FWIW, you can workaround this by encoding special URL characters:const c = "const a = '?' === false;"; import(`data:text/javascript,${encodeURIComponent(c)}`).then((res) => { console.log(res); });
Thanks, It works on node16, but still failed on node14, but with base64 format, both will work well, like this:
const c = "const a = '?' === false;"; import(`data:text/javascript;base64,${Buffer.from(c).toString('base64')}`).then((res) => { console.log(res); });
I think the fix was already backported to v14.18.0 (98259dc). Are you testing on an up-to-date version?
yes yes, 14.18.0 is ok,👍 (the error occurs on 14.17.1)
The problem is solved, if there is no other questions, I will close this issue after a few days. Thank you very much.
The problem is solved, if there is no other questions, I will close this issue after a few days. Thank you very much.
Always use
encodeURIComponentwith source code string in dynamic importing data url, and better to avoid using this(not a best practice)

Version
v16.14.2
Platform
Darwin tingyuandeMacBook-Pro.local 21.1.0 Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:23 PDT 2021; root:xnu-8019.41.5~1/RELEASE_X86_64 x86_64
Subsystem
No response
What steps will reproduce the bug?
Just run the code with latest node, error is here:
but, in chrome, the code runs well, no error.

How often does it reproduce? Is there a required condition?
100% reproduce.
What is the expected behavior?
no error should be thrown, It seems related to
?(ternary operator).What do you see instead?
node can not run the code, but chrome can run the same code.
Additional information
No response