-->

How to browserify a browserified module?

2019-08-09 10:45发布

问题:

The title seems confusing but I'll give an example.

Let's say I create a module that uses ES6 that runs in the browser, so I use browserify with babelify to build everything.

Now I want to include that same module in a project that uses browserify, but does not uses Babel to compile ES6, so I need the compiled version.

I tried to require the "browserified" module like this:

// es5-project.js
require('./compiled-module-with-browserify');

But when I run browserify es5-project.js I start to get some errors like this:

Error: Cannot find module './XXX' from '/Users/mauricio.oliveira/projects/project-name/dist-folder'

And that makes sense, since browserify compiled all modules into one file, it won't find the modules inside the compiled file.

Does anyone have faced a problem like this one? if you did, how you solved it?

Thanks!

回答1:

Found the answer!

This will do the trick https://github.com/substack/browserify-handbook#browser-field

Just define a "browser" index in the package.json file, and point to the initial source file.

:)