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

188
Views
In which case the entire JavaScript file will be called?

I search on the Internet and it said that the difference between require and import is that require will call entire JS file. So in my situation, does entire module.js file will be called? And if it not, in which case that file will be called entirely ?

module.js

const a = 10;
const b = 20;
const c = 30;

module.exports = { a , b };

app.js:

const nums = require('./module');

console.log(nums);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Require is Non-lexical, it stays where they have put the file. It can be called at any time and place in the program.You can directly run the code with require statement.If you want to use require module then you have to save file with ‘.js’ extension.

Import is lexical, it gets sorted to the top of the file. It can’t be called conditionally, it always run in the beginning of the file.To run a program containing import statement you have to use experimental module feature flag.If you want to use import module then you have to save file with ‘.mjs’ extension.

about 4 years ago · Juan Pablo Isaza Report

0

For some reason using the name module removed the code highlighting so I added the comments which fixed it....

In you example the entire module file is not loaded. And it is not correct to say that it can be. Module exports is not a file based module loader. Module functionality technically could be spread over many files. But, if you create a default and put everything in it that would be nearly the same thing.

(Assuming you are only using a single file to define your module.)

module.js

export const name = /** < **/ module /** name >**/;


export default  /** < **/ module /** name >**/;

You could then use named exports as before or using one of the import default syntax variations grab the entire file.

app.js

import /** < **/ module /** name> **/ from './modules.js';

or alternatively

import {default as /** < **/ module /** name> **/} from './modules.js';
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!