twilio-client.js
twilio-client.js copied to clipboard
process is not defined error with angular 12 [Bug]
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
any updates or work around to this? still struggling to find a fix
experiencing the same issue, and is a blocker in updating to Angular 12.
@nileshzala005 @msms007 have you guys found a working workaround maybe? :)
@pgarzina instead of NPM packages use CDN version or update to twilio 2.0 version
@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
Hi @pgarzina , Have you resolved your problem?. I got this when upgrading to angular 12
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 https://github.com/twilio/twilio-voice.js/issues/28#issuecomment-922833132 which we didn't have time to try out yet, but hopefully it works.
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 }, };
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
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
@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 :
- Install the
dotenv
package as a dev dependencynpm install --save-dev dotenv
- 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):
- Add the
.env
file to your.gitignore
(you don't want to commit this file) :
- Now add another file at the root of your project called
webpack.config.js
:
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
- Then, in your
angular.json
file add the setting that allows you to load custom webpack configuration at build time:
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
I resolved the issue by adding
(window as any).process = { env: { DEBUG: undefined }, };
in polyfill.ts file.
I've found the real solution. This problem is in util
library (browserify/node-util
) having webpack 5
Real solution is here https://github.com/browserify/node-util/issues/57#issuecomment-764436352
And no file has to be changed except webpack.config.js
once
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
};
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