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

673
Views
import() in ts is not dynamic? what's the cause?

According to MDN, import() is a function-like dynamic method. However, I found it not dynamic in my ts project.

Say I have an appleShare.json:

{
    price: 123
}

And then, there's an index.ts:

console.log("update the price to 456...")
// manually modify the json file content making price 456
let currentPrice = await import("./appleShare.json").then(obj=obj.price)
console.log(currentPrice)  

I ran index.ts directly in vs-code, the result in console:

update the price to 456...
123

I expect 456, but got 123. From my limited knowledge I guess there are two possible reasons:

  1. I have a misunderstanding in import() and dynamic importing.
  2. My understanding is right but vs-code compiled all the code to js before executing them. So, I will never get the newly modified price.

I want to ask, what's exactly the cause of the issue, and how to resolve it?

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

0

The "dynamic" import() has the following different behaviors from the regular import which is often referred to as the "static" import.

  1. You can construct a module filename in code and can then load that module from the filename you built. You cannot dynamically build module filenames with the regular import. Filenames for the regular import must be statically specified so they are known by anyone who parses the file, but does not run the code in it. This static declaration enables code analysis for things like tree-shaking and bundling. The dynamic import cannot be used as effectively with features like that.
  2. The import() can happen anywhere in your code (not only at the top of your module). The regular import cannot be just anywhere in your code. In this sense, it is "dynamically" loaded upon demand, not only at the beginning of the module.
  3. A dynamic import() statement can be used to load an ESM module into a CommonJS module. The regular import statement cannot be used in a CommonJS Module at all.

Modules, even dynamically loaded ones are cached. Once they are loaded, subsequent import() statements using the same filename just load the module from the cache, they do not re-read the file. That's why your subsequent import() is not picking up the modified JSON.

If you want to re-read the file, then don't use import - use something like fs.promises.readFile() and then parse the JSON. This will read a fresh copy of the data each time you call it.

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!