Error: Cannot find module, despite module installed globally

I am using Intellij Idea and I am trying to run a Node script that uses the Puppeteer module. When I run the script outside of Intellij Idea, it works fine. But when I run it inside, I get the following error:

Error: Cannot find module 'puppeteer'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/myuser/.rvm/gems/ruby-2.1.2@core/bundler/gems/docket-plus-43e0140393ef/lib/data/js/county/practice.js:6:19)

Basically, the script is inside a ruby gem stored somewhere else in filesystem. I run the script from another project. But I set up puppeteer with npm globally:

npm install -g puppeteer

This means that the puppeteer should be found no matter where I run the script. I installed the NodeJS plugin for Intellij Idea and I see puppeteer cited in the preferences:

 

Yet i continue to get this error. Why can't it find the puppeteer module?

2 comments
Comment actions Permalink

>This means that the puppeteer should be found no matter where I run the script

No, nothing of the kind. Globally installed modules can't be required from local scripts; the global installation is only used for setting up system-wide available binaries, such as unit test runners or bootstrappers or things like that.

You can use NODE_PATH to specify a different location, but note that using it is not recommended:

You are highly encouraged to place your dependencies locally in node_modules folders. They will be loaded faster, and more reliably.
0
Comment actions Permalink

Actually I had to add it to .bash_profile:

 

export NODE_PATH='/usr/local/lib/node_modules'

0

Please sign in to leave a comment.