Question

I've installed jsdom on my windows 8.1 machine.

If I am one directory above my local installation of jsdom, I can call

node jsdom

and everything works.

However, if, from the same dir / pwd, I call a script (at a different path) with

require("jsdom").jsdom;

I get errors of the type

Error: cannot find module 'jsdom'
Was it helpful?

Solution

Node's CLI and require() behave slightly differently:

node path resolves using path.resolve

  • Behaves like you'd expect your operating system to resolve a path.

require(path) resolves using require.resolve

  • The exact behavior is a little complicated, but basically if the beginning of the string looks like a package name, it will look in node_modules for a matching package (i.e. doesn't look like a file system path starting with ./, /, ../, etc).

In your case, you can either install the module with NPM (preferred):

npm install jsdom --save

require("jsdom").jsdom;

Or include the file directly if you've manually added it to your filesystem:

require("./jsdom.js").jsdom;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top