Ionică Bizău Ionică Bizău - 6 months ago 24
Node.js Question

Avoid bundling specific modules with Browserify

I have a dependency tree like this:

index.js
\__ A
\__ B
\__ C
| \__ D
\__ E


I want to bundle
index.js
and that works fine:
browserify --node index.js -o bundle.js


The problem is when one of the dependencies has a dependency. Let's suppose it's
D
that has a native dependency (C++ code).

I want to install it manually using
npm install D
and make the
bundle.js
to really require it from the disk, not from the
bundle.js
code.

How can I exclude the
D
module from the bundle and make the bundle to require it from the
node_modules
?

I tried using
--ignore D
, but it returns an empty object when required.

How can I
require
a real module from the
node_modules
directory (the way like Node's
require
does?

Kim Kim
Answer

Use the --exclude option together with --node:

browserify --node -s GlobalVariable your-script.js -o bundle.js --exclude some-dependency

This will create the bundle.js file which will define the GlobalVariable variable if there is no CommonJS environment.

--node is a handy option if you want to run the bundle in Node, not in the browser.

The --exclude option will exclude the some-dependency module from the output bundle.

Check out the Browserify Usage section.