self-import

1.0.1 • Public • Published

self-import

Allows requiring of own modules like dependency modules

npm version Build Status

Access your modules via require('foo') instead of long relative paths like require('../../..'). Inspired by require-self.

Background

Usually packages include tests and examples in their repos. This code needs to require the module itself.

But whereas modules depending on foo can write require('foo'), the test and example code within foo cannot reliably require itself that way. It must use a relative path like var foo = require('../..') instead.

Being able to do require('foo') instead of require('..') has a few advantages. The code can be moved around. Example code is a tad clearer and can be reused in a client module without changes. And if you are authoring in TypeScript, then import foo = require('foo') picks up all the static type info from foo.d.ts, exactly as a client module would see it.

Installation

npm i -D self-import

Usage

Call self-import in your package's prepublish script.

{
  // ...
  "scripts": {
    "prepublish": "self-import"
  },
  "devDependencies": {
    // ...
    "self-import": "*"
  }
}

Programmatic usage

const selfImport = require('self-import')
 
selfImport('/home/src/foo').then(() => console.log('Done!'))

License

MIT © Zoltan Kochan

Package Sidebar

Install

npm i self-import

Weekly Downloads

1

Version

1.0.1

License

MIT

Last publish

Collaborators

  • zkochan