Skip to content

fix: replace assert JSON imports with createRequire for compatibility#292

Open
Tsaihemanth150 wants to merge 1 commit into
hiteshchoudhary:mainfrom
Tsaihemanth150:fix/json-imports
Open

fix: replace assert JSON imports with createRequire for compatibility#292
Tsaihemanth150 wants to merge 1 commit into
hiteshchoudhary:mainfrom
Tsaihemanth150:fix/json-imports

Conversation

@Tsaihemanth150

@Tsaihemanth150 Tsaihemanth150 commented Jul 23, 2025

Copy link
Copy Markdown

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

@anirudhuuu anirudhuuu requested a review from wajeshubham July 25, 2025 18:50

@cloudglides cloudglides left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Tsaihemanth150

Tsaihemanth150 commented Aug 24, 2025

Copy link
Copy Markdown
Author

@wajeshubham , please consider reviewing above PR.
Thank you for time.
@cloudglides @hiteshchoudhary @sangamesh1439 @anirudhuuu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants