Can't use import attributes

1 week ago 16
ARTICLE AD BOX

I'm trying to import JSON:

this.inv = JSON.parse(await readFile(new URL("../../resources/inventory.json", import.meta.url), 'utf8'));

This builds fine but when I run Jest tests (npx jest) I get this error:

error TS1343: The 'import.meta' meta-property is only allowed when the '--module' option is 'es2020', 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', or 'nodenext'.

But module is set to nodenext.

I previously tried directly importing using:

import inv from "../../resources/inventory.json" with { type: 'json' };

But that failed with a similar error. How do I get ts-jest to work with nodenext?

tsconfig.json:

{ "compilerOptions": { "resolveJsonModule": true, "declaration": true, "module": "NodeNext", "outDir": "dist", "rootDir": "src", "strict": true, "target": "es2022", "types": ["node", "jest"], "moduleResolution": "nodenext", "esModuleInterop": true }, "include": ["./src/**/*"], "ts-node": { "esm": true } }

jest.config.ts:

import type { Config } from 'jest'; const config: Config = { preset: 'ts-jest/presets/default-esm', roots: ['<rootDir>/test'], extensionsToTreatAsEsm: ['.ts'], testEnvironment: 'node', transform: {} }; export default config;

and type is set to module in package.json.

Node.js v24.11.1 "ts-jest": "^29.4.4", "ts-node": "^10", "typescript": "^5"
Read Entire Article