最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to import json files in node v22 - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jul 17, 2024 at 12:47 MUHAMMAD AsimMUHAMMAD Asim 1041 silver badge8 bronze badges 2
  • 2 try -> with: { type: 'json' },` assert has been dropped, more info -> github./nodejs/node/issues/51622 – Keith Commented Jul 17, 2024 at 13:01
  • 1 yup with keyword worked and i could rollup event without @rollup/plugin-json thanks alot – MUHAMMAD Asim Commented Jul 18, 2024 at 10:16
Add a ment  | 

1 Answer 1

Reset to default 7

As mentionned in @keith ment, 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' }});
发布评论

评论列表(0)

  1. 暂无评论