esm-dev / esm.sh

A fast, smart, & global CDN for modern(es2015+) web development.
https://esm.sh
MIT License
2.95k stars 143 forks source link

Failed init cli on NPM #811

Open schlichtanders opened 3 months ago

schlichtanders commented 3 months ago

Failed init

I cannot initialize esm.sh with cli on npm

Error message

After running the code in npx reejs x https://esm.sh init in an already npm initialized repo I got this:

 INFO  Saving important data...
 TIP  If the error in your code is in any of the following extensions (.ts, .tsx, .jsx), kindly not focus on the line number as the line numbers depict the compiled code and not the original one. Add `DEBUG=true` to your environment variables to see the original code.
ReferenceError: Deno is not defined
    at https://esm.sh/|Node/v20.11.1 (reejs/0.17.0):434:1
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async URLImport (file:///home/myhome/myproject/node_modules/@reejs/imports/URLImport.js:24:26)
    at async file:///home/myhome/myproject/node_modules/reejs/src/cli/cmds/x.js:118:22

After running reejs x https://esm.sh init in a new empty folder, I got this

[REEJS]  🛠️  Setting Up for first time.../sade.bundle.mjs
✓ https://esm.sh/v132/sade@1.8.1/node/sade.bundle.mjs in 1.3s
✓ https://esm.sh/ in 0.301s
 INFO  Saving important data...
 TIP  If the error in your code is in any of the following extensions (.ts, .tsx, .jsx), kindly not focus on the line number as the line numbers depict the compiled code and not the original one. Add `DEBUG=true` to your environment variables to see the original code.
ReferenceError: Deno is not defined
    at file:///home/myhome/myproject/.reejs/cache/532fae.js:434:1
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async URLImport (file:///home/myhome/.npm-packages/lib/node_modules/reejs/node_modules/@reejs/imports/URLImport.js:24:26)
    at async file:///home/myhome/.npm-packages/lib/node_modules/reejs/src/cli/cmds/x.js:118:22

Additional info

$ npx reejs doctor  # from my pre initialized npm project       
[reejs] Doctor
[RUNTIME] node
[VERSION] 0.17.0
[NODE_VER] 20.11.1
[REEJS_CACHE] 2 files
[REEJS_DIR] /home/[REDACTED]/myproject/.reejs , /home/[REDACTED]/myproject/.reejs
[REEJS_DIR_EXISTS] false
[PWD] /home/[REDACTED]/myproject
[USER_AGENT] Node/v20.11.1 (reejs/0.17.0)
$ reejs doctor  # from the new empty folder           
[reejs] Doctor
[RUNTIME] node
[VERSION] 0.17.0
[NODE_VER] 20.11.1
[REEJS_CACHE] 0 files
[REEJS_DIR] /home/[REDACTED]/.npm-packages/lib/node_modules/reejs/.reejs , /home/[REDACTED]/.julia/dev/tmp/esm-test/.reejs
[REEJS_DIR_EXISTS] false
[PWD] /home/[REDACTED]/myproject
[USER_AGENT] Node/v20.11.1 (reejs/0.17.0)
renhiyama commented 3 months ago

Hi @schlichtanders thanks for reporting this issue. Reejs had removed support for Deno polyfill a few versions ago, due to unintended side affects. To intentionally enable deno support, please add --deno parameter at the end of your reejs command. (Since you're using npx, there's need of addititonal "--" before that parameter, in order to pass the additional parameter to reejs cli)

I currently tried that and then the deno polyfills loads up, but fails with the following error:

 INFO  Saving important data...
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/data/data/com.termux/files/usr/lib/node_modules/reejs/.reejs/cache/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):109:13666
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):109:458
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):110:3076
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):109:458
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):110:4066
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):109:458
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):110:59466
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):109:458
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):110:68331
    at https://esm.sh/v135/@deno/shim-deno@0.16.0/node/shim-deno.mjs|Node/v18.16.1 (reejs/0.17.0):109:458

I'll be debugging more of it later.

renhiyama commented 3 months ago

Here's my personal notice that explains this new error, I've already encountered it before: __dirname is defined in cjs, but not in mjs/esm. esm.sh currently still cannot polyfill/fix this.

From my end/Reejs' end - it tries to polyfill dirname at the start of each file where necessary. However I either disabled that support 2 months ago (that's when I last updated reejs) or reejs failed to polyfill `dirname` from its side too.

schlichtanders commented 3 months ago

I don't want to use deno, but want to use npm - hence I don't have deno installed

schlichtanders commented 3 months ago

In case the cli cannot be installed on npm, it would be great to update the central website documentation to reflect this

renhiyama commented 3 months ago

In case the cli cannot be installed on npm, it would be great to update the central website documentation to reflect this

Esm.sh's original service is to serve url imports, and is not a cli focused tool. The cli you see on deno & reejs - esm.sh only manages the importmaps file, but the underlying URL imports work is done by deno & reejs themselves.

You might want to look at jspm if you have problems running either of these two.

ije commented 3 months ago

FYI, in v136 the CLI script will be deprecated, instead a vscode extension will be released for import map updating(support index.html/importmap.json/deno.json)

image

schlichtanders commented 3 months ago

You might want to look at jspm if you have problems running either of these two.

My research already pointed me to jspm (took some time to find it). Thank you for the pointer, it really solves the cli part for import maps and also supports esm.sh