Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process is not defined error with angular 12 [Bug] #284

Open
nileshzala005 opened this issue May 18, 2021 · 15 comments
Open

process is not defined error with angular 12 [Bug] #284

nileshzala005 opened this issue May 18, 2021 · 15 comments
Labels
bug Something isn't working

Comments

@nileshzala005
Copy link

nileshzala005 commented May 18, 2021

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
- install 'util'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "util": false }

With Angular 12, this is working fine in Angular 11

Angular CLI: 12.0.0
Node: 14.15.4
Package Manager: npm 6.14.10
OS: darwin x64

Angular:
...

Package Version

@angular-devkit/architect 0.1200.0 (cli-only)
@angular-devkit/core 12.0.0 (cli-only)
@angular-devkit/schematics 12.0.0 (cli-only)
@schematics/angular 12.0.0 (cli-only)

If install util js via npm install util
build error is removed but at runtime getting this error.
util.js:109 Uncaught ReferenceError: process is not defined
at Object.1732 (util.js:109)

internally util js is used by twilio-client sdk
if (process.env.NODE_DEBUG) {
var debugEnv = process.env.NODE_DEBUG;
debugEnv = debugEnv.replace(/[|\{}()[]^$+?.]/g, '\$&')
.replace(/*/g, '.*')
.replace(/,/g, '$|^')
.toUpperCase();
debugEnvRegex = new RegExp('^' + debugEnv + '$', 'i');
}

here at the client-side its try to get process object that is node server object. this is causing the issue in angular 12

@nileshzala005 nileshzala005 added the bug Something isn't working label May 18, 2021
@nileshzala005 nileshzala005 changed the title [BUG] process is not defined error with angular 12 [Bug] May 18, 2021
@msms007
Copy link

msms007 commented Jul 13, 2021

any updates or work around to this? still struggling to find a fix

@pgarzina
Copy link

experiencing the same issue, and is a blocker in updating to Angular 12.

@pgarzina
Copy link

@nileshzala005 @msms007 have you guys found a working workaround maybe? :)

@nileshzala005
Copy link
Author

@pgarzina instead of NPM packages use CDN version or update to twilio 2.0 version

@pgarzina
Copy link

@pgarzina instead of NPM packages use CDN version or update to twilio 2.0 version

thanks for the reply, we did migrate to 2.0 (@twilio/voice-sdk": "^2.0.1", - https://github.com/twilio/twilio-voice.js) but the error appears there as well (I tried it out on a blank angular 12.1.2 project).

I will try the CDN version as you suggested. thanks

@huannxdev
Copy link

Hi @pgarzina , Have you resolved your problem?. I got this when upgrading to angular 12

@pgarzina
Copy link

pgarzina commented Sep 27, 2021

Hey @huannxdev
we have not resolved the problem, we rolled back to angular 11 until Twilio fixes it - which will potentially take a few months.

there is a suggested workaround here twilio/twilio-voice.js#28 (comment)
which we didn't have time to try out yet, but hopefully it works.

@huannxdev
Copy link

huannxdev commented Sep 27, 2021

Yeah, i resolved this by steps, you can try :

  • npm i util
  • add code into polyfill.ts file
    (window as any).process = { env: { DEBUG: undefined }, };

@ashish-koshy
Copy link

ashish-koshy commented Jan 31, 2022

I hope this helps someone with this issue. I created a sample boilerplate ionic-angular project that can load content from your .env file:
https://github.com/ashish-koshy/ionic-ng-pwa

Please do remember to create a .env file as it is not a part of this repo.
The value of ENVIRONMENT_NAME set in the .env file should reflect in the home component:

ENVIRONMENT_NAME=local

@Jeff-Stapleton
Copy link

Yeah, i resolved this by steps, you can try :

  • npm i util
  • add code into polyfill.ts file
    (window as any).process = { env: { DEBUG: undefined }, };

If I am not mistaken this will simply define process with the object you are giving it -- it still won't read your environment variables

@ashish-koshy
Copy link

ashish-koshy commented Jul 19, 2022

@Jeff-Stapleton
Yes that's correect, this method alone would not work. The intent behind performing such a hack is for us to be able to avoid a browser/DOM exception when the browser tries to dereference an object called process.env. The problem here is that this object does not or should not exist within the context of a browser being used by a client.

process.env object only makes sense within the context of NodeJS which is a server side language having direct access to the operating system its running on as opposed to the JavaScript code running within a client's browser. The process.env object allows NodeJS developers to load values specific to the environment they deploy their server side code to.

Now, how or why the process.env object is being dereferenced in this manner by twilio's client side library is unknown to me. This is a question that has to be answered or fixed by the twilio developers.

You need to perform a few additional steps to actually make this work. You have to manually provide the process.env object to the browser before it tries to execute any of your javascript code. Again, keep in mind that this is a dirty hack that twilio developers shouldn't be expecting us to perform :

  1. Install the dotenv package as a dev dependency npm install --save-dev dotenv
  2. Create a file called .env at the root of your project and add some dummy key value pairs seperated by the = symbol (the non existent ones that twilio's code is trying to blindly access):

image

  1. Add the .env file to your .gitignore (you don't want to commit this file) :

image

  1. Now add another file at the root of your project called webpack.config.js :

image

You can find the code here:
https://github.com/ashish-koshy/ionic-ng-pwa/blob/main/webpack.config.js

In this step you are asking the webpack module bundler to bundle a pseudo object called process.env at build time so that the browser would eventually be able to read it at run time without throwing exceptions. The key value pairs for the process.env object would get picked from within the .env file you just created above

  1. Then, in your angular.json file add the setting that allows you to load custom webpack configuration at build time:

image

You can find the code here:
https://github.com/ashish-koshy/ionic-ng-pwa/blob/main/angular.json

And finally for the love of god, please do not use this hack in your production code. Keep the .env file listed within your .gitignore

@Hitsaa
Copy link

Hitsaa commented Nov 17, 2022

I resolved the issue by adding

(window as any).process = {
env: { DEBUG: undefined },
};

in polyfill.ts file.

@elenitaex5
Copy link

I've found the real solution. This problem is in util library (browserify/node-util) having webpack 5
Real solution is here browserify/node-util#57 (comment)

And no file has to be changed except webpack.config.js once

@davidecarpini
Copy link

davidecarpini commented Mar 25, 2024

if you need to deploy Angular 6 with serve:

polyfills.ts

(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
(window as any).process = {
    env: { DEBUG: undefined },
    version: '', // to avoid undefined.slice error
};

@13601101
Copy link

Actually im getting below error after adding above code in polyfills.ts

An error was thrown in afterAll
Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
TypeError: Cannot read properties of undefined (reading 'prototype')
at Object.18606 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/express/lib/response.js:43:45)
at require (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Object.72540 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/express/lib/express.js:22:11)
at module (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Object.44202 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/express/index.js:11:1)
at webpack_require (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Module.56938 (http://localhost:9876/_karma_webpack_/main.js:189477:65)
at webpack_require (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at webpack_exec (http://localhost:9876/_karma_webpack_/main.js:869311:48)
at http://localhost:9876/_karma_webpack_/main.js:869312:2772
Chrome 124.0.0.0 (Windows 10): Executed 51 of 462 (1 FAILED) ERROR (0.683 secs / 0.593 secs)
Chrome 124.0.0.0 (Windows 10) ERROR
An error was thrown in afterAll
Uncaught TypeError: Cannot read properties of undefined (reading 'prototype')
TypeError: Cannot read properties of undefined (reading 'prototype')
at Object.18606 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/express/lib/response.js:43:45)
at require (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Object.72540 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/express/lib/express.js:22:11)
at module (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Object.44202 (http://localhost:9876/_karma_webpack_/webpack:/node_modules/express/index.js:11:1)
at webpack_require (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at Module.56938 (http://localhost:9876/_karma_webpack_/main.js:189477:65)
at webpack_require (http://localhost:9876/_karma_webpack_/webpack:/webpack/bootstrap:19:1)
at webpack_exec (http://localhost:9876/_karma_webpack_/main.js:869311:48)
at http://localhost:9876/_karma_webpack_/main.js:869312:2772

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants