Instantly share code, notes, and snippets.

Embed
What would you like to do?
Making web3/bitcore-lib work with Angular 6
{...
"scripts": {
"postinstall": "node patch.js",
...
}
}
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: {crypto: true, stream: true}');
fs.writeFile(f, result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
@paul-thomas0

This comment has been minimized.

Copy link

paul-thomas0 commented May 23, 2018

Please, try including fs: 'empty' in replace's second arguments for fixing the warning for fs module not found in zone.js

var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true, fs: 'empty'}');

Thanks you for the solution, it did fix the warning. However, are there any side-effects to this patch?

@krazygit

This comment has been minimized.

Copy link

krazygit commented May 24, 2018

@paul
You should update the patch according your need

for example

...
var result = data.replace(/node: false/g, 'node: {crypto: true, stream: true, fs: \'empty\', net: \'empty\'}');
@marvpaul

This comment has been minimized.

Copy link

marvpaul commented May 26, 2018

Same problem with ng6 and jspdf. This workaround solved the problem. Thanks a lot!

@janhocevar

This comment has been minimized.

Copy link

janhocevar commented May 29, 2018

Works!

@niespodd

This comment has been minimized.

Copy link
Owner Author

niespodd commented May 29, 2018

@paul-thomas0
This is a monkey-patching type of fix and you should use it only temporarily. Also make sure to fix Angular version, because any new version may break it again.

Don't forget to upvote angular/angular-cli#10954

@postpersonality

This comment has been minimized.

Copy link

postpersonality commented May 29, 2018

var result = data.replace(/node: false/g, "node: {fs: 'empty', global: true, crypto: 'empty', tls: 'empty', net: 'empty', process: true, module: false, clearImmediate: false, setImmediate: false}");

Reverts angular/devkit@8e7658a#diff-085e357d25d94ae495a662c157178fb0L103

@swbo101

This comment has been minimized.

Copy link

swbo101 commented May 31, 2018

Sorry, I'm new to Angular. Quick question. Where do I place the patch.js file and will that get run when I run ng serve or how does it get invoked?
Thanks

@trungngo1508

This comment has been minimized.

Copy link

trungngo1508 commented May 31, 2018

@swbo101
place it @ the same level of package.json file

@a-faraz

This comment has been minimized.

Copy link

a-faraz commented Jun 1, 2018

@swboi101

It will get run every time you do a yarn install or npm install since it is a postinstall script.

@swbo101

This comment has been minimized.

Copy link

swbo101 commented Jun 6, 2018

@trungngo1508 & @a-faraz

Thanks, much appreciated!

@gus-canya

This comment has been minimized.

Copy link

gus-canya commented Jun 26, 2018

@niespodd You rock!

@TomWhiteOmni

This comment has been minimized.

Copy link

TomWhiteOmni commented Jun 29, 2018

@niespodd
Excellent patch!

@nitin7dc

This comment has been minimized.

Copy link

nitin7dc commented Jul 5, 2018

Thanks!

@MM365

This comment has been minimized.

Copy link

MM365 commented Jul 25, 2018

How can I run a script/patch like this while deploying to heroku? Locally it works fine, but while deploying I get the same issues as before. Thank you guys!

@LNga

This comment has been minimized.

Copy link

LNga commented Aug 2, 2018

Thanks!

@cin3rama

This comment has been minimized.

Copy link

cin3rama commented Aug 5, 2018

@niespodd
I love you man...great patch

@Kotomanga

This comment has been minimized.

Copy link

Kotomanga commented Aug 10, 2018

You rocks
Thanks !!!

@Parsh

This comment has been minimized.

Copy link

Parsh commented Aug 18, 2018

Thanks a tonn!!!

@cozzbie

This comment has been minimized.

Copy link

cozzbie commented Aug 27, 2018

Not sure why this is not working. But good stuff.

@jdeg

This comment has been minimized.

Copy link

jdeg commented Sep 2, 2018

Yeah, it didn't work for me.

@mlkong

This comment has been minimized.

Copy link

mlkong commented Sep 4, 2018

Works!

@JeffGos

This comment has been minimized.

Copy link

JeffGos commented Sep 7, 2018

If this doesn't work for you just run 'node patch.js' from the command line once everything else has installed and that will apply the patch for you.

@literalpie

This comment has been minimized.

Copy link

literalpie commented Sep 28, 2018

For anyone who wants an explanation on what this patch does:

It simply goes to the file whose path is in line 2 (which is a web pack config) and it replaces node: false with node: {crypto: true, stream: true}

@NikitaTkachev

This comment has been minimized.

Copy link

NikitaTkachev commented Oct 19, 2018

Thanks a lot!

@pointtoken

This comment has been minimized.

Copy link

pointtoken commented Oct 25, 2018

You rule!

@Sabrinsulthana

This comment has been minimized.

Copy link

Sabrinsulthana commented Nov 5, 2018

@literalpie
Hi guys,
This works fine for me. Just get into the node-modules, and move into the browser.js file and change the node: {crypto: true, stream: true}
---node_modules/angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js---
Thanks!!!

@ClaudeSutterlin

This comment has been minimized.

Copy link

ClaudeSutterlin commented Nov 19, 2018

This same fix worked for the jsforce library.

@maxim2muchcoffee

This comment has been minimized.

Copy link

maxim2muchcoffee commented Nov 20, 2018

Still works for angular 7.0.0 with web3 v.1.0.0-beta.36, thank you

@Komal-bansal

This comment has been minimized.

Copy link

Komal-bansal commented Nov 24, 2018

Thank you so much, i was struggling with this error from last two days.

@SamuelMarks

This comment has been minimized.

Copy link

SamuelMarks commented Nov 29, 2018

Thanks, worked with Angular 7.1 with js-git.

@markvdesign

This comment has been minimized.

Copy link

markvdesign commented Dec 26, 2018

I was having trouble getting this to work but after I deleted the node_modules folder and run npm install again it worked.

@AaronBastian

This comment has been minimized.

Copy link

AaronBastian commented Jan 17, 2019

awesome. thank you!!!

@savefolla

This comment has been minimized.

Copy link

savefolla commented Jan 24, 2019

Thanks a lot!

@imvikaskohli

This comment has been minimized.

Copy link

imvikaskohli commented Jan 31, 2019

It also works for me. But I run node patch.js and then run the angular project. Thanks man

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment