Git Product home page Git Product logo

app_modules's Introduction

CircleCI

app_modules

Get rid of relative import syntax by making app subdirectories appear as modules.

E.g.,

require('app/foo');
// instead of
require('../../../foo');

Works seemlessly with transpiled code (see below).

Install

npm install app_modules

Configure

Run code like the following as early as possible in your app:

// before requiring other files...
var app_modules = require('app_modules');

// IRL, compute the following paths dynamically:

// initialize the library (this creates an app_modules dir in your project root):
app_modules.init('/path/to/my/project/');   

// make src/ available as a module named app:
app_modules.addModule('app', '/path/to/my/project/src/'); // app_modules/app now points to src/

Example

Given a Node.js project as follows:

- myproject
  - package.json
  - src/
    - main.js # start script
    - foo/
      - bar.js
  - lib/ # transpiled code

Initialize app_modules with dynamically computed paths:

// src/main.js
var app_modules = require('app_modules');
var path = require('path');

app_modules.init(path.resolve(__dirname, '..'));  // path of project root
app_modules.addModule('app', __dirname);  // dynamically link app to either src/ or lib/

// ... continue loading

Now you can write:

var bar = require('app/foo/bar');

The app module links correctly to src/ or lib/ depending on whether you're running the untranspiled src/main.js or transpiled lib/main.js code.

Warning

This library uses an old but unofficial method _initPaths defined on the Modules class.

API

init

Ensures a directory exists in the project root and adds it to the NODE_PATH. Directories in this folder will be accessible as modules.

init(projectRoot, app_modules_dirname)
// where
// projectRoot - path to the root dir of your node project
// app_modules_dirname - defaults to 'app_modules'

addModule

Symlinks a directory into your app_modules directory.

addModule(moduleName, sourcePath)
// where
// moduleName - name you'd like the sourcePath to be accessible as
// sourcePath - absolute path to a directory

app_modules's People

Contributors

aneilbaboo avatar

Stargazers

 avatar  avatar

Watchers

 avatar

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.