Missing crypto module when building a project using js-ethereum-cryptography
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.
- Add
@vercel/nccpackageyarn add @vercel/ncc - Build the
keccak.tsfile.ncc build keccak.js -o bundle/index.js - This should have created a new directory named
bundlein the root folder with anindex.jsfile. Inspect the generatedindex.jsfile 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
};
})();
- 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.
any updates on this? also seeing this with node 18.16.0. thanks! :)
hi. I have the same trouble. Any workaround for this?
Same 'MODULE_NOT_FOUND' problem. Did anybody solved this?