ncc icon indicating copy to clipboard operation
ncc copied to clipboard

Missing crypto module when building a project using js-ethereum-cryptography

Open aiagentdev-1990 opened this issue 3 years ago • 3 comments

Issue Hi everyone, I have a project that is using a dependency that is dependent on the keccak file from [email protected] library. I then bundle my project into a single JS file using the ncc build command.

Now the issue is that ncc transforms this code to the code below as ncc can't find the crypto package for some reason:

/***/ 84310:
/***/ ((module) => {

function webpackEmptyContext(req) {
	var e = new Error("Cannot find module '" + req + "'");
	e.code = 'MODULE_NOT_FOUND';
	throw e;
}
webpackEmptyContext.keys = () => ([]);
webpackEmptyContext.resolve = webpackEmptyContext;
webpackEmptyContext.id = 84310;
module.exports = webpackEmptyContext;
/***/ }),

This leads to my project crashing with this error when I try run it:

Error: Cannot find module 'crypto'
    at Object.webpackEmptyContext (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:183577:10)
    at /Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:83270:43
    at Object.78777 (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:83273:3)
    at __nccwpck_require__ (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:183842:43)
    at Object.35681 (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:83194:17)
    at __nccwpck_require__ (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:183842:43)
    at Object.30038 (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:169615:16)
    at __nccwpck_require__ (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:183842:43)
    at Object.55944 (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:168123:14)
    at __nccwpck_require__ (/Users/christophersastropranoto/Documents/external-adapters-js/packages/sources/layer2-sequencer-health/bundle/index.js:183842:43) {
  code: 'MODULE_NOT_FOUND'
}

I've confirmed that this issue is also happening when I try to build the ethereum-cryptography package by itself.

Steps to repro

Run from the root of the ethereum-cryptography repository.

  1. Add @vercel/ncc package yarn add @vercel/ncc
  2. Build the keccak.ts file. ncc build keccak.js -o bundle/index.js
  3. This should have created a new directory named bundle in the root folder with an index.js file. Inspect the generated index.js file and find the code below
exports.crypto = (() => {
    const webCrypto = typeof self === "object" && "crypto" in self ? self.crypto : undefined;
    const nodeRequire =  true &&
        __nccwpck_require__(387).bind(module); ------>. The node ID might be different
    return {
        node: nodeRequire && !webCrypto ? nodeRequire("crypto") : undefined,
        web: webCrypto
    };
})();
  1. The module with ID 387 resolves to the code below
/***/ 387:
/***/ ((module) => {

function webpackEmptyContext(req) {
	var e = new Error("Cannot find module '" + req + "'");
	e.code = 'MODULE_NOT_FOUND';
	throw e;
}
webpackEmptyContext.keys = () => ([]);
webpackEmptyContext.resolve = webpackEmptyContext;
webpackEmptyContext.id = 387;
module.exports = webpackEmptyContext;

Expected Behavior

Running ncc build does not generate a webpackEmptyContext function when looking for the crypto module.

Versions

@vercel/ncc: 0.34.0

Any suggestions on what to check are appreciated! Thanks in advance.

aiagentdev-1990 avatar Oct 04 '22 01:10 aiagentdev-1990

any updates on this? also seeing this with node 18.16.0. thanks! :)

imgntn avatar May 15 '23 00:05 imgntn

hi. I have the same trouble. Any workaround for this?

israel-gonzalezmedina avatar Nov 15 '23 15:11 israel-gonzalezmedina

Same 'MODULE_NOT_FOUND' problem. Did anybody solved this?

bncdemo avatar Apr 03 '24 04:04 bncdemo