ERR_IMPORT_ASSERTION_TYPE_MISSING 用于导入 json 文件

2024-05-09

这段代码运行良好。

我不知道是因为我升级到 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"

In my api.ts I have:

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

更新:根据提案,新的关键字是with代替assert, but assert出于兼容性原因应该保留。新代码如下所示:

import countryTable from "./data/countries.json" with { type: "json" };

您将需要使用:

import countryTable from "./data/countries.json" assert { type: "json" };

https://github.com/tc39/proposal-import-assertions https://github.com/tc39/proposal-import-assertions

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ERR_IMPORT_ASSERTION_TYPE_MISSING 用于导入 json 文件 的相关文章

随机推荐