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

Viewed 484

See the following import:

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

Which produces this error:

SyntaxError: The requested module does not provide an export named 'publishObjectAsync'

What's going on?

2 Answers

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!!!!!

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).

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.

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.

Related