fix: replace assert JSON imports with createRequire for compatibility#292
Open
Tsaihemanth150 wants to merge 1 commit into
Open
fix: replace assert JSON imports with createRequire for compatibility#292Tsaihemanth150 wants to merge 1 commit into
Tsaihemanth150 wants to merge 1 commit into
Conversation
Author
|
@wajeshubham , please consider reviewing above PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request fixes a compatibility issue with JSON imports in ES modules.
Previously, the project used the following pattern to import .json files:
import data from "./file.json" assert { type: "json" };
While this works in some versions of Node.js with experimental flags, it often fails with the error:
SyntaxError: Unexpected identifier 'assert'
To ensure consistent behavior across environments, all such imports were replaced with:
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const data = require("./file.json");
This approach:
Avoids the need for --experimental-json-modules flag.
Works reliably in ES module projects.
Fixes crashes during app startup (seen in yarn start).
Enhances developer experience and avoids platform-specific behavior.
✅ Changes Made
Replaced all import ... assert { type: "json" } with createRequire()-based imports.
Applied changes across all JSON import files (e.g., books.json, cats.json, status-codes.json, etc.).
Ensured consistent structure with a single createRequire declaration per file.
🧪 Tested
Verified that yarn start runs successfully without crashing.
Confirmed that all JSON data loads correctly and behaves as expected.
@hiteshchoudhary @anirudhuuu @sangamesh1439 @tarun-kavipurapu