A
A
Artem Shchurin2017-02-01 19:16:18
JavaScript
Artem Shchurin, 2017-02-01 19:16:18

How to write object to JS file using NodeJS?

I encountered NodeJS for the first time, set up gulp, no more.
The task is to find js modules, look at import, parse the object that exports these modules and collect everything in one file
. The fact is that when you try:

"use strict";
let fs = require('fs');
let data = {
    one: 'one',
    two: 'two'
};
fs.writeFile('test.js', data);

in test.js we get [object Object]
I'm wrong, I understand, but there is no desire to concatenate strings
How would it be possible to manipulate objects and write them to a file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yustas Alexu, 2017-02-01
@schurin

When faced with a task in node.js, it is highly likely that this task has already been solved by someone else.
And about import and other things: you cannot put them into a variable or pass them to a function in their naked form, you can only as a string.

V
Vitaly, 2017-02-01
@vitali1995

I don't know of a ready-made solution, but it's not hard to implement. Properties are iterated recursively: base types are cast to string form, objects and arrays are also expanded, functions have a toString property that decompiles the function and returns a string representation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question