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?

4
  • 1
    have you defined jsdom in angular-cli.json file? Commented Feb 23, 2018 at 10:37
  • no, I don't know how to do that. Any link you can suggest that demonstrates this process? Commented Feb 23, 2018 at 10:38
  • 1
    stackoverflow.com/questions/44817349/… Commented Feb 23, 2018 at 10:39
  • Thanks Kumar, learned something new :) Commented Feb 23, 2018 at 10:48

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@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 :-)
At want prevents you from doing this using nodejs ? A nodejs server could also request the contents and parse it.
@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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.