Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one specify a custom search path with Browserify?

I have a large project that consists of hundreds of source files broken into several folders.

Something like this:

src/
  AAA.js
  subdir/
    DDD.js

I would like to be able to specify dependencies with non-relative paths.

For instance, in DDD.js I would like to do this:

var AAA = require('AAA');

...rather than this:

var AAA = require('../AAA');

How can I achieve this with Browserify?

like image 598
namuol Avatar asked Mar 14 '14 17:03

namuol


2 Answers

As stated in the documentation, Browserify uses browser-resolve under the hood.

When using the node API (as opposed to the CLI), you can specify a paths option which contains a list of directories to pass to browser-resolve.

The solution for my example would thus be something like this:

var browserify = require('browserify');
var b = browserify({
  paths: [
    __dirname + '/src'
  ]
});

b.add(__dirname + '/src/AAA.js');
b.bundle().pipe(process.stdout);
like image 74
namuol Avatar answered Sep 21 '22 08:09

namuol


Or if you want to do it from the command line you can add your directory to the node search path:

NODE_MODULES=$NODE_MODULES:src browserify -o output.js input.js
like image 29
Will Madden Avatar answered Sep 22 '22 08:09

Will Madden