Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bundle node js code into one file?

I have tried using browserify etc... But they include browser specific code. I just want one javascript file to contain all my server side app.

I want to compile all the disparate sources of a server side node.js app into one file "app.js" so I can distribute it by itself and it contains all dependencies it requires inside the one file.

So if I had: one.js

require('./two.js')

two.js

console.log('two');

I'd run bundler one.js -o app.js

and app.js would look like:

console.log('two')

Ideally it would deal with the node system modules like fs, util, etc... intelligently. (I.e. probably not bundle them.)

like image 774
JayPrime2012 Avatar asked May 20 '26 02:05

JayPrime2012


1 Answers

Many nodejs modules require building or installation so you should consider distributing the node_modules folder or running npm install to install your app. If all the modules you use are simple js files with no further dependencies, I guess what you are looking for is to just merge the files together. You can easily do this with Gulp (and gulp-concat).

 var gulp       = require('gulp'),
        
     concat     = require('gulp-concat');

    gulp.src(['imustbefirst.js', 'imustbesecond.js', '*.js'])
          
        .pipe(concat('app.js'))
    
  
      .pipe(gulp.dest('./'))
like image 57
Kosmas Papadatos Avatar answered May 22 '26 15:05

Kosmas Papadatos