admin管理员组文章数量:1355031
I'm doing a small Node.js script, where I wanted to use:
in the same file. But I'm struggling on how to import both libraries at the same time.
If my type package.json
type is monJs
:
- I get
SyntaxError: Cannot use import statement outside a module
when trying to importimport { loadJsonFile } from "load-json-file"
- I get
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Dev\my-project\node_modules\load-json-file\index.js from D:\Dev\fsvl-date-check\index.js not supported. Instead change the require of D:\Dev\my-project\node_modules\load-json-file\index.js in D:\Dev\my-project\index.js to a dynamic import() which is available in all CommonJS modules.
if I try to load withrequire("load-json-file")()
.
Now if I try to switch my package.json
to module
:
- I get
SyntaxError: Named export 'prompt' not found. The requested module 'prompt-sync' is a CommonJS module, which may not support all module.exports as named exports.
when I try to import like this:import { prompt } from "prompt-sync";
. - I get
ReferenceError: require is not defined in ES module scope, you can use import instead
if I try to import it like this:var prompt = require("prompt-sync")();
How can I use both those packages in the same project?
I'm doing a small Node.js script, where I wanted to use:
- https://github./heapwolf/prompt-sync
- https://github./sindresorhus/load-json-file
in the same file. But I'm struggling on how to import both libraries at the same time.
If my type package.json
type is monJs
:
- I get
SyntaxError: Cannot use import statement outside a module
when trying to importimport { loadJsonFile } from "load-json-file"
- I get
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Dev\my-project\node_modules\load-json-file\index.js from D:\Dev\fsvl-date-check\index.js not supported. Instead change the require of D:\Dev\my-project\node_modules\load-json-file\index.js in D:\Dev\my-project\index.js to a dynamic import() which is available in all CommonJS modules.
if I try to load withrequire("load-json-file")()
.
Now if I try to switch my package.json
to module
:
- I get
SyntaxError: Named export 'prompt' not found. The requested module 'prompt-sync' is a CommonJS module, which may not support all module.exports as named exports.
when I try to import like this:import { prompt } from "prompt-sync";
. - I get
ReferenceError: require is not defined in ES module scope, you can use import instead
if I try to import it like this:var prompt = require("prompt-sync")();
How can I use both those packages in the same project?
Share Improve this question edited May 22, 2023 at 18:20 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked May 22, 2023 at 12:26 J4NJ4N 20.8k42 gold badges222 silver badges384 bronze badges 01 Answer
Reset to default 6From within a ES module ("type": "module"
):
You want to do import prompt from 'prompt-sync';
.
While you can fully import monjs modules from ES modules, there are no named exports. So you can only import the default export. Either of these lines work and do the same:
import { default as prompt} from 'prompt-sync';
import prompt from 'prompt-sync';
This is because an monjs module has only one export.
From within a monjs module
To import a ES module you need to do dynamic import
, as stated by the error message:
const loadJsonFileModule = await import('load-json-file');
import()
return a Promise, resolving to a module. So you need to call it from within an async function, or use .then
to resolve the promise.
Then loadJsonFileModule
is a module, wrapping all the exports, not the default export. So you will need to do something like loadJsonFileModule.loadJsonFile(...)
.
本文标签: javascriptHow to resolve require vs import in a Nodejs scriptStack Overflow
版权声明:本文标题:javascript - How to resolve require vs. import in a Node.js script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744019825a2577013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论