You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 2, 2023. It is now read-only.
constpackageJson=JSON.parse(fs.readFileSync('./package.json'));console.log(`Running version ${packageJson.version}`);
This can also be written as:
constpackageJson=require('./package.json');console.log(`Running version ${packageJson.version}`);
Both of these examples are written without asynchronous syntax on the user’s part: there are no callbacks, no promises, no await. Perhaps Node is doing asynchronous stuff behind the scenes, but the user is unaware of it. require appears to be as synchronous as readFileSync.
This feature request is that a user’s code to import JSON files via ESM should be like these examples: no callbacks, no promises, no await. This feature request doesn’t concern Node’s implementation under the hood, and whether it is synchronous or asynchronous; the request is only that the user’s code be able to be written in a synchronous style.
ESM imports of JSON can be written in a synchronous style:
import{version}from'./package.json';console.log(`Running version ${version}`);
The point of this feature is that users can use the same synchronous style of coding (in the vein of fs.readFileSync as opposed to callback- or promise-based alternatives) when the import is of JSON. Or put another way, users aren’t required to use await or promises or other asynchronous constructs in code in order to import JSON.