简体   繁体   English

NodeJS 中的实验性 ES 模块加载是否支持导入命名导出?

[英]Does experimental ES Module loading in NodeJS support importing named exports?

See the following import :请参阅以下import

import { publishObjectAsync, consumeObjectsAsync, createChannelAsync } from "../shared/messaging/rabbitmq"

Which produces this error:产生此错误:

SyntaxError: The requested module does not provide an export named 'publishObjectAsync' SyntaxError: 请求的模块不提供名为“publishObjectAsync”的导出

What's going on?这是怎么回事?

Oops, sorry, It was my fault but I believe that it's worth the effort providing my own answer so others may encounter the same problem and they can get a hint!哎呀,对不起,这是我的错,但我相信提供我自己的答案是值得的,这样其他人可能会遇到同样的问题,他们可以得到提示!

The code file extensions within /shared weren't renamed to .mjs !!!!! /shared中的代码文件扩展名没有重命名为.mjs !!!!!!

So, if you find this issue you should check if the export exists in the target module or if the imported module has the Michael Jackson file extension (.mjs).因此,如果您发现此问题,您应该检查导出是否存在于目标模块中,或者导入的模块是否具有 Michael Jackson 文件扩展名 (.mjs)。

That was the issue.这就是问题所在。 It supports alisaes all the way!它一直支持别名!

The syntax you used for aliases is right - so I would assume experimental ES Module loading does not support importing specific exports with aliases.您用于别名的语法是正确的 - 所以我假设实验性 ES 模块加载不支持导入带有别名的特定导出。

You could try to load all exports with an alias, though:不过,您可以尝试使用别名加载所有导出:

import * as rabbitmq from "rabbitmq";

export const createChannelAsync = () => rabbitmq.createChannelAsync(createConnectionAsync);

Or you could try if the export syntax supports aliases.或者您可以尝试导出语法是否支持别名。

export { name1 as default, … };

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export for further information on the syntax.请参阅https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/importhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements /export以获取有关语法的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 导入es6模块时,某些命名导出未定义 - Importing es6 module, some named exports are undefined 导入模块 NodeJS 中的所有导出 - Importing all exports in a module NodeJS ES模块:将命名导出作为模块导入吗? - ES Modules: import named exports as module? 正在通过以下方式加载 JavaScript ES6 模块<script src=“”> and importing all exports in a <script> tag the same? - Are loading a JavaScript ES6 module via <script src=“”> and importing all exports in a <script> tag the same? TypeScript & NodeJS:ES 模块中未定义导出 scope - TypeScript & NodeJS: Exports is not defined in ES module scope 将 CommonJS 默认导出导入为命名导出/无法加载 ES 模块 - Import CommonJS default exports as named exports / Cannot load ES module SyntaxError:请求的模块“react-popper”应为 CommonJS 类型,不支持命名导出 - SyntaxError: The requested module 'react-popper' is expected to be of type CommonJS, which does not support named exports ES6默认和命名导出 - ES6 default and named exports 找不到命名的导出“类型”。 请求的模块“mongoose”是一个 CommonJS 模块,它可能不支持所有 module.exports 作为命名导出 - Named export 'Types' not found. The requested module 'mongoose' is a CommonJS module, which may not support all module.exports as named exports Node.js模块导出混乱 - Nodejs module exports confusion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM