[Pkg-javascript-devel] Questions about using ES modules in CJS.

Ying-Chun Liu (PaulLiu) paulliu at debian.org
Tue May 3 11:41:01 BST 2022


Hi Praveen,

Sorry for bothering but I just got lots of questions about using ES 
modules and I'm not sure who to ask. I tried to read the history of 
Debian wiki pages about ES modules and found you.


Recently I've updated node-leven package to the latest upstream version 
and it becomes an ES module now.
To use ES modules, I wrote a simple "mjs" script file:

import leven from '/usr/share/nodejs/leven/index.js';
console.log(leven('test', 'test2')); // output: 1


The first question is why I cannot use "import leven from 'leven';"?

I got ERR_MODULE_NOT_FOUND if I don't use the full path.

But packages like assert works without full path.
Not sure if I got things wrong.


The second question is more complicated.
I tried to use leven from a CJS module.
It works if I use dynamic import with full path:

var leven;
async function test() {
    leven = await import ('/usr/share/nodejs/leven/index.js');
    leven = leven.default;
    console.log(leven('test', 'test1')); // output 1
}
test();

However, it seems to me that with CJS, there's no top-level await.
So I can only wrapping things in an async function.

But this is a bit trouble. How can I simply convert require('leven') 
used in node-commist if there's no top-level await?
I cannot wrap whole node-commist inside an async function because it 
should work like a library.

Yours,
Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-javascript-devel/attachments/20220503/6c981ef6/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: OpenPGP_signature
Type: application/pgp-signature
Size: 840 bytes
Desc: OpenPGP digital signature
URL: <http://alioth-lists.debian.net/pipermail/pkg-javascript-devel/attachments/20220503/6c981ef6/attachment.sig>


More information about the Pkg-javascript-devel mailing list