MUHAMMAD Asim
MUHAMMAD Asim

Reputation: 96

How to import json files in node v22

i was configuring a rollup.config.mjs file and this is how i used to import json files

import packageJson from "./package.json" assert { type: "json" };

as far i have tested it works in node 20 without any errors but a warning of (node:34471) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time

but in node v22 it is not working

right now i am using node 20 for rollup thanks to nvm(node version manager). i am expecting to know if there is way to import .json files in node v22

thank you for your time.

Upvotes: 5

Views: 2437

Answers (1)

MathKimRobin
MathKimRobin

Reputation: 1480

As mentionned in @keith comment, you should do this since node22:

import packageJson from "./package.json" with { type: "json" };

or for dynamic import:

const { default } = await import('./file.json', {with: {type: 'json' }});

Upvotes: 5

Related Questions