Can Browserify be used on server-side Node.js?
The Node.js modules system is great, but it leads to some bloat on the filesystem. In most cases, this filesystem bloat isn't really an issue. However, our team has been exploring AWS Lambda, and I'm sure there are similar server-style environments where a smaller code footprint would be helpful. Which is why I thought of Browserify, since it pulls only the dependencies it needs.
Is it possible and if it is, does anyone know if there is any extra configuration needed to make it work properly in a server-style environment?
@techpines there's this, which I haven't tried yet but have been meaning to take a look: https://www.npmjs.com/package/noderify
There is a --node flag which does just this.
Alias for both --no-builtins, --no-commondir, and sets --insert-global-vars
to just "__filename,__dirname". This is handy if you want to run bundles in
node.
--no-browser-field, --no-bf
Turn off package.json browser field resolution. This is also handy if you
need to run a bundle in node.
--node
Alias for --bare and --no-browser-field.
@techpines: With which solution did you came up? Did noderify work for you?
@mderijcke: Can I use this within gulp? I did something like: browserify({basedir: config.server.path, node: true}) ", but can't see a difference, documentation doesn't state that node option in api exists (just on commandline).
Hi @Cyber1000,
It's a bit counter intuitive but if you're using the API (which you are, when using Gulp) you should specify bare: true and browserField: false which is what --node does internally.
So the reason nothing happened is because opts.bare is not actually checked anywhere in the code :-)
@Cyber1000 I haven't implemented this yet on my end, but I was going to stick with browserify since it's a more mature library and it looks to already has this functionality builtin (plus we're already using it for client-side js)
Thanks for your answers, in the end I also stick to browserify, I just did an additional "bundleExternal: false" as some third-party-component made problems. So far I got my uglified/browserified server.js up and running.
browserify process the Node Global __dirname,How can I make it to be ignored by browserify?
I was facing this problem, too. I was trying to bundle a bunch of server-side JavaScript and I could not do it with the browserify NodeJS API. Guess the browserify library is not really the right tool for the job anyway as the name suggests.
Turns out you can use webpack (https://webpack.github.io/) and bundle JavaScript by setting 'node' as the target: http://jlongster.com/Backend-Apps-with-Webpack--Part-I .Works like a charm.
@goto-bus-stop Is what is said by @keksipurkki true? I would think Browserify supports node as a target considering this exists https://www.npmjs.com/package/serverless-plugin-browserifier
Most helpful comment
There is a --node flag which does just this.