i'm trying to get elements by id from an html file in a js file using nodejs. I'm getting the error 'document id not defined' because node doesn't provide a document object model by default.
So how can i use document.getElementById() in nodejs ?
Thank you !
If you want to parse files within the same server you should probably use some of this options, because nodejs it's just a JavaScript implementation, There is no
window
ordocument
object, see this. But to answer exactly your question:You could do it using Puppeteer
Here a basic example:
Use JSDOM npm package:
jsdom is a pure-JavaScript implementation of many web standards, notably the WHATWG DOM and HTML Standards, for use with Node.js. In general, the goal of the project is to emulate enough of a subset of a web browser to be useful for testing and scraping real-world web applications. https://github.com/jsdom/jsdom