Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
Import one JS file into another in plain JS

So let's imagine i have two js scripts

script1.js

export default function Hello() {
     return "Hello buddy!";
}

script2.js

import { Hello } from './script1.js';

function print(){
     let val = Hello;
     console.log(val);
}

When i run the function print in browser i get the following error

Uncaught SyntaxError: Cannot use import statement outside a module

Unexpected token 'export' 

I did some investigation and this is solved by adding type module to script2.js. But the question is. I dont have a HTML to change the script. I do everything in vanilla javascript. so, is the solution to get the scripts by ID and change the type of the script2.js from text/javascript to module?

Is there any other way to change the script2.js to module?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can add an .mjs file.

Example

// index.js

import otherFile from './otherFile.js';
// otherFile.js

import otherOtherFile from './otherOtherFile.mjs';

export default './otherFile.js';
// otherOtherFile.mjs

export default 'otherOtherFile.mjs';
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda