Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM: Can't require globally installed module

Tags:

node.js

npm

I am trying to require the NPM package browserify, which I have installed globally through this command:

npm install -g browserify

This is the file bundle.js in which I try to require browserify:

var browserify = require('browserify');
var fs = require('fs');
var b = browserify();

var outputFileStream = fs.createWriteStream('./test_bundle.js');

b.add('index.js');
b.bundle().pipe(outputFileStream);  // process.stdout

When I do node build.js I get this error:

Error: Cannot find module 'browserify'
( etcetera...)

If I then instead install browserify locally:

npm install -g browserify

I get no error and the build goes just fine.

I have uploaded a small demo program to this Github repository:

https://github.com/loldrup/test_require

So if you're on a Windows 7 machine, you should be able to reproduce my error simply by cloning:

git clone https://github.com/loldrup/test_require

and running:

node build

EDIT:

even after adding relevant node paths to my system variable 'path', and restarted the command promt, I still can't require globally installed node modules:

enter image description here


1 Answers

Node.js doesn't look in the folder where global modules are installed by default.

Add module.paths.push('path to global node_modules') at the beginning of the script or set up the enviroment variables.

like image 89
Jannik Becher Avatar answered Mar 07 '26 03:03

Jannik Becher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!