To deploy to @ danday74, we use this browser to work.
In short, this is how we use it.
So Browserify if you donβt know its nodejs CLI, which allows you to use the NodeJs require style for client code. It allows you to write well-structured, modular client code and embed it in a single file for inclusion in your page. Another advantage is that each file is attached to this single file. So no more random global (unless you use strict mode). The only thing that appears in your files is things that you explicitly export.
Since this is a CLI, you need to install it globally so you can use it on the command line.
npm install -g browserify
Then to run just from the do command line
browserify main.js > output.js
and indicate what is on your page
<script src="output.js"></script>
We personally mean that in our package.json , we do other things like linting and sass. here is an example
{ "name": "some app", "scripts": { "build:js": "browserify src/index.js > dist/built.js", "build:css": "node-sass sass/main.scss dist/built.css", "build": "npm run build:js && npm run build:css" } }
Now we just run npm run build and build sass and js.
What the browser will do is traverse your file recursively, looking for calls to require , then it will move to that file and repeat. As part of the search path, it will look in your node_modules folder. This is why you can include modules installed through npm .
Here is a small example.
//In a file called data.js located in same folder as main.js module.exports = [1, 2, 2, 3, 4, 5, 5, 6]; //in a file called main.js var unique = require('uniq'), data = require('./data'); console.log(unique(data)); //[1, 2, 3, 4, 5, 6]
To do this, first find the module named uniq installed via NPM (since there is no relative or absolute path). He will then search for our own file called data.js , located in the same folder.
When will this be built using browserify main.js > out.js
I hope this helps explain what a browser is, and how it can be useful for managing the structure when you want to enable npm modules in the client. I know this is not suitable for everyone, especially if you have a large application that does not use a browser, but now I use such a building tool to write modular code, I would never go to bacl.