i have tried using fs module but I couldn't use this module in my project
const fs = require('fs');
import * as fs from ('fs')
None of the above commands don't work
You can use file saver or the solution here. You can't use node.js modules on the SPA.
If you want to do that, you need to create an API and save on the server side.
You can in cases use node modules as described in this SO question, but the browser will not allow to access the file system as you intend to do with th fs module.
fs module is node module and You can't use it on the client-side.
I suggest you use this
Save JSON string
Related
I am importing my json file from asset folder to read some configurations inside the app which works perfectly and I can change the json values based on my requirement
you can refer : https://www.techiediaries.com/import-local-json-files-in-typescript/
But in the production build version, When try to change the values from json file in the asset folder, it reads development data
after the investigation we found that json data are embedded in main-es5 file,
is there is any way to read json file using import method and we can change data dynamically (based on environment)
I would suggest the cleaner way to load it via HttpClient, and if it contains some configuration, I think this is the way you can change the data in assets and app will load again and again with HttpClient.
And if you want to read based on the environment, typescript import doesn't work.
Create a service, read the JSON via HttpClient based on pre-placed environment-specific JSON.
This is another alternative solution you can try.
Create one app.constant.ts file
//
// ===== File app.constants.ts
//
'use strict';
export const appConstants = {
'key':'value',
'key1':'value'
};
Import like this in component :
import {appConstants} from './../app.constants';
Use in component like this :
this.variable = appConstants.key;
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' `
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).
Lets say I have a yaml file which I want to validate in Angular6. If yaml file is parsed in json or other format successfully then say it's a valid yaml file otherwise it's not.
I found javascript and java has the solution for this question , but I wanted to do it using angular6.Is it possible to parse using angular and validate it?
I'm not sure WHY you'd want to parse a .yml file from Angular (vs., say, a standalone NodeJS app).
But sure.
The easiest way is probably to add a 3rd party library to your Angular build project (npm install --save), then invoke it from your Angular app.
js-yaml is a good choice: https://www.npmjs.com/package/js-yaml
EXAMPLE CODE (nodeJS):
yaml = require('js-yaml');
fs = require('fs');
// Get document, or throw exception on error
try {
var doc = yaml.safeLoad(fs.readFileSync('/home/ixti/example.yml', 'utf8'));
console.log(doc);
} catch (e) {
console.log(e);
}
I have an angular 2 project, how can I read and write to a JSON file on my server?
I can do what I want within my code itself bit I don't want to have to change my code, recompile and upload my website every time.
Any help? Examples are greatly appreciated
Angular can read the remote JSON file using the HTTP Client but it can't directly write to the remote file.
For writing, you can use a server side script such as PHP (supported by x10Hosting) to provide a url that allows Angular to post to (also using the HTTP Client), to update the JSON.
For example something like this PHP:
$data = json_decode('./data.json'); // decode the json
$data->something = $_POST['something']; // update the something property
file_put_contents('./data.json', json_encode($data)); // write back to data.json
I'm currently working in a project using Electron (basically nodejs with chromium) plus Angular2
Is there any way to save a typescript object in a file?
Currently, I'm saving objects in a json file. The problem is that all object methods are lost
Should I try to save the __proto__ variable?
Should I use a framework or a special database engine?
Today I'm reading the object from a the json file and parsing it using JSON.parse. Then I reassign all the properties in a new object, which is not scalable
Any suggestion is welcome
Thanks in advance. José
I'm the OP!
I just find this library that, apparently, solve the problem
https://www.npmjs.com/package/serializer.ts
So what to do? How to have in users array of User objects instead of
plain javascript objects? Solution is to create new instances of User
object and manually copy all properties to new objects.
Alternatives? Yes, you can use this library. Purpose of this library
is to help you to map you plain javascript objects to the instances of
classes you have created.
I'll give a shot and feedback. Bests
You can export variables as json like:
myJsons.ts:
export var myVar = {
name: "John"
}
and use them inside your ts files like:
myComponent.ts:
import {myVar} from '../path-to-myJsons'
this.myJson = myVar;
this.name = this.myJson.name;