Can't resolve 'net'/'global' or other node globals
Versions
Angular CLI: 6.0.0-beta.4
Node: 8.9.3
OS: win32 x64
Angular: 6.0.0-beta.6
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
@angular/cdk: 5.2.3
@angular/cli: 6.0.0-beta.4
@angular/material: 5.2.3
@angular-devkit/build-optimizer: 0.4.3
@angular-devkit/core: 0.4.3
@angular-devkit/schematics: 0.4.3
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 6.0.0-beta.4
@schematics/angular: 0.4.3
@schematics/package-update: 0.4.3
typescript: 2.6.2
webpack: 4.0.1
Repro steps
udpate from 1.7.2 to @next branch beta4
Observed problems:
1
it was working in 1.7.2 but not in @next branch I get in one module problem
But I fixed it after "npm i net"
2
after reinstalling 6beta4 I get:
Could not find local "typescript" package.The "@ngtools/webpack" package requires a local "typescript@^2.4.2" package to be installed.Error: Cannot find module 'typescript'
Error: Could not find local "typescript" package.The "@ngtools/webpack" package requires a local "typescript@^2.4.2" package to be installed.Error: Cannot find module 'typescript'
at Object.<anonymous> (C:\...\i10\node_modules\@ngtools\webpack\src\index.js:18:11)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\...node_modules\@angular\cli\models\webpack-config.js:3:19)
at Module._compile (module.js:635:30)
Fixed after "npm update --next" and "npm i"
3
Then I have an deprecation warning:

4
Then I have strange long chunk name

5
Another point is when running ng build --build-optimizer --prod
It block at step : 70% building modules without never finish .
Mention any other details that might be useful (optional)
Maybe this is due to webpack4.0 resolution, any temp idea?
None of those are truly wrong. They are all design decisions based on the complexity of the code, volume of platform-dependent APIs, and personal choice. The basic conditional, for instance, is quite acceptable if the API usage is minimal; or if combined with an abstraction layer that is then used throughout the package.
On version 6 of Angular CLI we are removing the shim for global and other node built-ins as I mentioned in https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814.
If you are using a library that assumes these globals are present, you can try manually shimming it inside your polyfills.ts file:
// Add global to window, assigning the value of window itself.
(window as any).global = window;
I ran a quick test with socket.io-client and this seemed to allow it to run.
@filipesilva I understand the reasoning behind this, but not providing a recommendation for dealing with it or a switch to disable it is unreasonable. The suggestion of submitting a ticket to all of the repos related to the dependencies my app has is almost laughable and unrealistic. We need a way to deal with this in the near term if anyone is going to be able to migrate their angular 5 apps to angular 6
@filipesilva :
The incorrect part is libraries that are meant to run in the browser relying on node globals being available. Browser code runs in the browser, not in node, and shouldn't expect things that are not available in a browser context to be there.
That's why some libraries have code that looks like this :
if (ENVIRONMENT_IS_NODE) {
var fs = require("fs");
var NODEJS_PATH = require("path");
NODEFS.staticInit();
};
Such code used to run fine in Angular 5.
However, in Angular 6, this same code produces the following errors :
ERROR in ./lib/luciad/photon/photon_painter.js Module not found: Error: Can't resolve 'fs' in 'D:\RIA ANGULAR\ria-angular-cli-2\lib\luciad\photon' ERROR in ./lib/luciad/photon/photon_painter.js Module not found: Error: Can't resolve 'path' in 'D:\RIA ANGULAR\ria-angular-cli-2\lib\luciad\photon'
This is simply ridiculous.
@clydin :
This is statement is not what is implied by the first:
"No library should support both browser and node".
Rather, a library should not assume its environment if it intends to support both node and a browser.
Of course. And that's why libraries have stuff like if (ENVIRONMENT_IS_NODE) { }, which now no longer seems possible with Angular 6. Thus, compatibility with Angular 6 prevents libraries from supporting both browser & Node.
None of those are truly wrong. They are all design decisions based on the complexity of the code, volume of platform-dependent APIs, and personal choice. The basic conditional, for instance, is quite acceptable if the API usage is minimal; or if combined with an abstraction layer that is then used throughout the package.
So are you saying I CAN use libraries that have stuff like if (ENVIRONMENT_IS_NODE) { } in them in Angular 6?
And if so, what would I need to do to get this to work?
well seems like someone else is even more frustrated than me ;D
Any update how to solve those issues?
How to bring access to node's process back again?
Tried something like: (window as any).process = process; or (window as any).process = window; at polyfills.ts but of course it didn't work. Doing that I have access to a process variable but it comes empty =S...
Update: rethink if you really need this, I was digging here 'cause of Ionic 4 and I ended up with the newest Ionic 4 environments no needing this access anymore.
For anyone still looking for an answer, here's how I managed to require('fs') in my angular 7 app. Or for that matter, any other node module.
Versions
Angular CLI: 7.0.4
Node: 10.13.0
OS: win32 x64
"@angular/animations": "~7.0.0",
"@angular/common": "~7.0.0",
"@angular/compiler": "~7.0.0",
"@angular/core": "~7.0.0",
"@angular/forms": "~7.0.0",
"@angular/http": "~7.0.0",
"@angular/platform-browser": "~7.0.0",
"@angular/platform-browser-dynamic": "~7.0.0",
"@angular/router": "~7.0.0",
"@angular-devkit/build-angular": "~0.10.0",
"@angular/cli": "~7.0.4",
"@angular/compiler-cli": "~7.0.0",
"@angular/language-service": "~7.0.0",
"electron": "^3.0.7",
"typescript": "~3.1.1"
1. Install @types/node
npm install --save-dev @types/node
2. Modify tsconfig.json
Take note of "allowSyntheticDefaultImports" flag. It must be set to true.
{
"compileOnSave": false,
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "es2015",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
],
"strict": false
}
}
3. Require fs
import { Component } from '@angular/core';
import { } from 'electron';
import Fs from 'fs';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
constructor() {
//check if platform is electron
let isElectron: boolean = window && window['process'] && window['process'].type;
if (isElectron) {
let fs: typeof Fs = window['require']('fs');
let app: Electron.App = window['require']('electron').remote;
console.log(fs, app, window['process']);
}
}
}
Note: The import statements at the top of the file are just to provide for type information. The variable values are set using node require.
4. Result

Edit:
Turns out, that if your project has dependencies that require 'fs', 'path', 'child_process' etc. The angular compiler fails to compile the code. To get around this, as someone has already suggested, add (window as any).global = window; to your polyfills.ts.
In my case, I had chokidar, node-pty and electron as a dependency. This worker for me.
in an angular 7 app the workaround (https://github.com/angular/angular-cli/issues/9827#issuecomment-386154063) only works for me if i put
(window as any).global = window;
in a .ts file and import it in polyfill.ts , e.g. import 'cstm-polyfill.ts';
Hello, is there any lib for deflation that can be used with Angular 6 ? Since the upgrade I am having the following issues with these zlibs: https://github.com/devongovett/browserify-zlib/issues/36 https://github.com/imaya/zlib.js/issues/74 I've tried some workarounds, described in the issues above but to no avail. p.s. I use pako for the deflation without any issue, however, the above is still valid for the mentioned libs.
I followed the guidance you provided here and on my StackOverflow Question but it is still failing to compile.
@filipesilva @Nishkalkashyap @vlkrrrr is there an alternative method to execute rather than using webpack? The config.js produced via ng eject is deprecated and I don't want to rely on 3rd party libraries. I can understand using electron.remote does not produce any errors with regards to a ts component but electron/index.js is having some issues and (windows as any).require('fs') code is not working. Any help will be appreciated! Is there any way where we fetch the required module from elsewhere except electron package?
While https://github.com/angular/angular-cli/issues/9827#issuecomment-369578814 is relevant to the problem. I need some form of solution in order to get my app working.
@shinde-ha using https://github.com/manfredsteyer/ngx-build-plus to alter the webpack config is the most convenient option I think.
@filipesilva @clydin i think ngx-build-plus might not be required unless necessary. I guess we can always have IPC communication and supply 'fs' and 'path' thru main process which handle node built-ins and send the contents back to renderer.
Another option for those is to use TS config path mapping to map the “fs”/“path” imports to either manually installed shims or even arbitrary application code.
@filipesilva @clydin It makes sense on the fact that chromium server architecture should have such changes. But it risks the main process in electron to be relatively busy. Consequently, What are your thoughts about handling this issue with regards to electron being used in Angular? I won't prefer relying on any third party libraries.
Why cli shows a warning from a library which requests a stream global inside the try/catch block? e.g. https://github.com/isaacs/sax-js/issues/237
I fixed this by adding
"types": [
"node"
]
in tsconfig.app.json
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.