如何解决在Node.js脚本中的require与import的区别。

huangapple go评论74阅读模式
英文:

How to resolve require vs. import in a Node.js script

问题

I can help with the translation. Here's the content you provided:

我正在编写一个小的Node.js脚本,我想要在同一个文件中使用以下两个库:

但是我在如何同时导入这两个库方面遇到了困难。

如果我的package.json类型是commonJs

  • 当我尝试导入import { loadJsonFile } from "load-json-file"时,我会得到SyntaxError: Cannot use import statement outside a module
  • 如果我尝试使用require("load-json-file")()来加载,我会得到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.

现在,如果我尝试将package.json切换为module

  • 当我尝试像这样导入时:import { prompt } from "prompt-sync";,我会得到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.
  • 如果我尝试像这样导入:var prompt = require("prompt-sync")();,我会得到ReferenceError: require is not defined in ES module scope, you can use import instead

我该如何在同一个项目中使用这两个包?

英文:

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 commonJs:

  • I get SyntaxError: Cannot use import statement outside a module when trying to import import { 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 with require("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?

答案1

得分: 4

从ES模块内部 ("type": "module"):

你想要执行 import prompt from 'prompt-sync';
虽然你可以从ES模块完全导入CommonJS模块,但没有具名导出。所以你只能导入默认导出。以下任何一行都可以工作并且具有相同的效果:

import { default as prompt } from 'prompt-sync';
import prompt from 'prompt-sync';

这是因为CommonJS模块只有一个导出。

从CommonJS模块内部

要导入ES模块,你需要进行动态的 import,正如错误消息所述:

const loadJsonFileModule = await import('load-json-file');

import() 返回一个Promise,解析为一个模块。所以你需要从异步函数内部调用它,或者使用.then来解析Promise。

然后 loadJsonFileModule 是一个模块,包装了所有的导出,而不是默认导出。所以你需要做类似于 loadJsonFileModule.loadJsonFile(...) 这样的操作。

英文:

From within a ES module ("type": "module"):

You want to do import prompt from 'prompt-sync';.
While you can fully import commonjs 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 commonjs module has only one export.

From within a commonjs 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(...).

huangapple
  • 本文由 发表于 2023年5月22日 20:26:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306199.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定