angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Webpack Config is stripping out Crypto

Open jmc265 opened this issue 10 years ago • 73 comments

  1. OS?
    Mac OSX El Capitan
  2. Versions. angular-cli: local (v1.0.0-beta.10, branch: webpack)
    node: 4.4.7
    os: darwin x64
  3. Repro steps. I've pulled down the Angular-cli project and am building it locally, so I am using the new webpack version. The project that I am building uses Crypto libraries (via a dependency of a dependency ...). When I was running ng build I was seeing the dependency built like this:
    /* WEBPACK VAR INJECTION */(function(Buffer) {'use strict';

    var crypto = __webpack_require__(147);
    var BufferUtil = __webpack_require__(21);
    var $ = __webpack_require__(15);

    var Hash = module.exports;

    Hash.sha1 = function(buf) {
      $.checkArgument(BufferUtil.isBuffer(buf));
      return crypto.createHash('sha1').update(buf).digest();
    };
    ...

And then when I looked at module 147 (I.E. the crypto module) I was seeing nothing there:

/***/ },
/* 147 */
/***/ function(module, exports, __webpack_require__) {

/***/ },

After a while of searching I took a look in the Angular-CLI code and saw that part of the webpack config (addon/ng2/models/webpack-build-common.ts) was this:

node: {
      global: 'window',
      crypto: 'empty',
      module: false,
      clearImmediate: false,
      setImmediate: false
    }

When I remove crypto: 'empty' everything builds correctly with the crypto module getting pulled in.

So my question is, why have you specified that crypto should be empty? Is there any way to fix this more permanently?

jmc265 avatar Aug 04 '16 10:08 jmc265

Thank you Francois. This got the build to complete without errors. Many thanks. I hope our web architect approves of this. Seems okay to me. Is fs: 'empty' because it is a server-side library?

JoeKahl avatar Jan 02 '19 15:01 JoeKahl

Sure, I'm glad it works. It's because there is no browser version of fs, which is good ^^. So you cannot use fs method in a browser. It's like child_process.

GrandSchtroumpf avatar Jan 02 '19 16:01 GrandSchtroumpf

@GrandSchtroumpf's solution worked for me as well. But I'll just add to the pile and say that it would be great to see first-party support for this customization.

bjohnson-va avatar Jan 08 '19 23:01 bjohnson-va

@bjohnson-va there is first party support. please see the last paragraph of my comment above https://github.com/angular/angular-cli/issues/1548#issuecomment-415477535 for a non-webpack specific solution.

clydin avatar Jan 08 '19 23:01 clydin

this is how I got it working via post-install script in Angular 6 with Angular CLI

const fs = require('fs');
const f = 'node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js';

fs.readFile(f, 'utf8', function (err, data) {
    if (err) {
        return console.log(err);
    }
    var result = data.replace(/node: false/g, "node: {fs: 'empty', crypto: true, stream: true }");

    fs.writeFile(f, result, 'utf8', function (err) {
        if (err) return console.log(err);
    });
});

just add whatever you need inside "node: {fs: 'empty', crypto: true, stream: true }"

tisak avatar Jan 26 '19 08:01 tisak

@studds could you show your configuration for crypto path mapping in tsconfig.json? thanks

Jun711 avatar Mar 26 '19 02:03 Jun711

OK, status update for Angular 7.2.11 and crypto freaks. My current configuration that is running uses this:

  • follow the whole article pointed out above, because the comment left out important steps: https://codeburst.io/customizing-angular-cli-6-build-an-alternative-to-ng-eject-a48304cd3b21
  • put the following in your extra-webpack.config.js:
module.exports = {
  node: {
    vm: true,
    stream: true
  },
  resolve: {
    alias: {
      "crypto": "crypto-browserify"
    }
  }
}

This replaces the crypto module with the (mostly compatible) crypto-browserify. Works here, YMMV.

ineiti avatar Apr 02 '19 04:04 ineiti

@filipesilva I realize your comment is quite old but according to MDN every major browser is now supporting crypto and the dance around to fixing this is quite painful - any plans on rectifying this? https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

gnesher avatar May 22 '19 12:05 gnesher

If using the web crypto API, there is no action required within the CLI. It is purely a browser API much like the DOM or other web APIs.

clydin avatar May 22 '19 12:05 clydin

@clydin bcryptjs throws a warning Module not found: Error: Can't resolve 'crypto' in ... and I thought this is related - but it appears to be a harmless warning as it tries to access the node crypto module before reverting to the web crypto api - you can ignore my previous comment

gnesher avatar May 23 '19 06:05 gnesher

Hi @ineiti ,

Your solution worked fine for the ng build but I could not make it work for ng serve. It looks like it loads the config file but completely ignores the alias. Do you have any idea what could be the problem?

Baidaly avatar Aug 04 '19 01:08 Baidaly

To @Baidaly - currently I downgraded to tn == 5.1.1 and use nativescript-nodeify, which needs a small patch to run correctly with our library. Which is really unfortunate... I would like to have this problem solved in a nicer way.

ineiti avatar Aug 05 '19 07:08 ineiti

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.