Node Not Finding Global Module

纵饮孤独 提交于 2019-12-22 04:26:10

问题


So I realize that this is a fairly generic title and question, but I've scoured through so many answers, but sadly none of them seem to be working for me. I'm hoping with a little more information from myself perhaps someone has a specific answer, or knows exactly which answer to redirect me to.

My problem: when I install node modules globally (e.g. npm install -g nightmare) I'm unable to access them in my project folder. I get the error:

module.js:471
    throw err;
    ^

Error: Cannot find module 'nightmare'
...

From what I've read so far installing the module locally inside the project folder seems to be the preferred practice (is this true?), but for now during the development phase I'd like to be able to use the modules I have installed globally. I'm newer to node.js so I'm still discovering what the "usual practice" is for these things though. I first installed Node using Homebrew, but being as I had issues with how things were installing and the pathing (which people were saying is common when using Homebrew), I decided to try a clean slate, uninstalling Homebrew, then eventually Node and NPM as well. I reinstalled just Node and NPM via the site, but am still having no luck being able to access my global modules.

I'm pretty confused at this point because I have some node_module files and packages in some random spots, but not where my current global packages are being installed. I'm assuming leftover from a previous installation. That being said I've adjusted my $NODE_PATH several times in hopes that I could access the modules, but no such luck with that.

The current installation spot according to a --verbose install is /Users/<user>/.npm-packages/lib, which I thought was strange being as a lot of the people already answering this question are correcting their paths to things like /usr/local/lib/node_modules which has things in it for me, but not the modules installed. I'm assuming that (because of the 'local' file it's in) this is the local installing... maybe? To be honest I'm pretty confused at this point as far as why everything is installed where it is and why, even with changing the $NODE_PATH I'm unable to access the modules.

If I've missed something or need to include some other output please feel free to ask and prompt for more. I'm not sure what else is needed for more direction.

Thank you in advance!


回答1:


You can not import packages that are installed globally.

You have 2 options, either installing locally:

npm install nightmare --save

Or creating a local link to the global package:

npm install -g nightmare
npm link nightmare


来源:https://stackoverflow.com/questions/42445679/node-not-finding-global-module

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