HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

ERR_IMPORT_ASSERTION_TYPE_MISSING for import of json file

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
importfileerr_import_assertion_type_missingforjson

Problem

This code was working fine.

I don't know if it's because I upgraded to Node 17 or what, but now I get

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';


Here's how I start api.ts which is used by server.ts:

NODE_ENV=production node --optimize_for_size --trace-warnings --experimental-json-modules --no-warnings server/server.js

Solution

Update

According to the proposal, the new keyword is with instead of assert; but assert is supposed to stay for compatibility reasons. The new code would look as follows:
import countryTable from "./data/countries.json" with { type: "json" };


Old Answer

You will need to use:
import countryTable from "./data/countries.json" assert { type: "json" };


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

Context

Stack Overflow Q#70106880, score: 436

Revisions (0)

No revisions yet.