5

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 !

Sarah ßezi
  • 99
  • 1
  • 2
  • 6
  • 1
    node.js programs don't run in a browser. There is no `document` in the first place. Are you asking how to parse an HTML file in node.js? –  Sep 10 '18 at 11:27
  • 3
    Possible duplicate of [How do I parse a HTML page with Node.js](https://stackoverflow.com/questions/7372972/how-do-i-parse-a-html-page-with-node-js) –  Sep 10 '18 at 11:28

2 Answers2

4

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 or document object, see this. But to answer exactly your question:

how can I use document.getElementById() in nodejs ?

You could do it using Puppeteer

Puppeteer is a Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol. It can also be configured to use full (non-headless) Chrome or Chromium.

Here a basic example:

const puppeteer = require('puppeteer');

(async () => {
    const browser = await puppeteer.launch();


        page = await browser.newPage();
        await page.goto('http://example.com/some.html', {waitUntil: 'load'});


    const newPage = await page.evaluate(() => {

        return  document.getElementById("idexample").innerHTML;

        });

     console.log(newPage)

  })();
Emeeus
  • 5,072
  • 2
  • 25
  • 37
  • OP wants to parse an HTML file on the server, not some website. –  Sep 10 '18 at 13:21
  • 1
    @ChrisG You could parse the files of the same server, also the question is: "how to use document.getElementById() in Nodejs" and this is a way to do that. – Emeeus Sep 10 '18 at 13:26
  • The question doesn't mention there's a server running. And it clearly also says `from an html file`, which means this is almost an [XY problem](http://xyproblem.info/) and using `document.getElementById()` was already a wrong alley in the first place. On top of that, the question is a clear duplicate, and answered extensively. –  Sep 10 '18 at 13:54
  • 1
    @ChrisG yes, it's probably an XY problem, I've made a clarification in my answer. Either way if somebody find this question googling how to use document.getElementById() in node, this is one possible correct answer. Thanks for the feedback! – Emeeus Sep 10 '18 at 14:31
2

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

gugateider
  • 1,983
  • 2
  • 13
  • 17