I created a new project
I npm install -g browserify
I tested using the cmdline, browserify app.js > bundle.js
. Cool.
I want to minify so I npm install uglifyify --save-dev
I tested using the cmdline, browserify -g uglifyify app.js > bundle.js
. Great.
Now I want to do this with code, but I get Error: Cannot find module 'browserify'
This is my code, basically to replace the cmdline
var browserify = require('browserify')
var fs = require('fs')
var bundler = browserify('./app.js')
bundler.transform({
global: true
}, 'uglifyify')
bundler.bundle()
.pipe(fs.createWriteStream('./bundle.js'))
It seems I would need to again install browserify locally to this project?
I created a new project
I npm install -g browserify
I tested using the cmdline, browserify app.js > bundle.js
. Cool.
I want to minify so I npm install uglifyify --save-dev
I tested using the cmdline, browserify -g uglifyify app.js > bundle.js
. Great.
Now I want to do this with code, but I get Error: Cannot find module 'browserify'
This is my code, basically to replace the cmdline
var browserify = require('browserify')
var fs = require('fs')
var bundler = browserify('./app.js')
bundler.transform({
global: true
}, 'uglifyify')
bundler.bundle()
.pipe(fs.createWriteStream('./bundle.js'))
It seems I would need to again install browserify locally to this project?
Share Improve this question asked Mar 14, 2016 at 15:50 BarryBones41BarryBones41 1,4911 gold badge15 silver badges32 bronze badges2 Answers
Reset to default 6Installing an npm module like browserify
allows you to use browserify
as a mand on the mand line. To use the module within your project's code, you must install the module as a dependency. In other words, yes, is must be installed locally within the project's ./node_modules
folder and referenced in the package.json
file.
From the npm documentation:
- Local install (default): puts stuff in
./node_modules
of the current package root.- Global install (with
-g
): puts stuff in/usr/local
or wherever node is installed.- Install it locally if you're going to
require()
it.- Install it globally if you're going to run it on the mand line.
- If you need both, then install it in both places, or use
npm link
.
As said in the other answer, one way to solve this is that you can install browserify locally instead of globally, like: npm install --save browserify uglifyfy
. Then you can add a script
in package.json
:
...
"scripts": {
"build": "browserify app.js > bundle.js",
...
},
...
Now, npm run-script build
will know how to find local browserify, which is going to be in your node_modules/
directory. And your require('browserify')
will work, since browserify is now local.
Another way you could solve this is NODE_PATH
env variable. Set this variable in your bashrc
or equivalent like this:
export NODE_PATH=$NODE_PATH:$HOME/.nvm/versions/node/v4.2.6/lib/node_modules
Adjust the path to wherever your global node_modules are. Then you can require()
whatever you installed with -g flag in your code.
However this is suboptimal, as it can lead to errors and misunderstandings. But if it's for some quick-and-dirty scripts, it can help.