问题
I've got a project which utilizes the openpgpjs library as a dependency, and I'm WebPacking up this whole application for deployment on an Azure Functions to resolve cold start issues.
The Azure functions work fine when running locally in a non-Webpacked format, but as soon as I WebPack up the files I keep getting the error at runtime:
"mscorlib: Error: Cannot find module 'crypto'"
The code which is triggering this error appears to be from Openpgpjs's util.js.
This whole file is here: https://github.com/openpgpjs/openpgpjs/blob/master/src/util.js
The offending code snipper is below:
/**
* Get native Node.js crypto api. The default configuration is to use
* the api when available. But it can also be deactivated with config.use_native
* @return {Object} The crypto module or 'undefined'
*/
getNodeCrypto: function() {
if (!this.detectNode() || !config.use_native) {
return;
}
return require('crypto');
},
For the life of me, I can't figure out why this is happening and I can't get the error to go away.
I'm using my own script pack-git to do the Webpacking, etc. However, I get the same behavior if I use Azure's function pack. (Which didn't exist when I started writing the code for master-pack.
As a result I think this has something to do with the way the library is playing with WebPack, but I'm not sure where to begin in figuring it out/troubleshooting.
Does anyone have any ideas on what is going wrong, or at least ideas to help me further isolate the problem?
------------------- EDIT ---------------------
I've created a simple project that reproduces the issue when run through Funcpack, which is using Webpack.
Reproduction Steps:
Install funcpack, and azure-functions-cli globally:
npm install -g funcpack azure-functions-cli
Clone the following repo:
git clone https://github.com/securityvoid/funcpack-bug.git
Run an npm install while in the folder you cloned into:
npm install
Start the Azure Function with azure-functions-cli from within the base of the cloned repository.
func run aaaa
Navigate to the following URL and validate you get a blank page back, which shows its working:
http://localhost:7071/Test
Stop the azure-functions-cli by closing the window.
Run funcpack in the base of the directory:
funcpack
Repeat steps 4-5
NOTE: This time you get an error about the module crypto not being found.
来源:https://stackoverflow.com/questions/44792770/mscorlib-error-cannot-find-module-crypto-after-using-webpack-on-an-applicati