0

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' `
8
  • You should show at least what you tried Commented Jul 17, 2019 at 16:11
  • 1
    Angular apps executes in the brower. Bowser apps aren't allowed to write files. Commented Jul 17, 2019 at 16:13
  • That is true, but NodeJS used to offer the fs module (or was it angular? anyway, it worked.) you could use fs.writeFile() for example. Commented Jul 17, 2019 at 16:14
  • Well, it's important to understand what environment you're running your code on, otherwise you run into exactly these types of situations. Commented Jul 17, 2019 at 16:15
  • Yes, but NodeJS is used to write server applications, not browser applications. Commented Jul 17, 2019 at 16:16

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly, sorry. I am running Angular CLI.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.