Angular 6 - process is not defined when trying to serve application

后端 未结 5 1834
心在旅途
心在旅途 2020-11-30 08:43

I am getting the following error when I try to serve my angular 6 application using cosmicjs:

Uncaught ReferenceError: process is not defined
    at Object..         


        
相关标签:
5条回答
  • 2020-11-30 09:18

    To fix the problem, I added the following lines to 'polyfills.ts' file,

    (window as any).global = window;
    global.Buffer = global.Buffer || require('buffer').Buffer;
    global.process = require('process');
    
    0 讨论(0)
  • 2020-11-30 09:20

    This is an incompatibility with Angular v6. They removed support (shim) of process and global variables in browser.

    I suggest you to use Angular 5, till cosmic.js fixes the error. Maybe you can even open an issue for it.

    0 讨论(0)
  • 2020-11-30 09:26

    As long as you are not satisfied with putting some random logic into polyfills.ts and looking for a way to actually access the process.env in your Angular application please find below solution.

    Change your "builder": "@angular-devkit/build-angular:dev-server" to "builder": "@angular-builders/custom-webpack:dev-server" in angular.json.

    0 讨论(0)
  • 2020-11-30 09:34

    This is an incompatibility with Angular 6. They removed support (shim) of process and global variables in browser.

    Adding the following before your closing into your index.html will remove the error.

    <script>
      var global = global || window;
      var Buffer = Buffer || [];
      var process = process || {
        env: { DEBUG: undefined },
        version: []
      };
    </script>
    
    0 讨论(0)
  • 2020-11-30 09:42

    In polyfill.ts, add this line:

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

    Reference: https://github.com/algolia/algoliasearch-client-javascript/issues/691


    Or npm i -S process, then add this to polyfill.ts:

    import * as process from 'process';
    window['process'] = process;
    
    0 讨论(0)
提交回复
热议问题