最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is it necessary to install Browserify twice to bundle - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

2 Answers 2

Reset to default 6

Installing 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.

发布评论

评论列表(0)

  1. 暂无评论