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

Jérémy Lal kapouer at melix.org
Tue May 3 13:24:16 BST 2022


Le mar. 3 mai 2022 à 13:56, Yadd <yadd at debian.org> a écrit :

> On 03/05/2022 13:15, Pirate Praveen wrote:
> >
> >
> > On ചൊ, മേയ് 3 2022 at 06:41:01 വൈകു +0800 +0800, Ying-Chun Liu (PaulLiu)
> > <paulliu at debian.org> wrote:
> >> 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 think that is how ES module standard is supposed to work. You can add
> > a symlink to node_modules
> >
> > mkdir -p node_modules
> > ln -s /usr/share/nodejs/leven node_modules
> >
> > There is debian/nodejs/extlinks in pkg-js-tools to help with this
> > symlink creation.
>
> or by hand `pkgjs-ln leven`
>
> > I think Jeremy Lal mentioned in the past that global path for ES modules
> > will not be supported.
> >> 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.
>

If you can import such a module like this:
require('node:assert')
or
import('node:assert')

it means it's a core, embedded, node module.

>> 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.
>
> Jest, await is available only in async functions. But for a simle
> script, you can use zx which accepts await
>

Nota Bene: nodejs 16 supports top-level await in ES module:

import { readFile } from 'node:fs/promises'
console.log(await readFile('test.txt'))

will work if placed into a test.mjs file or if package.json has
type:'module' and file is test.js.

>>  But this is a bit trouble. How can I simply convert require('leven')
> >> used in node-commist if there's no top-level await?
>
> If leven is a pure ES module, you can build CJS files using `mjs2cjs -a`
> in your debian/rules (it requires rollup and
> node-rollup-plugin-node-resolve). This will modify package.json and
> build a commonjs file, usable with require (tested, it works fine)
>

node -e 'require("leven")'
or
pkgjs-ln leven
node -e 'import("leven")'

both already work.
(See remark about extlinks above too)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://alioth-lists.debian.net/pipermail/pkg-javascript-devel/attachments/20220503/b46f19b3/attachment.htm>


More information about the Pkg-javascript-devel mailing list