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

207
Vistas
Import in VSCode without using .mjs files

I'm having trouble importing files in JavaScript. I am fairly new to the language and have followed a couple of VSCode tutorials that demonstrate and explain importing. Their importing seems to work out of the box, with one tutorial setting up his files like this, which worked for him: Working import

I also made sure to set up my index.html properly to use ES6 module imports. My index.html looks like this:

<!DOCTYPE html>
<html>
<head>
    <title>JS Modules</title>
    <script type="module" src="main.js"></script>
</head>
<body>
</body>
</html>

But no matter what I do, the only way I can get VSCode to handle imports and not give this error

SyntaxError: Cannot use import statement outside a module

is by changing my .js files to .mjs files (which makes my imports function properly). I have a vague understanding that this is because of how JS is implemented using Node in VSCode and Node does not play nicely with imports(?), but am not completely clear on that.

I have also browsed other questions about this topic, but none explain why those tutorial videos are able to use imports seemingly out-of-the-box in VSCode with .js file extensions when I cannot, so if someone could shed light on this that would be much appreciated.

EDIT: My JavaScript code (copied from video):

main.js:

import User from './user.js'

const user = new User('Bob', 11);
console.log(user)

user.js:

export default class User {
    constructor(name, age) {
        this.name = name;
        this.age = age;
    }
}

export function printName(user) {
    console.log(`User's name is ${user.name}`)
}

export function printAge(user) {
    console.log(`User's age is ${user.age}`)
}
about 4 years ago · Juan Pablo Isaza
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