Así que imaginemos que tengo dos scripts js
script1.js
export default function Hello() { return "Hello buddy!"; }script2.js
import { Hello } from './script1.js'; function print(){ let val = Hello; console.log(val); }Cuando ejecuto la función de impresión en el navegador, aparece el siguiente error
Uncaught SyntaxError: Cannot use import statement outside a module Unexpected token 'export'Investigué un poco y esto se resolvió agregando un módulo de tipo a script2.js. Pero la pregunta es. No tengo un HTML para cambiar el script. Hago todo en vainilla javascript. Entonces, ¿la solución es obtener los scripts por ID y cambiar el tipo de script2.js de text/javascript a module?
¿Hay alguna otra forma de cambiar el script2.js al módulo?
Puede agregar un archivo .mjs .
Ejemplo
// index.js import otherFile from './otherFile.js'; // otherFile.js import otherOtherFile from './otherOtherFile.mjs'; export default './otherFile.js'; // otherOtherFile.mjs export default 'otherOtherFile.mjs';