133

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

6 Answers 6

249

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" };

You will need to use:

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

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

11
  • 12
    Import assertions were introduced in node v17.1.0 according to this, but became required after v17.5.0 (related to this change:github.com/nodejs/node/pull/40785). Import assertions address a security concern (v8.dev/features/import-assertions) about trusting cross-origin imports.
    – pbatey
    Mar 21, 2022 at 23:07
  • Thanks! You just saved me a world of trouble understanding why a code that worked fine yesterday suddenly stopped working! I actually needed the second version detailed in GitHub, but you saved me a world of hurt! Mar 31, 2022 at 10:45
  • i dont have a data folder Jul 13, 2022 at 2:27
  • 5
    If you do this and get TS2821: Import assertions are only supported when the '--module' option is set to 'esnext' you can just add // @ts-ignore to the line above the import as a workaround for the moment.
    – Malvineous
    Dec 24, 2022 at 8:13
  • 1
    @UneconscienceUneSource its a CORS related security measure. Obviously python does not have to deal with this. Feb 2 at 12:05
22

For anyone having ESLint validation issues (due to assert not being supported yet), you can try loading the JSON from the filesystem synchronously:

const loadJSON = (path) => JSON.parse(fs.readFileSync(new URL(path, import.meta.url)));

const countries = loadJSON('./data/countries.json');

Reference: https://github.com/eslint/eslint/discussions/15305

3
  • readFile blocks, so it would be better to promisify it and use top level await Jul 29 at 3:44
  • I get Cannot use 'import.meta' outside a module
    – coler-j
    Sep 18 at 15:17
  • @coler-j this will not work with CJS. Sep 18 at 15:18
14

To import .json in ES modules you can use

module.createRequire()

https://nodejs.org/dist/latest-v18.x/docs/api/esm.html#no-require-exports-or-moduleexports

because assertions are still experimental in v18.12.1 of node.js

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

5

If you are not using Node v17.1.0+ and are instead stuck with v16 or lower, AND you are using the --es-module-specifier-resolution=node flag to the node process, something like this:

{
  "start": "node --es-module-specifier-resolution=node --no-warnings src/index.js"
}

trying to do something like this:

import swaggerDocument from "../swagger.json" assert { type: "json" }

will result in the following error:

ReferenceError: require is not defined in ES module scope, you can use import instead.

Trying to do this:

const swaggerDoc = require('../swagger.json');

will result in the same above error.

There are several solutions:

import { readFile } from 'fs/promises';
const swaggerDoc = JSON.parse(
  await readFile(new URL('../swagger.json', import.meta.url))
);

or:

import { createRequire } from "module";
const require = createRequire(import.meta.url);
const swaggerDoc = require("../swagger.json");

I prefer the first way. I think it is a little more clear as to what is happening.

1

This works only if you need to import a specific object that having issues with ES6+.

module.createRequire()

Code Example:

import { createRequire } from 'node:module'

// Import the package.json file to get the version number by using the createRequire function
const require = createRequire(import.meta.url)
const { version } = require('../../package.json')

You can also use a bundler to convert your code on commonjs.

-1

once you have imported your data,and signifying the address , you should type assert {type:"json"} to eliminate this error. import assertion has been introduced in node v17 for data checking and authentication.

Type something like this import data from './data/mock.json' assert {type:"json"}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.