Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting started with browserify: import local files?

I have been prototyping a JavaScript application and now I want to move to a more robust setup using browserify and managing dependencies with require.

Currently I have the following files in my application:

chart.js
form.js
highcharts-options.js
vendor/
  highcharts.js
  jquery.js

highcharts-options.js is basically a list of constants, while chart.js looks like this...

var myChart = { 
  setup: function(data) { ...  this.render(data); },
  render: function(data) { ... }
},

and form.js looks like this:

var myForm = { 
  setup: function() { button.onclick(_this.getData(); },
  getData: function() { // on ajax complete, callChart },
  callChart: function() { myChart.setup(data); }
};
myForm.setup();

And then I have an index.html page that imports everything as follows:

<script src="/js/vendor/highcharts.js"></script>
<script src="/js/vendor/jquery.js"></script>
<script src="/js/highcharts-options.js"></script>
<script src="/js/chart.js"></script>
<script src="/js/form.js"></script>

So now I want to move this to a more modern setup with browserify.

I have deleted the vendor directory and instead created an index.js file and a package.json file, so now my directory structure looks like this:

index.js
package.json
chart.js
form.js
highcharts-options.js
node_modules/

I have run npm i --save highcharts-browserify and npm i --save jquery and that has saved these modules to package.json and installed them in node_modules. I've also added a build task in package.json: browserify index.js -o bundle.js. And in my front-end template I know just have:

<script src="/js/bundle.js"></script>

So far so good.

My question is what to put into my index.js file, because I'm not sure how to import the files that I already have. So far I've got this:

var $ = require('jquery');
var HighCharts = require('highcharts-browserify');

var options = require('highcharts-options');
var myChart = require('chart');
var myForm = require('form');

myForm.setup(); 

But when I try to build this, I get:

 Error: Cannot find module 'chart' from '/mypath/static/js/app'

It looks like require doesn't know how to find this file, or how to import it, which is not surprising given that this is all total guesswork on my part.

How should I adapt these files to work in a more modular way? Am I on the right lines, or is this completely the wrong approach? I'm not even sure what I should be Googling for.

(NB: Eventually I want to refactor chart.js and form.js to use Backbone, but I need to work one step at a time.)

like image 217
Richard Avatar asked May 21 '15 11:05

Richard


People also ask

How do I use browserify require?

Bundle up your first module With Browserify you can write code that uses require in the same way that you would use it in Node. Browserify parses the AST for require() calls to traverse the entire dependency graph of your project. Drop a single <script> tag into your html and you're done!

How do I use browserify bundles?

Use a node-style require() to organize your browser code and load modules installed by npm. browserify will recursively analyze all the require() calls in your app in order to build a bundle you can serve up to the browser in a single <script> tag.

How do I NPM a browserify package?

Browserify works by taking in a file and walking through all the require calls within to bundle all your code up into a file that can be used on the front end. Create app/app. js and put this within: var Backbone = require("backbone"); var $ = require('jquery/dist/jquery');

Is browserify a bundler?

Browserify is an open-source JavaScript bundler tool that allows developers to write and use Node. js-style modules that compile for use in the browser.


1 Answers

You are very close!

First, the way to reference a module in the same directory is to say:

var myChart = require('./chart');

Without the leading path component, require will look in your npm package directory.

Second, you need to export the variables in the modules so that they can be used elsewhere. So your form module needs to look something like this:

var myForm = { 
  setup: function() { button.onclick(_this.getData(); },
  getData: function() { // on ajax complete, callChart },
  callChart: function() { myChart.setup(data); }
};
myForm.setup();
module.exports = myForm;
like image 128
Robert Moskal Avatar answered Sep 26 '22 03:09

Robert Moskal