0.2.1 • Public • Published


The namespaces you always wanted but never asked for

Build Status Coverage Status

Namespacer is a simple library that aliases namespaces into filepaths without having to deal with including folders into your NODE_PATH or anything like that. It also allows you to create namespaces programaitcally.

It's simple, when you load your spaces file it assumes it's based off of your current module root, at the same level as your node_modules folder.

Currently it follows the PSR-4 because I have yet to create a javascript spec for loading namespaces neatly and include options for glob loading.

A namespace can be created from any object as long as it follows the format of the key being the namespace where the value is the location on the file system relative to the .spaces.js file.

'use strict';

module.exports = {
    'NS/': 'Namespace/',
    'NS/Config': 'Namespace/Something/Config'

It is also possible to dynamically decide what folder your namespace leads to, for example if you wanted to have specific configuration files given in an environment you can structure your spaces like so.

'use strict';

module.exports = {
    'ENV/': `config/${process.env.NODE_ENV}`

To load an environment you can give namespacer a relative or absolute path to your spaces file, or an object of namespaces. If passed a straight object it will guess at your root being the node execution root. If namespaces are passed from a file the all namespaces are relative to the location of the spaces file.

If your spaces file is in <root>/config/.spaces.js and it defines 'NS/foo' => 'foo' the mapped path is <root>/config/foo. Simply define the namespace as 'NS/foo' => '../foo' to resolve the issue or require the namespace configuration as an object and set the root as the root of the application.

//Include namespace however you want, via an instance or static
const Namespace = require('namespacer-js');
const {namespace} = require('namespacer-js');

//Load namespace from object mapping the results to root
namespace.addSpacesFromObject({'NS/': 'config/'}, path.resolve('./'));
namespace.addSpacesFromObject(require('./.spaces.js'), path.resolve('./'));
//Load namespace from default spaces file, looks for .spaces.js before .spaces.json
//Load namespace from relative path to spaces file

//Load namespace from absolute path to spaces file

Package Sidebar


npm i namespacer-js

Weekly Downloads






Last publish


  • sieabah