0

I'm working on a Windows environment.

I install a module with

npm install -g themodule

The module is installed, I can see it with the command

npm list -g --depth 0

But when I do a require("themodule"), I get an the error:

Error: Cannot find module 'themodule'

I assume it's a rookie mistake but I don't see it.

Did someone have an explanation?

GBMan
  • 475
  • 2
  • 4
  • 16
  • On your terminal, if you type `themodule`, is it recognized? – Samuel Feb 17 '19 at 13:32
  • 1
    Possible duplicate of [NodeJS require a global module/package](https://stackoverflow.com/questions/15636367/nodejs-require-a-global-module-package) – sorabh86 Feb 17 '19 at 13:55
  • No duplication, I'm sure. – GBMan Feb 17 '19 at 14:26
  • When I type themodule I get not error saying themodule isn't a command. I guess it's something with variable environment, I'm on windows. – GBMan Feb 17 '19 at 14:28

1 Answers1

0

You are globally installing the package but not making it available in your node_modules folder for your project..

you need to give

npm i --save-dev themodule // this installs a devDependency for development purpose

npm i --save themodule // this installs a dependency and is for production ready.

(please see.. npm i is equivalent for npm install)

--

in short.. devDependencies object in package.json will have all the development related packages and dependency object will have production ready packages..

So.. to summon this.. when you say require('themodule'), then node checks whether this dependency package is available in node_modules folder or not..

so in your case it is not and that is the reason for the error..

Now, add the dependency package details in the package.json or do an npm i --save themodule

ggorantala
  • 196
  • 2
  • 16
  • Is this module available in your node_modules folder ? Can you check – ggorantala Feb 17 '19 at 14:57
  • You mean in the node_modules folder of my project? No it's not since I install it globally. – GBMan Feb 17 '19 at 15:29
  • @GBMan by installing globally (on windows machines anyway) you are simply installing to a node modules folder on your user profile instead of one of the local project folder. Can you check in a location like the following `C:\Users\XXX\AppData\Roaming\npm\node_modules` – DublinDev Feb 17 '19 at 16:25
  • Can you set a system variable NODE_PATH to your system node_modules path you gave above... or use “requireg” module to pull globally installed packages – ggorantala Feb 17 '19 at 16:34
  • Your global packages are not available to your project yet... so if you want them to directly work for you in your project.. create a system variable and point it to the bode_modules global folder.. – ggorantala Feb 17 '19 at 16:47
  • @DublinDev The module is here. – GBMan Feb 17 '19 at 18:50
  • @gopigorantala, I create a system variable NODE_PATH, this didn't change anything, and I look the values of PATH. The path for the global node_modules is inside so... I doesn't understand yet... I must resign myself to work in local... – GBMan Feb 17 '19 at 18:50
  • If this is the case then i will wipe the node_modules folder, unistall node and start again.. – ggorantala Feb 18 '19 at 07:48