-2

How can I find and edit an attribute value in a JavaScript file using nodeJS.

for example: If i have a file called app.js containing following code.

let title = "hello world";

document.getElementById("arr").innerHTML = title;

how can I change the value of title let title = "hello world"; or any other attribute using nodeJS.

7
  • 1
    question is not clear Commented Oct 25, 2017 at 6:15
  • 3
    Read NodeJS file document, nodejs.org/api/fs.html Commented Oct 25, 2017 at 6:15
  • try using fs module Commented Oct 25, 2017 at 6:17
  • 1
    Why do you want to programmatically change a JavaScript file? Commented Oct 25, 2017 at 6:18
  • 2
    While it is certainly possible,better approaches exist. For example use a config file, and import it in app.js, then edit/generate the config. Or, since you're going to use gulp anyway, substitute some variables in the compilation step. Also the question reads like a classic example of a XY Problem Commented Oct 25, 2017 at 6:27

2 Answers 2

0

Instead of trying to parse/edit JS files why not use something like JSON which is easy to serialize?

Your gulp task can write whatever values are needed to a file:

const fs = require('fs')
fs.writeFileSync('./blah.json', JSON.stringify({ title: "whatever" }))

Then your code references the JSON file

const title = require('./blah.json').title
document.getElementById("arr").innerHTML = title;
Sign up to request clarification or add additional context in comments.

3 Comments

no the issue is my gulp file watches for changes on js files not only one file. there for I cannot use a seperate JSON file to store values, because from the above it only changes the json file values :/.
So modify the gulp watch? As @pawel mentioned, include detail explaining the actual process you are trying to achieve in the question. Editing javascript programatically is not likely to be the best solution to your problem.
@DilakshanSooriyanathan stackoverflow.com/questions/30171340/… - this answer should explain how to get gulp to watch for changes to different files
0

I was able to edit a JavaScript file using regex. my code is below:

  function updateFile(filename, replacements) {
            return new Promise(function(resolve) {
                fs.readFile(filename, 'utf-8', function(err, data) {
                    var regex, replaceStr;
                    if (err) {
                        throw (err);
                    } else {
                        regex = new RegExp("(\\" + 'let' + "\\s* ]*" + replacements[0].rule + "\\s*=\\s*)([^\\n;}]+)([\\s*;}])");
                            replaceStr = "$1" + replacements[0].replacer + "$3";
                            data = data.replace(regex, replaceStr);

                    }
                    fs.writeFile(filename, data, 'utf-8', function(err) {

                        if (err) {
                            throw (err);
                        } else {
                            resolve();
                        }
                    });
                });
            })
        }

usage:

var file = src/app.js
var myNewValue = "Hello";

     updateFile(file, [{
                rule: 'newApp',
                replacer: myNewValue
            }], function (err) {
                sails.log.info((err));
            });

this will edit,

let newApp = lol; to let newApp = Hello;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.