npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rinclude

v0.4.3

Published

custom module include

Readme

rinclude

require module with custom folder and generate virtual index.js automatically

install

npm install rinclude

Usage

Add the following to the startup file

const include = require('rinclude');
global.include = include; // It can be used anywhere.

Notice

It no longer create an index.js file.
You do not need to exclude the index.js file from programs like pm2.
You can adjust it with an option.

create index.js file

const include = require('rinclude');

include.path('./custom');

...

// after that
include.clean(); // Remove all recorded paths.

include.options({ createIndex: true, type: 'es6' }); // type es6 or es5
include.path('./custom');

Detailed usage

basic

Add the following to the startup file

const include = require('rinclude');
global.include = include; // It can be used anywhere.

example

// Read files in custom folders
 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── index.js
 ├── app.js
 └── sample.js
// app.js
const include = require('rinclude');
global.include = include; // It can be used anywhere.

include.path('./custom');

const timer = include('timer');
timer.start();
timer.stop();

require('.sample')
// smaple.js
const timer = include('timer');
timer.start();
timer.stop();

Avoid collisions

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── index.js
 ├── custom2
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── index.js
 └── app.js
include.path('./custom);
include.path('./custom2); // Crash

Use prefix


include.path('./custom');
include.path('./custom2', 'two');

const timer = include('timer');
const timer2 = include('two.timer');

timer.start();
timer.stop();

timer2.start();
timer2.stop();

Automatically create an virtual index file

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           └── stop.js
 └── app.js
include.path('./custom');
const timer = include('timer'); // The index.js file is required.

Add the .generateIndex file.

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── .generateIndex
 └── app.js

Automatically create virtual index.js file.

The contents of virtual index.js are as follows.

module.exports = {
  start : require('./start.js'),
  stop : require('./stop.js')
};

You can now access the timer.

include.path('./custom');
const timer = include('timer');

timer.start();
timer.stop();

When you add a file and restart app.js, virtual index.js is created automatically

 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           ├── pause.js       // add new file
 │           └── .generateIndex
 └── app.js

The contents of virtual index.js are as follows.

module.exports = {
  pause: require('./pause.js'),
  start : require('./start.js'),
  stop : require('./stop.js')
};

You can now access the timer.

include.path('./custom');
const timer = include('timer');

timer.start();
timer.stop();
timer.pause();

Usage : .generateIndex

  1. basic
  • Create only a .generateIndex file.
  • It lists all the files in the folder.
 .
 ├── custom
 │     └── timer
 │           ├── start.js
 │           ├── stop.js
 │           └── .generateIndex
 └── app.js
  • Generated virtual index.js
module.exports = {
  start : require('./start.js'),
  stop : require('./stop.js')
};
  1. You can have a special folder structure.
 .
 ├── custom
 │     └── timer
 │           ├── api
 │           │    ├── start.js
 │           │    └── stop.js
 │           ├── lcd
 │           │    └── display.js
 │           ├── info.js
 │           └── .generateIndex
 └── app.js

ex1)

  • Contents of .gnerateIndex
api, lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  display: require('./lcd/display.js)
};

ex2)

  • Contents of .gnerateIndex
api, lcd:lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  lcd: {
    display: require('./lcd/display.js)
  }
};

ex3)

  • Contents of .gnerateIndex
api:api, lcd
  • Generated virtual index.js
module.exports = {
  api : {
    start : require('./api/start.js'),
    stop : require('./api/stop.js')
  },
  info : require('./info.js'),
  display : require('./lcd/display.js')
};
  1. Excludes undefined folders.
 .
 ├── custom
 │     └── timer
 │           ├── api
 │           │    ├── start.js
 │           │    └── stop.js
 │           ├── lcd
 │           │    └── display.js
 │           ├── something
 │           │    └── notNeeded.js
 │           ├── info.js
 │           └── .generateIndex
 └── app.js
  • Contents of .gnerateIndex
api, lcd:lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  lcd: {
    display: require('./lcd/display.js)
  }
};
  1. It supports several subfolders.
 .
 ├── custom
 │     └── timer
 │           ├── api
 │           │    ├── start.js
 │           │    └── stop.js
 │           ├── lcd
 │           │    ├── check
 │           │    │    └── checking.js
 │           │    └── display.js
 │           ├── info.js
 │           └── .generateIndex
 └── app.js
  • Contents of .gnerateIndex
api, lcd:lcd
  • Generated virtual index.js
module.exports = {
  start : require('./api/start.js'),
  stop : require('./api/stop.js'),
  info : require('./info.js'),
  lcd: {
    check: {
      checking: require('./lcd/check/checking.js')
    },
    display: require('./lcd/display.js)
  }
};

will create index.js

const include = require('rinclude');
include.path('./custom');
include.options({ createIndex: true, type: 'es6' });
// This file was generated automatically.
// Do not edit!!
import pause from './pause.js';
import start from './start.js';
import stop from './stop.js';

export default { pause, start, stop };
const include = require('rinclude');
include.path('./custom');
include.options({ createIndex: true, type: 'es5' });
// This file was generated automatically.
// Do not edit!!
module.exports = {
  pause: require('./pause.js'),
  start : require('./start.js'),
  stop : require('./stop.js')
};

gulp task

When a new file is created or deleted in the path, a new index.js file is created.

Create a new index.js file even if the file content of the .generateIndex changes.

// /tasks/rinclude.js
'use strict';

const include = require('rinclude');
const watch = require('gulp-watch');
require('colors');

// Add a custom library folder here.
const options = {
  libs: [{
    path: '../test',
    prefix: undefined
  }]
};

function manipulate(opts) {
  opts.js = [];
  opts.gen = [];

  opts.libs.forEach(lib => {
    const base = lib.path.replace(/\.+\//g, '');

    opts.js.push(`${base}/**/*.js`);
    opts.gen.push(`${base}/**/.generateIndex`);
  });
}

manipulate(options);

function createIndexjs() {
  include.clean();
  options.libs.forEach(lib => {
    include.path(lib.path, lib.prefix);
  });
  console.log('[rinclude]'.green + ' index.js was created automatically.');
}

function rinclude(cb) {
  include.options({ createIndex: true, type: 'es6' }); // type es6 or es5

  createIndexjs();
  cb();
}

watch(options.js, { readyDelay: 500 }, vinyl => {
  if (/add/.test(vinyl.event)) {
    console.log('[gulp]'.green + ` File ${vinyl.path} was added`);
    createIndexjs();
  }

  if (/unlink/.test(vinyl.event)) {
    console.log('[gulp]'.green + ` File ${vinyl.path} was removed`);
    createIndexjs();
  }
});

watch(options.gen, { readyDelay: 500 }, vinyl => {
  if (/change/.test(vinyl.event)) {
    console.log('[gulp]'.green + ` File ${vinyl.path} was changed`);
    createIndexjs();
  }
});

module.exports = rinclude;
// /gulpfile.js
'use strict';
const gulp = require('gulp');

// load gulp tasks
require('gulp-load-all-tasks')('tasks');

// Build task definitions
gulp.task('dev', gulp.series('rinclude'));
// /server.js
You do not need to require('rinclude').
Because index.js has already been created.