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

Keystore package does not work in a Node ESM environment #909

Closed
camsjams opened this issue Apr 18, 2023 · 1 comment · Fixed by #1049
Closed

Keystore package does not work in a Node ESM environment #909

camsjams opened this issue Apr 18, 2023 · 1 comment · Fixed by #1049
Assignees
Labels
bug Issue is a bug

Comments

@camsjams
Copy link
Contributor

What version of fuels-ts are you using?

latest/ 0.39.0

Steps to Reproduce

  1. Create a Node esm project
  2. Attempt to load a fuels package or util that requires keystore
  3. The execution fails as require is not available

Expected Behavior

This code should also work in esm style environments.

This block needs to support require for legacy support but also somehow do dynamic import for ESM:

if (!selectedCrypto && typeof require === 'function') {
try {
// eslint-disable-next-line global-require
selectedCrypto = require('crypto');
selectedStrategy = 'Node';
} catch (error) {
// eslint-disable-next-line no-console
console.error('keystore expects a standard Web browser or Node environment.', error);
}
}

Actual Behavior

Crashes with error message:

keystore expects a standard Web browser or Node environment. Error: Dynamic require of "crypto" is not supported
    at file:///test-fuels/bg/node_modules/.pnpm/@[email protected]/node_modules/@fuel-ts/keystore/dist/index.mjs:1:231
    at file:///test-fuels/node_modules/.pnpm/@[email protected]/node_modules/@fuel-ts/keystore/src/universal-crypto.ts:25:22
@camsjams camsjams added the bug Issue is a bug label Apr 18, 2023
@arboleya
Copy link
Member

I stumbled upon this issue when debugging all the CJS/ESM shenanigans at:

I left some annotations with a possible fix here:

/**
* TODO: Add banner/code-snippet using `createRequire` for ESM support
*
* In the `esm` output, `require` will not be available, only `import`.
*
* We need to use `createRequire` in order to work around it.
*
* Places using a similar approach:
* https://github.com/evanw/esbuild/issues/946#issuecomment-814703190
* https://github.com/egoist/tsup/discussions/505#discussioncomment-3911142
*/

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

Successfully merging a pull request may close this issue.

2 participants