admin管理员组文章数量:1334342
I would like to import a JSON5 file into a Javascript object in the same way [one can import a JSON file](import config from '../config.json').
Shows this message on hovering but it's clearly there
Cannot find module '../conf/config.json5' or its corresponding type declarations.ts(2307)
I have 2 side questions related to the main one above:
- Will I get intelisense for contents inside .json5, like regular json
- Does it even work like it works with require()? Do I have to use import() instead of regular import ?
I would like to import a JSON5 file into a Javascript object in the same way [one can import a JSON file](import config from '../config.json').
Shows this message on hovering but it's clearly there
Cannot find module '../conf/config.json5' or its corresponding type declarations.ts(2307)
I have 2 side questions related to the main one above:
- Will I get intelisense for contents inside .json5, like regular json
- Does it even work like it works with require()? Do I have to use import() instead of regular import ?
2 Answers
Reset to default 6You will need the package to do so: json5.
There are one of two ways we can use this:
One: (target module system is CommonJS) require it
Following the README, we register json5:
import "json5/lib/register";
Then you can use require
to import it:
const config = require("../config/config.json5");
Two: Reading the file and then using json5 to parse it:
import json5 from "json5";
import { readFile } from "fs/promises";
(async () => {
const text = await fs.readFile("./path/to/config.json5"); // path to config.json5 from entry point
const config = json5.parse(text);
})();
You can also use the provided CLI to convert json5 files to regular json that you can use.
The creator updated the json5 wiki
Wiki
You need to create an extra file with .d.ts
extension to support intellisense.
本文标签: javascriptHow to import a JSON5 file (as one can regular JSON) in TypescriptStack Overflow
版权声明:本文标题:javascript - How to import a JSON5 file (as one can regular JSON) in Typescript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742332987a2455064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论