esm.sh icon indicating copy to clipboard operation
esm.sh copied to clipboard

Failed to import - js-sha256

Open ebramanti opened this issue 3 years ago • 4 comments

Failing module

import { sha256 } from "https://esm.sh/[email protected]?dev&no-check&target=deno"

Error message

After running deno run I got this:

error: Uncaught ReferenceError: require is not defined
        var crypto = eval("require('crypto')");
                     ^
    at eval (eval at nodeWrap (https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:152:22), <anonymous>:1:1)
    at nodeWrap (https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:152:22)
    at createMethod (https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:137:21)
    at https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:516:21
    at esm-build-82b454cebf87682b90e9c5a3fa3db93fdb261443-e0f5f3a5/node_modules/js-sha256/src/sha256.js (https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:532:7)
    at __require (https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:13:50)
    at https://cdn.esm.sh/v64/[email protected]/deno/js-sha256.development.js:537:32

Additional info

  • esm.sh version: v64
  • Deno version: 1.18.0

This was previously working on v57, but even with a pin it is now breaking on my local build. This is a sub-dependency of @project-serum/anchor.

ebramanti avatar Jan 24 '22 23:01 ebramanti

I've also identified the issue to be with this line: https://github.com/emn178/js-sha256/blob/master/src/sha256.js#L83

var crypto = eval("require('crypto')");

Because the require is contained within an eval it is unable to be rewritten by esm.sh. I also tried globally setting window.JS_SHA256_NO_NODE_JS but it was not evaluated properly before the code was executed.

ebramanti avatar Jan 24 '22 23:01 ebramanti

why not use deno std/hash module? ems.sh can't handle the eval case

ije avatar Jan 25 '22 19:01 ije

why not use deno std/hash module? ems.sh can't handle the eval case

This is an internal dependency to @project-serum/anchor as I mentioned in the issue above. It allows you to override this behavior with global variables on window but it doesn't seem to work when I try it from my entry point in Deno.

ebramanti avatar Jan 25 '22 20:01 ebramanti

there is a workaround maybe work:

import crypto from "https://esm.sh/crypto"
globalThis.require = (name) => {
  if (name == "crypto") return crypto
}

import "@project-serum/anchor"

ije avatar Jan 27 '22 09:01 ije