jsonファイルのインポートでERR_IMPORT_ASSERTION_TYPE_MISSINGが発生する 質問する

jsonファイルのインポートでERR_IMPORT_ASSERTION_TYPE_MISSINGが発生する 質問する

このコードは正常に動作していました。

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

ベストアンサー1

更新: 提案によると、新しいキーワードはwithの代わりに ですassertが、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

おすすめ記事