0

I'm trying to use jsdom in this Angular 5 project.

npm install jsdom --save

Now, I import the library:

import * as jsdom from "jsdom";

And then use the library in my code just like the npm readme suggests:

const inMemoryDom = new jsdom('<html>...</html>');

The app crashes and console reads Cannot find module "child_process". Any clue what this could mean?

Jem
  • 6,226
  • 14
  • 56
  • 74

1 Answers1

4

The jsdom library is meant to be used with nodejs, it's therefore not possible to use it inside a browser environment. Because modules like child_process are only available inside a nodejs environment.

In theory the only option to run it inside a browser environment would be to use something like browserify.

Inside the github repository of jsdom there's a section explaining what has to be done to use it inside a browser environment.

cyr_x
  • 13,987
  • 2
  • 32
  • 46
  • 1
    What would be the point of using jsom in an browser environment in the first place? I don't really see the point to be honest. – Patrick Kelleter Feb 23 '18 at 10:39
  • @PatrickKelleter it's an app that load the HTML contents of legacy websites on a dead intranet, parses the contents and uploads bits to a database. It's not pretty but it's effective :-) – Jem Feb 23 '18 at 10:41
  • 1
    At want prevents you from doing this using nodejs ? A nodejs server could also request the contents and parse it. – cyr_x Feb 23 '18 at 10:42
  • @Cyrix, thanks that makes sense now. Will try browserify, or will stick to const parser = new DOMParser(); Indeed, nodejs could be an option in this case. I wanted to skip writing a backend as Angular seems a good fit for adding buttons etc. to help the engine make decisions when parsing. – Jem Feb 23 '18 at 10:44