-4

Is it possible to run a nodejs file and use functions FROM that nodejs file from an html file? Or, is there a NPM Package that allows you to manipulate html elements in an html file from a nodejs file?

2
  • As in RCP or actually calling the client side's node binary? Or maybe you mean webpack? You need to clarify what you're asking. Commented Aug 4, 2020 at 1:38
  • Not sure, I want to be able to use nodejs inside an html file, but I haven't found any answers to it. I do have a nodejs file that loads the html file, although, I want to be able to control nodejs with the html file aswell. Is this possible? Commented Aug 4, 2020 at 1:47

1 Answer 1

1

Actually for your second question there is a package just for that. It's called Cheerio. Here's an example using it:

var cheerio = require('cheerio');
var fs = require('fs'); 

fs.readFile('path/to/file.html', 'utf8', function(err, data) {

    if (err) throw err;

    var $ = cheerio.load(data);

    $('body').append('<p>Hello World</p>');
    console.log($.html());
});
Sign up to request clarification or add additional context in comments.

3 Comments

No problem! I had a similar problem like this before. Glad I could help :)
Question: would it be able to add nodejs scripts? so like, a <script> tag that contains nodejs code like fs.readFile()?
Check out this question: stackoverflow.com/questions/50708715/… It has alot of helpful answers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.