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

genSalt not working on nodejs esm #150

Open
randrei12 opened this issue Oct 19, 2023 · 0 comments
Open

genSalt not working on nodejs esm #150

randrei12 opened this issue Oct 19, 2023 · 0 comments

Comments

@randrei12
Copy link

When I try to use genSalt function on es modules I get the following error: Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative. It works on commonjs.

I took a look into the bcryptjs source code and I found this:

function random(len) {
    /* node */ if (typeof module !== 'undefined' && module && module['exports'])
        try {
            return require("crypto")['randomBytes'](len);
        } catch (e) {}
    /* WCA */ try {
        var a; (self['crypto']||self['msCrypto'])['getRandomValues'](a = new Uint32Array(len));
        return Array.prototype.slice.call(a);
    } catch (e) {}
    /* fallback */ if (!randomFallback)
        throw Error("Neither WebCryptoAPI nor a crypto module is available. Use bcrypt.setRandomFallback to set an alternative");
    return randomFallback(len);
}

I believe the problem is that module['exports'] parameter, which, of course, doesn't exists in esm nodejs. I think a better verification method is to check if process object exists. Please take a look on this issue, thanks!

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

1 participant