Browserify cannot find module when trying to bundle many js files

Ricardo Castañeda picture Ricardo Castañeda · Dec 26, 2014 · Viewed 28.3k times · Source

This is my first day doing node, I'm having some problems trying to bundle some js files.

MyFolder
|-- app (folder)   
|  |-- Collections (contains: movies.js)   
|  |-- Models (contains: movie.js)  
|  |-- node_modules  
|-- main.js  
|-- node_modules (folder)    
|-- static (folder)  

This is the content of js files I want to compress into static/bundle.js

 // app/models/movie.js  
 var Backbone = require("backbone");
 var Movie = Backbone.Model.extend({
   defaults: {
     title: "default",
     year: 0,
     description: "empty",
     selected: false
   }
 });
 module.exports = Movie;

 // app/collections/movies.js  
 var Backbone = require("backbone");
 var Movie = require('models/movie');
 var Movies = Backbone.Collection.extend({
      model: Movie
 });
 module.exports = Movies;

When I run browserify -r ./app/main:app > static/bundle.js the file bundle.js is created with the scripts from app/main.js. It works as expected.

But when I run browserify -r ./app/collections/movies.js:movies \ -r ./app/models/movie.js:movie > static/bundle.js, it creates an empty bundle.js and shows this:

Error: Cannot find module '/Users/MyFolder/app/models/movie.js:movie' from '/Users/MyFolder'  

My folder app/node_modules is sync with ln -sf ../models . and ln -sf ../collections .

Question 1: Any hint what I'm doing wrong?
Question 2: If static/bundle.js exists. Does running browserify again overwrites the file or not? On my local tests it doesn't overwrite, so am I supposed to delete this file each time for update?

Answer

cheshireoctopus picture cheshireoctopus · Jan 27, 2015

Might consider adding ./ to your path:

var Movie = require('./models/movie');

see: How to use browserify to bundle a backbone app?