require-quick

1.0.0 • Public • Published

require-quick

A quick way to use local modules within a project.

There are already many NPM packages which share the same essential purpose as require-quick. Yet, my ideas differ slightly, and because organization of code is important to the flavor of any project, I thought I'd "roll my own" once again.

The goals of require-quick are:

  1. To allow developers to quickly create modules with nothing but a single JS file.
  2. To allow developers to easily organize these modules in whatever directory structure is desired.
  3. It should only scan through the filesystem once. We don't want to perform another scan each time a module is imported.

Create Modules With Nothing But A Single JS File

Simply make a file with the same name as the module, and add .js at the end. For example, requireQuick('my-module') would live in single file called my-module.js.

Easily Organize Modules In Whatever Directory Structure

Those "single JS files" can be nested as deeply as you want in as many directories as you want. You just supply an environment variable beginning with rqroot, to use for the root path. (Also, more than one root path can be specified: just use more than one environment variable. Module location precedence is according to the "[].sort()" order of the env var names.)

For example,

const path          = require('path');
const requireQuick  = require('require-quick');

process.env.rqroot_one  = 'C:\\files\\quick modules';
process.env.rqroot_two  = path.join(__dirname, 'project modules');  // Since "t" > "o", this root gets subordinate precedence.

const myModule        = requireQuick('my-module');         // Located somewhere within "quick modules".
const aProjectModule  = requireQuick('a-project-module');  // Located somewhere within "project modules".

Of course, you can set environment variables any way you want:

  • Via a system-wide setting.
  • Before process launch, via the launch command.
  • After process launch, using process.env. (As in the example above.)

Only Scans The Filesystem Once

All root paths are scanned for JS files during the first invocation of requireQuick('some-module') — though each module itself still isn't loaded until require time (as typical). Thus, make sure that the env vars are set, and the JS files themselves are in place, prior to the first invocation.

Package Sidebar

Install

npm i require-quick

Weekly Downloads

1

Version

1.0.0

License

CC-PDDC

Unpacked Size

5.88 kB

Total Files

4

Last publish

Collaborators

  • wv-coder