PositiveGuy
PositiveGuy

Reputation: 20162

ERR_IMPORT_ASSERTION_TYPE_MISSING for import of json file

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

Upvotes: 231

Views: 190695

Answers (7)

Q10Viking
Q10Viking

Reputation: 1111

In my project, I wanted to import tsconfig.json in rollup.config.js using a file-to-URL approach. Although I configured assert: { type: 'json' }, it still threw an error.

  import { resolve } from 'node:path'
  import { pathToFileURL } from 'node:url'

  const dir = process.cwd()
  const tsconfigPath = resolve(dir, 'tsconfig.json')
  const tsconfigJson = await import(pathToFileURL(tsconfigPath).href, {
    assert: { type: 'json' }
  })

Eventually, I resolved it by using the following syntax:

import tsconfigJson from './tsconfig.json' with { type: "json" }

For reference, my Node.js version is v22.13.0.

Upvotes: 0

GoldenretriverYT
GoldenretriverYT

Reputation: 4755

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

Upvotes: 411

Moein samani
Moein samani

Reputation: 21

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

Upvotes: 1

Daniel Viglione
Daniel Viglione

Reputation: 9407

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.

Upvotes: 8

Powerm1nt.
Powerm1nt.

Reputation: 31

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.

Upvotes: 3

Sergei Krikun
Sergei Krikun

Reputation: 261

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

Upvotes: 26

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

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

Upvotes: 32

Related Questions