<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Le mar. 3 mai 2022 à 13:56, Yadd <<a href="mailto:yadd@debian.org">yadd@debian.org</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 03/05/2022 13:15, Pirate Praveen wrote:<br>
> <br>
> <br>
> On ചൊ, മേയ് 3 2022 at 06:41:01 വൈകു +0800 +0800, Ying-Chun Liu (PaulLiu) <br>
> <<a href="mailto:paulliu@debian.org" target="_blank">paulliu@debian.org</a>> wrote:<br>
>> Hi Praveen,<br>
>><br>
>>  Sorry for bothering but I just got lots of questions about using ES <br>
>> modules and I'm not sure who to ask. I tried to read the history of <br>
>> Debian wiki pages about ES modules and found you.<br>
>><br>
>><br>
>> Recently I've updated node-leven package to the latest upstream <br>
>> version and it becomes an ES module now.<br>
>>  To use ES modules, I wrote a simple "mjs" script file:<br>
>><br>
>> import leven from '/usr/share/nodejs/leven/index.js';<br>
>>  console.log(leven('test', 'test2')); // output: 1<br>
>><br>
>><br>
>> The first question is why I cannot use "import leven from 'leven';"?<br>
>><br>
> I think that is how ES module standard is supposed to work. You can add <br>
> a symlink to node_modules<br>
> <br>
> mkdir -p node_modules<br>
> ln -s /usr/share/nodejs/leven node_modules<br>
> <br>
> There is debian/nodejs/extlinks in pkg-js-tools to help with this <br>
> symlink creation.<br>
<br>
or by hand `pkgjs-ln leven`<br>
<br>
> I think Jeremy Lal mentioned in the past that global path for ES modules <br>
> will not be supported.<br>
>> I got ERR_MODULE_NOT_FOUND if I don't use the full path.<br>
>><br>
>> But packages like assert works without full path.<br>
>>  Not sure if I got things wrong.<br></blockquote><div><br></div><div>If you can import such a module like this:<br></div><div>require('node:assert')</div><div>or</div><div>import('node:assert')</div><div><br></div><div>it means it's a core, embedded, node module.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">>> The second question is more complicated.<br>
>>  I tried to use leven from a CJS module.<br>
>>  It works if I use dynamic import with full path:<br>
>><br>
>> var leven;<br>
>>  async function test() {<br>
>>     leven = await import ('/usr/share/nodejs/leven/index.js');<br>
>>     leven = leven.default;<br>
>>     console.log(leven('test', 'test1')); // output 1<br>
>>  }<br>
>>  test();<br>
>><br>
>>  However, it seems to me that with CJS, there's no top-level await.<br>
>>  So I can only wrapping things in an async function.<br>
<br>
Jest, await is available only in async functions. But for a simle <br>
script, you can use zx which accepts await<br></blockquote><div><br></div><div>Nota Bene: nodejs 16 supports top-level await in ES module:</div><br>import { readFile } from 'node:fs/promises'<br><div>console.log(await readFile('test.txt'))</div><div><br></div><div>will work if placed into a test.mjs file or if package.json has type:'module' and file is test.js.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">>>  But this is a bit trouble. How can I simply convert require('leven') <br>
>> used in node-commist if there's no top-level await?<br>
<br>
If leven is a pure ES module, you can build CJS files using `mjs2cjs -a` <br>
in your debian/rules (it requires rollup and <br>
node-rollup-plugin-node-resolve). This will modify package.json and <br>
build a commonjs file, usable with require (tested, it works fine)<br></blockquote><div><br></div><div>node -e 'require("leven")'</div><div>or</div><div>pkgjs-ln leven</div><div>node -e 'import("leven")'</div><div><br></div><div>both already work.</div><div>(See remark about extlinks above too)</div></div></div>