JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 145
  • Score
    100M100P100Q81556F

Namespace support for node.js, making it easy to require an entire directory.

Package Exports

  • require-namespace

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (require-namespace) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

require-namespace

When you require modules in node.js you include the path to the file which means that moving files break dependent modules.

To avoid that issue the new notion of a namespace allows you to require a dependency in a way that is more loosely coupled to the directory structure on disk.

Installation

$ npm install require-namespace

Usage

During initialisation you synchronously create a namespace and associate it with a directory:

namespace.createSync(__dirname + '/model/', 'domain')

The second argument provides the name of the namespace.

At this point the directory is recursively scanned and a record of each file is kept. Once this is done we can access the modules from the namespace:

var domain = namespace.domain;
var linkProcessor = domain.LinkContentProcessor;

That require will work if there was a file called 'LinkContentProcessor.js' anywhere within the directory we used when creating the namespace.

Example

The project comes with an example that you can run using

node examples/simple.js

Tests

The tests use mocha and can be run using:

mocha -R spec "spec/**/*_spec.js" --recursive