GithubHelp home page GithubHelp logo

local-modules's Introduction

local-modules

require local modules with absolute path

problem

require local modules in node can be annoying with relative paths. e.g.

    var cache = require('../../../plugins/cache');

detailed problem description and discussion as well as different solutions can be found here:

better local require paths

solution / hack

treat specific application folders like the node_modules folder, so that we can require stuff without ugly relative paths.

###install npm install local-modules --save

###use

with the given project structure:

    .
    ├── index.js
    ├── package.json
    ├── components
    │   └── components-test
    │       └── index.js
    ├── lib
    │   ├── lib-test
    │   │   ├── package.json
    │   │   └── test.js
    │   └── plugins
    │       ├── cache
    │       │   ├── index.js
    │       │   └── lru
    │       │       └── index.js
    │       └── index.js
    └── node_modules
        └── test
            └── index.js

you can require this from the app directory:

    // only call this once
    require('local-modules')('lib', 'components');

    // require from node_modules as usual
    require('test');

    // require from lib with absolute paths
    require('lib-test');
    require('plugins/cache');

    // require from components directory directly
    require('components-test');

or within the lib/plugins/cache/lru/index.js file, you can require this:

    require('plugins');
    require('lib-test');
    require('components-test');

test

tested so far with node 0.8, 0.10, 0.11

  1. clone the repo
  2. run node test

inspiration

https://gist.github.com/branneman/8048520

credits

  • @branneman for the great article on this topic
  • @joelabair for the cool hack

local-modules's People

Contributors

andineck avatar

Stargazers

Rafał Pięciorak avatar Sabin Tudor avatar James Maloney avatar Dominik Lubański avatar Michael Shick avatar Filip Malinowski avatar Artem Sapegin avatar Will Sahatdjian avatar

Watchers

 avatar James Cloos avatar

Forkers

isabella232

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.