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
Just run the code with latest node, error is here:
data:text/javascript,var config0 = { text: '{{ a => a === "aa" ?%20%22bb%22%20:%20%22cc%22%20}}'};var%20_configs%20=%20{%20%22aa%22:%20config0%20};var%20configFiles%20=%20/*%20@__PURE__%20*/%20Object.freeze(/*%20@__PURE__%20*/%20Object.defineProperty({%20%20__proto__:%20null,%20%20%22default%22:%20_configs},%20Symbol.toStringTag,%20{%20value:%20%22Module%22%20}));export%20{%20configFiles%20as%20default%20};:1
var config0 = { text: '{{ a => a === "aa"
^^^^^^^^^^^^^^^^^^^^
SyntaxError: Invalid or unexpected token
at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:115:18)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:289:14)
at async link (node:internal/modules/esm/module_job:70:21)
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.
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 to new URL() everything after the ? is a query parameter and is the main reason this behavior is as Node implemented it.
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:
constc="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:
constc="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:
constc="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:
constc="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)
Activity
lovetingyuan commentedon Mar 28, 2022
A simpler example is here:
aduh95 commentedon Mar 28, 2022
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:
bmeck commentedon Mar 28, 2022
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.lovetingyuan commentedon Mar 29, 2022
Thanks, It works on node16, but still failed on node14, but with base64 format, both will work well, like this:
aduh95 commentedon Mar 29, 2022
I think the fix was already backported to v14.18.0 (98259dc). Are you testing on an up-to-date version?
lovetingyuan commentedon Mar 29, 2022
yes yes, 14.18.0 is ok,👍 (the error occurs on 14.17.1)
lovetingyuan commentedon Mar 29, 2022
The problem is solved, if there is no other questions, I will close this issue after a few days. Thank you very much.
lovetingyuan commentedon Mar 29, 2022
Always use
encodeURIComponentwith source code string in dynamic importing data url, and better to avoid using this(not a best practice)