node.js module works from command line, not from script

為{幸葍}努か 提交于 2019-12-11 20:09:08

问题


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'

回答1:


If you omit the extension, node will resort to looking in the node_modules folder for a package named "jsdom" (installed via NPM).

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;


来源:https://stackoverflow.com/questions/19969737/node-js-module-works-from-command-line-not-from-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!