Did you know you can now import .json
files natively in modern JavaScript without fetch()
, bundlers, or dynamic hacks?
Yes, itβs officially supported in all modern browsers and Node.js (v20.6+), and it looks like this:
js
import config from './config.json' assert { type: 'json' };
console.log(config.theme);
Thatβs it. No more boilerplate. π
β
Works In:
Chrome, Firefox, Safari, Edge (2024+)
Node.js v20.6+
You just need to set "type": "module" in your package.json
π§ Why This Is a Game-Changer
Instead of doing:
fetch('/config.json')
.then(res => res.json())
.then(data => console.log(data));
You now just:
import config from './config.json' assert { type: 'json' };
Cleaner syntax. Synchronous. Built-in support.
Itβs perfect for:
App configs
Locale files
Static content for blogs and docs
DevOps dashboards
Theme/data presets
`
π§ͺ Common Pitfalls & Fixes
β Error | β Fix |
---|---|
Cannot find module |
Check your file path and extension |
Unexpected token |
Make sure the JSON is valid (Use Formatter) |
Module parse failed |
Add assert { type: 'json' } to your import |
CORS error |
Enable CORS or load JSON from the same origin |
`
π οΈ Bonus Tool β Format & Validate Your JSON First
If your JSON file has even one trailing comma, this import will fail.
Before importing, I use this free tool I built to format, validate, and preview JSON quickly:
π https://jsonformatter.online
It also supports:
- JSON to CSV, YAML, XML, Markdown
- Download as Excel (XLSX)
- A full JSON Log Viewer for NDJSON & streaming
- Upload or fetch JSON from a URL
Give it a try and let me know what features youβd love to see!
π Read the Full Guide Here
I wrote a full breakdown of how to use native JSON imports, whatβs supported, how to structure projects, and common gotchas here:
π How to Use Native JSON Imports in JavaScript (2025 Update)
Top comments (2)
This is incorrect.
assert
should not be used as it has been deprecated in favour of thewith
syntax which enjoys wide support across modern browsers (whichassert
does not, as you incorrectly state it does):caniuse.com/mdn-javascript_stateme...
developer.mozilla.org/en-US/docs/W...
oh! good catch :)