Question

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?

Était-ce utile?

La solution

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);

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top