admin管理员组文章数量:1208153
I couldn't find any answers on importing JSON files with the new ES modules implementation, all the answers that I've found on StackOverflow are for code that's transpiled using Babel, I want to import my package.json
file:
import pkg from '../package.json';
And I'm getting this error:
(node:7863) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
internalBinding('errors').triggerUncaughtException(
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /home/user/files/project/package.json imported from /home/user/files/project/version.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:126:13)
at Loader.resolve (internal/modules/esm/loader.js:72:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:156:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
at link (internal/modules/esm/module_job.js:41:36) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
I'm using the latest Node.js 13.6.0, am I only left with the option to read the file using the fs
module?
I couldn't find any answers on importing JSON files with the new ES modules implementation, all the answers that I've found on StackOverflow are for code that's transpiled using Babel, I want to import my package.json
file:
import pkg from '../package.json';
And I'm getting this error:
(node:7863) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
internalBinding('errors').triggerUncaughtException(
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".json" for /home/user/files/project/package.json imported from /home/user/files/project/version.js
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:126:13)
at Loader.resolve (internal/modules/esm/loader.js:72:33)
at Loader.getModuleJob (internal/modules/esm/loader.js:156:40)
at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
at link (internal/modules/esm/module_job.js:41:36) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
I'm using the latest Node.js 13.6.0, am I only left with the option to read the file using the fs
module?
- 1 Does this answer your question? How to import a json file in ecmascript 6? – Justin Workman Commented Jan 14, 2020 at 17:37
- Um, JSON isn't Javascript, so why would you think that it would? Just read the file and parse the contents. – Jared Smith Commented Jan 14, 2020 at 17:45
- @JustinWorkman Actually no, I've checked all the answers and none of them worked for me, I wanted to check if there's a solution other than reading the file. – Pierre Commented Jan 14, 2020 at 17:57
- @Pierre you have two options: read the file and parse the contents, or use a loader like webpack that deals with non-JS assets (not appropriate for a node project, obvs). Ecmascript modules are for, well, Ecmascript, JSON is completely different. – Jared Smith Commented Jan 16, 2020 at 12:34
- stackoverflow.com/a/60206393/1653236 – CodeFinity Commented Sep 24, 2021 at 12:55
2 Answers
Reset to default 28I've found in the Node.js ES Modules docs that currently importing JSON is only supported in the CommonJS mode and the flag --experimental-json-modules
is required for importing JSON files in ES modules.
Assuming an index.mjs with
import packageConfig from './package.json';
The --experimental-json-modules flag is needed for the module to work.
node index.mjs # fails
node --experimental-json-modules index.mjs # works
You can import it as so:
import * as config from '../config.json'
本文标签: javascriptDoes the ES modules implementation support importing JSON filesStack Overflow
版权声明:本文标题:javascript - Does the ES modules implementation support importing JSON files? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738746463a2110147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论