JSPM

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

Simplifies requiring of modules by adding an intermediate namespace, where each namespace is associated with a 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

Node.js Namespaces

NOTE - This project is a very early alpha.

During initialisation you synchronously create a namespace and associate it with a directory. The creation is currently purely synchronous because it is done as part of system startup and this approach simplifies the code.

To create a namespace you thus do this as part of the system startup:

var domain = namespace.createSync('domain', __dirname + '/server/javascript/domain/')

At this point the directory is recursively scanned and a record of each file is kept. Anywhere that you want to require modules from the namespace you do the following:

var domain = require('require-namespace')('domain')
var TwitterConfigFactory = domain.require('TwitterConfigFactory')

In this last snippet of code we first get the domain namespace and then use it to require 'TwitterConfigFactory'. Its only when we ask for 'TwitterConfigFactory' that file with that same name is required.

Among the advantages of this approach is it means the code that requires a dependency is more loosely coupled to the directory structure on disk, moving TwitterConfigFactory wouldn't break any code that requires it.

Examples

Directory structure:

domain
  authentication
    user.js
    twitter
      twitterAuthentication.js

Creating the namespace and then resolving dependencies from it:

var domain = namespace.createSync('domain', __dirname + '/server/javascript/domain/');
var twitterAuthentication = domain.require('twitterAuthentication'); // NOTE - Doesn't matter that it was in a sub-directory
var twitterAuthentication = domain.require('user');