Answer the question
In order to leave comments, you need to log in
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);
[object Object]
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question