javascript Angular 6 - 尝试为应用程序提供服务时未定义进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50313745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 6 - process is not defined when trying to serve application
提问by Danoram
I am getting the following error when I try to serve my angular 6 application using cosmicjs:
当我尝试使用 cosmicjs 为我的 angular 6 应用程序提供服务时出现以下错误:
Uncaught ReferenceError: process is not defined
at Object../node_modules/cosmicjs/dist/index.js (index.js:6)
at __webpack_require__ (bootstrap:81)
at Object../src/app/app.component.ts (main.js:94)
at __webpack_require__ (bootstrap:81)
at Object../src/app/app.module.ts (app.component.ts:9)
at __webpack_require__ (bootstrap:81)
at Object../src/main.ts (environment.ts:18)
at __webpack_require__ (bootstrap:81)
at Object.0 (main.ts:12)
at __webpack_require__ (bootstrap:81)
My latest theory is something to do with the process.env property being undefined.
我的最新理论与 process.env 属性未定义有关。
I'm following this tutorial here
我在这里关注本教程
and my exact code can be found here
我的确切代码可以在这里找到
The tutorial seems to be using an older version of angular that uses .angular-cli.json instead of the new angular.json and I think this is part of the issue in trying to specify the environment variables.
本教程似乎使用了旧版本的 angular,它使用 .angular-cli.json 而不是新的 angular.json,我认为这是尝试指定环境变量时出现的问题的一部分。
回答by A T
In polyfill.ts, add this line:
在 中polyfill.ts,添加这一行:
(window as any).process = {
env: { DEBUG: undefined },
};
Reference: https://github.com/algolia/algoliasearch-client-javascript/issues/691
参考:https: //github.com/algolia/algoliasearch-client-javascript/issues/691
Or npm i -S process, then add this to polyfill.ts:
或者npm i -S process,然后将其添加到polyfill.ts:
import * as process from 'process';
window['process'] = process;
回答by Daniel Habenicht
This is an incompatibility with Angular v6. They removed support(shim) of processand globalvariables in browser.
这是与 Angular v6 的不兼容。他们删除了浏览器中对process和global变量的支持(垫片)。
I suggest you to use Angular 5, till cosmic.jsfixes the error. Maybe you can even open an issue for it.
我建议你使用 Angular 5,直到cosmic.js修复错误。也许你甚至可以为它打开一个问题。
回答by Gurjeet singh
This is an incompatibility with Angular 6. They removed support (shim) of process and global variables in browser.
这与 Angular 6 不兼容。他们在浏览器中删除了对进程和全局变量的支持(垫片)。
Adding the following before your closing into your index.html will remove the error.
在关闭 index.html 之前添加以下内容将消除错误。
<script>
var global = global || window;
var Buffer = Buffer || [];
var process = process || {
env: { DEBUG: undefined },
version: []
};
</script>
回答by Walter Corrales
To fix the problem, I added the following lines to 'polyfills.ts' file,
为了解决这个问题,我在“polyfills.ts”文件中添加了以下几行,
(window as any).global = window;
global.Buffer = global.Buffer || require('buffer').Buffer;
global.process = require('process');
回答by Daniel Danielecki
As long as you are not satisfied with putting some random logic into polyfills.tsand looking for a way to actually access the process.envin your Angular application please find below solution.
只要您不满意在 Angular 应用程序中加入一些随机逻辑polyfills.ts并寻找实际访问process.envAngular 应用程序的方法,请找到以下解决方案。
Change your "builder": "@angular-devkit/build-angular:dev-server"to "builder": "@angular-builders/custom-webpack:dev-server"in angular.json.
将您更改"builder": "@angular-devkit/build-angular:dev-server"为"builder": "@angular-builders/custom-webpack:dev-server"in angular.json。

