문제

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'
도움이 되었습니까?

해결책

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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top