Concatenating browserify builds

If you're concatenating javascript modules compiled by browserify, which you might want to do if your build system uses Make, and you're using source maps with the browserify --debug option, and you're not using the --standalone option, this error will popup in your browser console:

Uncaught Error: Cannot find module 'function ()

It so happens that to fix another issue, the browserify folks omit the semicolon that would otherwise follow the final argument to the module IIF:

With a semicolon you can't wrap the bundle in an expression. I don't want to thrash on this issue because we had semicolons at one point and they were removed.

The one that would appear right before the line containing the source map:

},{"moduleName":1}]},{},[2]) // <----- there should be a semicolon here
//# sourceMappingURL=data:application/json;charset=utf-


1) Use the --standalone option. This requires specifying your module's global identifier on the command line:

browserify --debug --standalone moduleName infile.js > outfile.js

2) Or insert a line containing a semicolon by piping the output through awk:

browserify --debug infile.js | awk '/\/\/# sourceMappingURL/{print ";"}1' > outfile.js

3) Or edit the compiled file in place with sed:

sed -i '/\/\/# sourceMappingURL/i;' outfile.js


Note that the last example is not POSIX compliant, so if you're using OSX you'll either need to insert a literal new line, or if you're using bash and some other shells then $'\n' will suffice... see this StackExchange answer for more.

By Spritely Design