这个代码运行得很好。
我不知道是不是因为我升级到Node 17什么的,但现在我
TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]:
Module "file:///Users/xxxxx/code/projects/xxxxx/dist/server/data/countries.json"
needs an import assertion of type "json"
在我的api.ts
中有:
import countryTable from './data/countries.json';
下面是我如何启动由api.ts
使用的server.ts
NODE_ENV=production node --optimize_for_size --trace-warnings --experimental-json-modules --no-warnings server/server.js
发布于 2021-11-25 06:50:46
您需要使用:
import countryTable from "./data/countries.json" assert { type: "json" };
发布于 2022-09-16 16:07:40
对于任何有ESLint验证问题的人(由于assert
还不受支持),您可以尝试同步地从文件系统加载JSON:
const loadJSON = (path) => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));
const countries = loadJSON('./data/countries.json');
发布于 2022-11-19 22:07:10
若要在ES模块中导入.json
,可以使用
module.createRequire()
https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#no-require-exports-or-moduleexports
因为在node.js的第18.12.1版中断言仍然是实验性的
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const countryTable = require('./data/countries.json');
https://nodejs.org/dist/latest-v18.x/docs/api/module.html#modulecreaterequirefilename
https://stackoverflow.com/questions/70106880
复制相似问题