proofgeist/fmdapi

ERR_UNSUPPORTED_ESM_URL_SCHEME on codegen with Windows since v3

Closed this issue ยท 6 comments

Hi,
First of all, thank you for this library.

I am not sure if I am doing something wrong.
Since v3 the codegen script does not work on my windows machine.
Here is the powershell error output:

yarn run v1.22.19
$ C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\.bin\codegen
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file and data are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'
    at new NodeError (node:internal/errors:399:5)
    at throwIfUnsupportedURLScheme (node:internal/modules/esm/resolve:1059:11)
    at defaultResolve (node:internal/modules/esm/resolve:1135:3)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ESMLoader.import (node:internal/modules/esm/loader:525:22)
    at importModuleDynamically (node:internal/modules/cjs/loader:1186:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:429:21)
    at importModuleDynamically (node:internal/vm:106:46) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

Node.js v18.16.0

I tried adding an explicit path to the config file, but that did not work.
In my research I only found solutions where upgrading the node version was the solution.
But Node 18.16.0 should be sufficient, so I am really not sure.

Can you confirm you're on the latest release 3.0.9?

Since I don't have a windows machine available to me to test with, here's one other thing you can try:

Run the codegen --init command again. It will generate a new config file ending in .mjs and you can copy your config from the old .js file to the new one. Then try the codegen command again to see if you get the same error.

If that still doesn't work, you may delete the extra config file (doesn't matter which one) and try the beta version I just published:

npm i @proofgeist/fmdapi@3.0.10-beta.0

Yes i was trying with v3.0.9 and I had tried to regenerate with the .mjs config file but it did not work.

However, the 3.0.10 beta works with the .js files! Thank you!
When regenerating with yarn codgen --init (and .mjs extension) I now get a different error:

PS C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk> yarn codegen 
yarn run v1.22.19
$ C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\.bin\codegen
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk\fmschema.config.mjs not supported.
Instead change the require of C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk\fmschema.config.mjs to a dynamic import() which is available in all CommonJS modules.
    at C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\@proofgeist\fmdapi\dist\cli.js:55:22
    at Generator.throw (<anonymous>)
    at rejected (C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\@proofgeist\fmdapi\dist\cli.js:7:65) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v18.16.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I am glad to help test on windows if there is anything I can do.

Thanks for being willing to help!

Please try version 3.0.10-beta.2 and let me know if you are able to use both .js and .mjs files. Also, please share the line of the console that begins with ๐Ÿ” Reading config from after running the codegen command.

3.0.10-beta.2

  1. .js
PS C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk> yarn codegen
yarn run v1.22.19
$ C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\.bin\codegen
๐Ÿ” Reading config from \\?\C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk\fmschema.config.js
โœ… Generated schemas

Done in 2.08s.
  1. .mjs
PS C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk> yarn codegen
yarn run v1.22.19
$ C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\.bin\codegen
๐Ÿ” Reading config from \\?\C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk\fmschema.config.mjs
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "\\?\C:\Users\dhege\Desktop\GitFolder\sites-monorepo\apps\rgk\fmschema.config.mjs" is not a valid package name imported from C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\@proofgeist\fmdapi\dist\cli.js
    at new NodeError (node:internal/errors:399:5)
    at parsePackageName (node:internal/modules/esm/resolve:817:11)
    at packageResolve (node:internal/modules/esm/resolve:840:5)
    at moduleResolve (node:internal/modules/esm/resolve:938:20)
    at defaultResolve (node:internal/modules/esm/resolve:1153:11)
    at nextResolve (node:internal/modules/esm/loader:163:28)
    at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
    at ESMLoader.import (node:internal/modules/esm/loader:525:22)
    at importModuleDynamically (node:internal/modules/cjs/loader:1186:29) {
  code: 'ERR_INVALID_MODULE_SPECIFIER'
}

Node.js v18.16.0
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The //?/ part is weird.

On a completely different note, I got stuck on the codegen part the last couple of times, so I stopped testing and went back to v2.

The codegen part definitely worked with v3 because the layout fields were added to the schamas I added. However, when I try to use the generated clients I get the following error:

TypeError: (intermediate value).default is not a function
     at C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\@proofgeist\fmdapi\dist\client.js:83:78
     at Generator.next (<anonymous>)
     at fulfilled (C:\Users\dhege\Desktop\GitFolder\sites-monorepo\node_modules\@proofgeist\fmdapi\dist\client.js:5:58)
     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

I also noticed that the shared.json was not being updated. I then tried adding the fileTokenStore manually (I am running with NODE_ENV = development, so as far as I know this should not be necessary). But it still doesn't work. Am I doing something wrong?

I don't quite understand the //?/ part either, but from what I read it was supposed to fix something on Windows. I'll keep researching and let you know when I have a new beta to test.

In v3 the fileTokenStore method would be required if you want to use it. I kept running into issues with the code importing fs on environments that didn't support it so we decided to make the default memory only.

I believe 3.0.10-beta.3 will fix that error when using the generated client (you shouldn't have re-run codegen to test this). I realize the the default memory store doesn't need to be imported dynamically because it will be valid on any runtime.