No articles found
Try different keywords or browse our categories
Fix: ExperimentalWarning: Importing JSON modules error
Quick fix for 'ExperimentalWarning: Importing JSON modules' error in Node.js. Learn how to properly import JSON files in modern Node.js applications.
The ‘ExperimentalWarning: Importing JSON modules’ error occurs when importing JSON files in ES modules, as JSON module imports are still experimental in Node.js and require explicit enabling.
How the Error Happens
❌ Error Scenario:
// ❌ This causes the warning
import config from './config.json'; // ExperimentalWarning: Importing JSON modules is an experimental feature
✅ Quick Fix - Handle JSON Imports Properly
Solution 1: Use Dynamic Import with Assertions
// ✅ Import JSON with type assertion
const config = await import('./config.json', { assert: { type: 'json' } });
console.log(config.default); // ✅ Access the JSON data
Solution 2: Use fs.promises.readFile
// ✅ Read JSON files using fs
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const configPath = join(__dirname, 'config.json');
const config = JSON.parse(await readFile(configPath, 'utf8'));
console.log(config);
Solution 3: Use require() in ES Modules
// ✅ Use require for JSON in ES modules
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const config = require('./config.json');
console.log(config);
Solution 4: Enable JSON Modules (Experimental)
# ✅ Run with JSON modules enabled
node --experimental-json-modules app.js
// ✅ Then you can import JSON directly (still experimental)
import config from './config.json' assert { type: 'json' };
console.log(config); Related Articles
Fix: ERR_PACKAGE_PATH_NOT_EXPORTED error
Quick fix for 'ERR_PACKAGE_PATH_NOT_EXPORTED' error in Node.js. Learn how to resolve module export issues in ES modules and package configurations.
Fix: ERR_MODULE_NOT_FOUND Node.js error
Quick fix for 'ERR_MODULE_NOT_FOUND' error in Node.js. Learn how to resolve module resolution issues in ES modules and CommonJS.
Fix: ERR_UNKNOWN_FILE_EXTENSION '.ts' error
Quick fix for 'ERR_UNKNOWN_FILE_EXTENSION .ts' error in Node.js. Learn how to properly run TypeScript files in Node.js environments.