relatively

0.1.2 • Public • Published

A WIP experimental approach to solving the Node require() ../../../../../../.. problem. See for example Better local require() paths for Node.js. Allows require()ing paths that are relative to the application root, or whatever you configure.

Demo

Use case

// Before
require("../../../../../../../whatever");
 
// After
require("app/whatever");

Features

  • Only needs to be invoked at app launch. Doesn't require any changes to modules, they just call require() normally (except with the paths you configure).
  • Uses only public Node API.
  • More flexible than NODE_PATH.

Usage

Create an initialization script that you require prior to your normal app entry script:

// init.js
require("relatively")({
  mods: [
    {
      type: "dir",
      // Makes `require("app/whatever")` behave like
      // `require("/path/to/some/dir/whatever")`, from anywhere in your
      // dependency tree.
      from: "app",
      to: "/path/to/some/dir",
    },
  ]
});
// entry.js
// Loads /path/to/some/dir/x/y/z
require("app/x/y/z");

node -r ./init.js entry.js

Readme

Keywords

Package Sidebar

Install

npm i relatively

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • jmm