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

184
Views
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 answers
Answer question

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 Report
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!