As of Node v0.5.x, yes.
You can require your JSON just as you would require a JS file.
const someObject = require('./somefile.json')
In ES6:
import someObject from './somefile.json'
Answer from goatslacker on Stack OverflowAs of Node v0.5.x, yes.
You can require your JSON just as you would require a JS file.
const someObject = require('./somefile.json')
In ES6:
import someObject from './somefile.json'
JSON files don’t require an explicit exports statement. You don't need to export to use it as Javascript files.
So, you can use just require for valid JSON document.
data.json
{
"name": "Freddie Mercury"
}
main.js
var obj = require('data.json');
console.log(obj.name);
//Freddie Mercury
How to import json files in js
How to import json files in node v22 - javascript
Error when trying to use Typescript with Express
Is it possible to import yaml files in typescript ?
TypeScript natively supports JSON, but for YAML I think you will need a bundler like webpack
More on reddit.comVideos
Hi, is there any way to import .json file in to a js file like (import data from './data.json') ? I've tried that and also added <script type="module"/> in my html also still it was not working for me, I'd greatly appreciate to learn how to import json in my js file.
Thankyou in advance ;)