Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!