Answer the question
In order to leave comments, you need to log in
How to insert text/code into a file using node?
I have a js file, and depending on the configuration, I need to generate code in certain places, how can I do this?
Answer the question
In order to leave comments, you need to log in
Everything is very simple:
const fs = require('fs');
const StringDecoder = require('string_decoder').StringDecoder;
const decoder = new StringDecoder('utf8');
// читаем файл
fs.readFile(path, (e, file) => {
if (e) throw new Error(e);
file = decoder.write(file); // конвектируем в строку
file = file.replace('строка которую меняем', 'строка на которую меняе');
fs.writeFile(patch, file, callback); // записываем обратно
})
Take a simple templating engine whose syntax won't break js. For example twitter.github.io/hogan.js
Rewrite the modified js code into the template engine language.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question