kevlened / isomorphic-webcrypto

:game_die: webcrypto library for Node, React Native and IE11+
https://www.w3.org/TR/WebCryptoAPI/
MIT License
116 stars 42 forks source link

Unable to run in NodeJS environment #14

Closed Manbearpixel closed 5 years ago

Manbearpixel commented 5 years ago

Great project, I noticed in the specs that Node 4+ should be supported. My code appears as follows:

import * as webcrypto from "isomorphic-webcrypto";

export class Core implements CoreDef {
  public static test(): string {
    return webcrypto.getRandomValues(new Uint8Array(16)).toString();
  }
}

When ran however, the following error appears:

System.err: ReferenceError: window is not defined
System.err: File: "msrcrypto/dist/msrcrypto.js, line: 8287, column: 0

So it appears one of the dependencies your project uses still relies on the window object which would not be available in Node. Do you have some examples of this working with Node?

Thanks

kevlened commented 5 years ago

There are tests in Node to ensure it works: https://travis-ci.com/kevlened/isomorphic-webcrypto/builds/86928885

It looks like you may be running your code through a transpiler. Are you using Typescript?

msrcrypto is only loaded for react-native. I'd check if your transpiler is configured for react-native. A configuration for react-native would look for *.ios.js and *.android.js files.

kevlened commented 5 years ago

Closing for now. Feel free to reply if this is still an issue.