// syntax-error.mjs
console.log([
'str1'
'str2'
]);
Perform a dynamic import with node -e "import('./syntax-error.mjs').catch(console.error)", the output does not help identify the error and could be seen by users as if node.js itself had a SyntaxError:
SyntaxError: Unexpected string
at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)
at async link (internal/modules/esm/module_job.js:37:21)
Now create and run a loader.mjs script:
Running loader.mjs produces useful output:
file:///usr/src/npm/failures/syntax-error.mjs:3
'str2'
^^^^^^
SyntaxError: Unexpected string
at Loader.moduleStrategy (internal/modules/esm/translators.js:83:18)
I was not able to find a way to catch the error of a dynamic import and show the complete SyntaxError. Interestingly node --unhandled-rejections=strict -e "import('./syntax-error.mjs')" or running await import('./syntax-error.mjs') from node --experimental-repl-await both display the location of the SyntaxError.
Perform a dynamic import with
node -e "import('./syntax-error.mjs').catch(console.error)", the output does not help identify the error and could be seen by users as if node.js itself had a SyntaxError:Now create and run a loader.mjs script:
Running loader.mjs produces useful output:
I was not able to find a way to catch the error of a dynamic import and show the complete SyntaxError. Interestingly
node --unhandled-rejections=strict -e "import('./syntax-error.mjs')"or runningawait import('./syntax-error.mjs')fromnode --experimental-repl-awaitboth display the location of the SyntaxError.