Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support modules in NodeJS #201

Open
benrobson opened this issue Jan 8, 2022 · 11 comments
Open

Support modules in NodeJS #201

benrobson opened this issue Jan 8, 2022 · 11 comments

Comments

@benrobson
Copy link

Hey,
I've recently refactored my project from commonjs to modules and since then WOKCommands refuses to work.
Here is my error: https://sourceb.in/BBobXoD2Jr

It seems the error is in node_modules\wokcommands\dist\FeatureHandler.js:37
I would love to use this package but it refuses to work and wouldn't want to edit the package.

@imranbarbhuiya
Copy link

This can be solved with dynamic import, which is supported in both cjs and esm. Lmk if anyone have already started to work on it, else I will be happy to create a pr to fix this.

@benrobson
Copy link
Author

benrobson commented Jan 10, 2022 via email

@imranbarbhuiya
Copy link

How would I dynamic import with type module? Get Outlook for Androidhttps://aka.ms/AAb9ysg

________________________________ From: Parbez @.> Sent: Monday, January 10, 2022 1:34:57 PM To: AlexzanderFlores/WOKCommands @.> Cc: Ben Robson @.>; Author @.> Subject: Re: [AlexzanderFlores/WOKCommands] Support modules in NodeJS (Issue #201) This can be solved with dynamic import, which is supported in both cjs and esm. Lmk if anyone have already started to work on it, else I will be happy to create a pr to fix this. — Reply to this email directly, view it on GitHub<#201 (comment)>, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADVRDWD7JYE4KNMGO5JJOZTUVJAVDANCNFSM5LQI7LPA. You are receiving this because you authored the thread.Message ID: @.***>

The issue is present in this lib not in your code. So you can create a pull request to solve the issue. You just need to change these require to import. It'll return a promise with an object like this Promise<{default: any}>. You need to change it as required.

@benrobson
Copy link
Author

benrobson commented Jan 10, 2022 via email

@benrobson
Copy link
Author

I have attempted to look into this and changed wokcommands\dist\FeatureHandler.js:37 to: this.registerFeature(await import(file), fileName); which gave me this error:

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs 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:371:5)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:839:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at Loader.import (node:internal/modules/esm/loader:177:28)
    at importModuleDynamically (node:internal/modules/cjs/loader:1041:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:21)
    at importModuleDynamically (node:vm:381:43)
    at exports.importModuleDynamicallyCallback (node:internal/process/esm_loader:30:14)
    at FeatureHandler.setup (C:\Users\Ben\OneDrive\Documents\GitHub\ModularEnigma-zander-web\node_modules\wokcommands\dist\FeatureHandler.js:37:51) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

@leonardobagi
Copy link

Revert all changes you made, and if your error is "ESM_MODULE_REQUIRE" or something of that sort, just remove "type": "module" from package.json.

Hey, I've recently refactored my project from commonjs to modules and since then WOKCommands refuses to work. Here is my error: https://sourceb.in/BBobXoD2Jr

It seems the error is in node_modules\wokcommands\dist\FeatureHandler.js:37 I would love to use this package but it refuses to work and wouldn't want to edit the package.

@imranbarbhuiya
Copy link

Revert all changes you made, and if your error is "ESM_MODULE_REQUIRE" or something of that sort, just remove "type": "module" from package.json.

Hey, I've recently refactored my project from commonjs to modules and since then WOKCommands refuses to work. Here is my error: https://sourceb.in/BBobXoD2Jr
It seems the error is in node_modules\wokcommands\dist\FeatureHandler.js:37 I would love to use this package but it refuses to work and wouldn't want to edit the package.

He wants to work with module.

@benrobson
Copy link
Author

Correct, and WOKCommands is not working with module, so I am looking to see how to make it universal between the 2.

@imranbarbhuiya
Copy link

I have attempted to look into this and changed wokcommands\dist\FeatureHandler.js:37 to: this.registerFeature(await import(file), fileName); which gave me this error:

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs 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:371:5)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:839:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at Loader.import (node:internal/modules/esm/loader:177:28)
    at importModuleDynamically (node:internal/modules/cjs/loader:1041:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:21)
    at importModuleDynamically (node:vm:381:43)
    at exports.importModuleDynamicallyCallback (node:internal/process/esm_loader:30:14)
    at FeatureHandler.setup (C:\Users\Ben\OneDrive\Documents\GitHub\ModularEnigma-zander-web\node_modules\wokcommands\dist\FeatureHandler.js:37:51) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

await import sends {default: value} so u need to grab the default value. I'll try it if I get some time.

@leonardobagi
Copy link

I have attempted to look into this and changed wokcommands\dist\FeatureHandler.js:37 to: this.registerFeature(await import(file), fileName); which gave me this error:

node:internal/errors:464
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs 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:371:5)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:839:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at Loader.import (node:internal/modules/esm/loader:177:28)
    at importModuleDynamically (node:internal/modules/cjs/loader:1041:29)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:21)
    at importModuleDynamically (node:vm:381:43)
    at exports.importModuleDynamicallyCallback (node:internal/process/esm_loader:30:14)
    at FeatureHandler.setup (C:\Users\Ben\OneDrive\Documents\GitHub\ModularEnigma-zander-web\node_modules\wokcommands\dist\FeatureHandler.js:37:51) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

await import sends {default: value} so u need to grab the default value. I'll try it if I get some time.

In this case try using the path.join(__dirname, "commands") or fileURLToPath, if I understood well the error

@Kruimeldief
Copy link

It's not really the solution you want but with PR #209 you can use modules except for the commands themselves, which would be .cjs (.cts). It's just a question of whether it'll be merged. If not, you can change the single line of code locally in the node_modules directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants