diff --git a/lib/internal/modules/esm/formats.js b/lib/internal/modules/esm/formats.js index 63742914597c46..c4181b50f10451 100644 --- a/lib/internal/modules/esm/formats.js +++ b/lib/internal/modules/esm/formats.js @@ -26,7 +26,7 @@ if (experimentalWasmModules) { function mimeToFormat(mime) { if ( RegExpPrototypeExec( - /\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?/i, + /^\s*(text|application)\/javascript\s*(;\s*charset=utf-?8\s*)?$/i, mime, ) !== null ) return 'module'; diff --git a/test/es-module/test-esm-invalid-data-urls.js b/test/es-module/test-esm-invalid-data-urls.js index e434c895a2e37d..6eafe75e7d7ed7 100644 --- a/test/es-module/test-esm-invalid-data-urls.js +++ b/test/es-module/test-esm-invalid-data-urls.js @@ -18,4 +18,7 @@ const assert = require('assert'); code: 'ERR_UNKNOWN_MODULE_FORMAT', message: 'Unknown module format: text/css for URL data:text/css,.error { color: red; }', }); + await assert.rejects(import('data:WRONGtext/javascriptFORMAT,console.log("hello!");'), { + code: 'ERR_UNKNOWN_MODULE_FORMAT', + }); })().then(common.mustCall());