/***************************************************************************************************
* Zone JS is required by default for Angular itself.
*/
import 'zone.js/dist/zone'; // Included with Angular CLI.
/***************************************************************************************************
* APPLICATION IMPORTS
*/
console.log('process: ', process);
console.log('global: ', global);
/***************************************************************************************************
* Zone JS is required by default for Angular itself.
*/
import 'zone.js/dist/zone'; // Included with Angular CLI.
/***************************************************************************************************
* APPLICATION IMPORTS
*/
console.log('process: ', process);
console.log('global: ', global);
问题
在
angular 6
项目中使用graphql-tools
的mockServer
方法时报:Uncaught ReferenceError: process is not defined
错误下图在浏览器控制台打印出了错误的函数调用堆栈,错误发生在
graphql
模块中环境及依赖版本
ng-cli
:package.json
-dependencies
原因
angular 6
开始,移除了process
和global
这些变量在浏览器环境中的垫片(shim),也就是说,在浏览器环境中的window对象上,已经没有这些变量了。先让我们回到
angular 5
时代在polyfills.ts
模块中打印process
和global
变量看看:同样的,在
angular 6
项目中的polyfills.ts
模块中打印process
和global
变量:可以看到
process
变量并不存在于window
对象上,所以这个错误和使用不使用graphql-tools
无关。解决方案:
一、在
polyfills.ts
文件中加入以下代码:二、通过webpack的DefinePlugin定义
process.env
变量待更新...
参考链接
https://stackoverflow.com/questions/50313745/angular-6-process-is-not-defined-when-trying-to-serve-application/50313953?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814
https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063