[英]is there a require for json in node.js
I would like to include a couple of JSON files in my JavaScript code that are in the same directory as my JavaScript source file.我想在我的 JavaScript 代码中包含几个 JSON 文件,这些文件与我的 JavaScript 源文件位于同一目录中。
If I wanted to include another JavaScript file I could simply use require
.如果我想包含另一个 JavaScript 文件,我可以简单地使用
require
。 Now I'm using readFileSync
and __dirname
to get the JSON, which I think is an ugly way to do it.现在我使用
readFileSync
和__dirname
来获取 JSON,我认为这是一种丑陋的方法。
Is there something similar for require that enables me to load a JSON file?是否有类似的 require 使我能够加载 JSON 文件?
As of node v0.5.x yes you can require your JSON just as you would require a js file.从 node v0.5.x 开始,您可以像需要 js 文件一样需要 JSON。
var someObject = require('./somefile.json')
In ES6:在 ES6 中:
import someObject from ('./somefile.json')
JSON files don't require an explicit exports statement. JSON 文件不需要显式导出语句。 You don't need to export to use it as Javascript files.
您无需导出即可将其用作 Javascript 文件。
So, you can use just require
for valid JSON document.因此,您可以只对有效的 JSON 文档使用
require
。
data.json数据.json
{
"name": "Freddie Mercury"
}
main.js主文件
var obj = require('data.json');
console.log(obj.name);
//Freddie Mercury
Two of the most common最常见的两种
First way :第一种方式:
let jsonData = require('./JsonFile.json')
let jsonData = require('./JsonFile')
// if we omitting .json also works let jsonData = require('./JsonFile')
// 如果我们省略let jsonData = require('./JsonFile')
也可以
OR或者
import jsonData from ('./JsonFile.json')
Second way :第二种方式:
1) synchronously 1) 同步
const fs = require('fs')
let jsonData = JSON.parse(fs.readFileSync('JsonFile.json', 'utf-8'))
2) asynchronously 2)异步
const fs = require('fs')
let jsonData = {}
fs.readFile('JsonFile.json', 'utf-8', (err, data) => {
if (err) throw err
jsonData = JSON.parse(data)
})
Note: 1) if we JsonFile.json is changed, we not get the new data, even if we re run require('./JsonFile.json')注意:1)如果我们 JsonFile.json 被改变了,我们不会得到新的数据,即使我们重新运行 require('./JsonFile.json')
2) The fs.readFile or fs.readFileSync will always re read the file, and get changes 2) fs.readFile 或 fs.readFileSync 将始终重新读取文件,并获得更改
You can import json files by using the node.js v14 experimental json modules flag.您可以使用 node.js v14 实验性 json 模块标志导入 json 文件。 More details here
更多细节 在这里
file.js文件.js
import data from './folder/file.json'
export default {
foo () {
console.log(data)
}
}
And you call it with node --experimental-json-modules file.js
你用
node --experimental-json-modules file.js
调用它
A nifty non-caching async one liner for node 15 modules:一个用于 node 15 模块的漂亮的非缓存异步单行:
import { readFile } from 'fs/promises';
const data = await readFile('{{ path }}').then(json => JSON.parse(json)).catch(() => null);
You even can use require of your JSON without specifying the extension .json .您甚至可以在不指定扩展名.json 的情况下使用 JSON 的require 。 It will let you change the file extension to .js without any changes in your imports.
它将允许您将文件扩展名更改为.js,而无需对导入进行任何更改。
assuming we have ./myJsonFile.json in the same directory.假设我们在同一目录中有./myJsonFile.json 。
const data = require('./myJsonFile')
If in the future you'll change ./myJsonFile.json to ./myJsonFile.js nothing should be changed in the import.如果将来您将./myJsonFile.json更改为./myJsonFile.js ,则导入时不应更改任何内容。
You can use a module to create a require.您可以使用模块来创建需求。
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const foo = require('./foo.js')
如果你使用的是 typescript ,你可以在你的 tsconfig.json 中添加一个名为resolveJsonModule: true的新字段,然后你可以像这样导入任何 .json 文件的所有信息:
import * as jsonfile from "./path/to/json"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.