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

177
Views
Does an imported file inherit the scope of its importer?

When a file is imported in JavaScript, does that action cause the scope of variables in the imported file become that of the importer file, thus negating the necessity to add import declarations in the second imported file since they are declared in the importer file?

For example, if this is loaded first:

/* Config.js */
import { ImportantVar, OkayVar, LastVar } from 'first/file/path.js';
import DataObject from 'second/file/path.js';

and then this is loaded:

/* Second File */
export const DataObject = function( param1, param2 ) {
   return new ImportantVar( param1, param2 );
}

Should this line be added to the top of the second file?

import ImportantVar from 'first/file/path.js';

I don't care if the second file is broken should that it be run independently, i.e. not as an import from config.js.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No. Every module has its own scope.

You're not "importing a file" or "importing the code to be run", you are importing the exported bindings of the module; the module stands on its own, and its exports can be imported in multiple places (while the code is only evaluated once). Inheriting scope wouldn't work with that.

So yes, you'll need to import { ImportantVar } from 'first/file/path.js' in your example.

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!