I'm trying to migrate my Angular Universal project from Angular v5 to v6
I've got a service where I use fs
to load the translation on the server side. Everything works well with Angular v5.
With Angular v6, when I run npm run start
aka ng serve --proxy-config proxy.conf.json
I face the following error
ERROR in ./src/providers/core/translate/translate-universal-loader.service.ts Module not found: Error: Can't resolve 'fs' in '/Users/me/Documents/projects/myproject/src/providers/core/translate'
In my service I declare fs
like the following:
declare var require: any;
const fs = require('fs');
I also tried to declare it like following, but didn't help
import * as fs from 'fs';
To tell webpack to ignore fs I tried to add the following in my webpack.server.config.js
without success
node: {
fs: 'empty'
}
also tried with a webpack plugin, wasn't successful neither
new webpack.IgnorePlugin(/fs/)
but actually it's maybe not the config use by ng serve
but I don't know if I could still eject the configuration with v6?
anyone has got an idea?
UPDATE
If I declare fs as any
it solves the problem for ng serve
but unfortunately it will not work on the server side after npm run build:ssr
and run npm run serve
. On the server side I will then face the following error
ERROR ReferenceError: fs is not defined
p.s.: my project follows https://github.com/angular/universal-starter structure, config and dependencies
In Angular 8, you can now use Angular Builders to specify a
web.config.js
which extends the virtual config produced by Angular.This blogpost explains it quite well.
tldr:
npm i -D @angular-builders/custom-webpack
angular.json
filearchitect.serve
andarchitect.build
to tell it to use thecustom-webpack
module to extend the virtual config with yourwebpack.config.js
filewebpack.config.js
- in this case it would look like this: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
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.
3. Require fs
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
.For updates, Track the issue here
https://github.com/angular/angular-cli/issues/9827
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.
The accepted answer is correct; you can't use
fs
anymore in Angular v6+.However, this alternative builder (it's an extension to the Angular CLI) allows you to target an Electron environment and have full access to Electron's features:
https://github.com/angular-guru/electron-builder