mneumann
mneumann

Reputation: 786

Best way to write to JSON file in Node 10/Angular 7?

I'm not sure if this question belongs here, but this is driving me insane. I am running Node v10.16.0. Sorry!

Since in Angular 7 fs no longer works - what is the best way to write to a JSON file?

Importing a JSON file now works nice and easy, but how do I write to it?

Edit: Adding what I tried.

component.ts:

import * as fs from 'fs';
[...]
saveChanges(changes) {
    fs.writeFile('config.json', JSON.stringify(config), null);

Error: `Module not found: Error: Can't resolve 'fs' `

Upvotes: 0

Views: 2220

Answers (1)

pascalpuetz
pascalpuetz

Reputation: 5428

Calling fs from within Angular is not possible - and usually not what you want to do.

Angular is a Front-End Framework that is used to bind data to a UI. This happens in a Browser environment, not NodeJS.

What you are most likely doing is serving your Angular application using some kind of NodeJS Server (like express, or the Angular CLI).

Upvotes: 2

Related Questions