mscorlib: Error: Cannot find module 'crypto' after using Webpack on an application

我与影子孤独终老i 提交于 2019-12-14 02:29:21

问题


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:

  1. Install funcpack, and azure-functions-cli globally:

    npm install -g funcpack azure-functions-cli

  2. Clone the following repo:

    git clone https://github.com/securityvoid/funcpack-bug.git

  3. Run an npm install while in the folder you cloned into:

    npm install

  4. Start the Azure Function with azure-functions-cli from within the base of the cloned repository.

    func run aaaa

  5. Navigate to the following URL and validate you get a blank page back, which shows its working:

    http://localhost:7071/Test

  6. Stop the azure-functions-cli by closing the window.

  7. Run funcpack in the base of the directory:

    funcpack

  8. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!