N Kaushik

Fix for TypeError ERR_UNKNOWN_FILE_EXTENSION- Unknown file extension .json

September 26, 2021

Fix for TypeError ERR_UNKNOWN_FILE_EXTENSION: Unknown file extension .json:

If you try to import a JSON file in a NodeJS project, you might have faced this issue:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for 'file.json'

Complete error log:


TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /Users/cvc/Documents/Development🌎🌎🌎/notes/writing/programs/npm/example.json
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:101:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:31)
    at async ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:53:21)
    at async Promise.all (index 0)
    at async link (internal/modules/esm/module_job.js:58:9) {
npm ERR! errno 1
npm ERR! npm@1.0.0 start: `node example.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the npm@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/cvc/.npm/_logs/2021-09-26T06_18_08_962Z-debug.log

JSON module import error nodejs

Reason and solution:

Currently, JSON module import is experimental. Check here. It is supported only in commonjs mode.

But, you can use the flag —experimental-json-modules with node to fix this error.

If you add this flag, both commonjs and module mode works with .json file import.

"start": "node --experimental-json-modules example.js"


Named import is not supported for JSON imports. For example, if you have a example.js file with a example.json file in the same folder, you can import it as like below:

import e from './example.json';


It will print the contents of the .json file.

Subscribe to my Newsletter