0

I am using angular to create an application and has a requirement to store a json object currently stored in a variable to file.json located in src/app/assets using angular. I have searched a lot and have not found a way to do this. Ask if need any more information.

7
  • 1
    So, are you trying to read and write the JSON file? I'm afraid you'll not be able to perform the write operation. That's how the browser works. Commented Jun 6, 2020 at 16:20
  • You can do it using nodejs writefile nodejs.org/api/… Commented Jun 6, 2020 at 16:21
  • 1
    @Ferhado not in the frontend (he is using Angular) Commented Jun 6, 2020 at 16:22
  • You are wright, not in frontend, I forgot about that, thanks! Commented Jun 6, 2020 at 16:25
  • @JoseGuzman file already exists in the assets folder i only need to update it or change the contents of it is there any workaround for this? Commented Jun 6, 2020 at 16:26

1 Answer 1

4

You cannot write files with Angular. Don't forget that the Angular app is not running in the directory structure you create. It's not even running on a server. It's running in the browser as compiled JavaScript. It has no direct write access to any filesystem.

If you need to write to server-side files in your application, you need some server-side code. This can be achieved, for example, with NodeJS (Express, NestJS…) if you want to stick with JavaScript. Either way, you can't write files directly with Angular.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.