Resolve modules from any directory.
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is even with jshanson7:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example-app
lib
test
.babelrc
.editorconfig
.eslintrc
.gitignore
.travis.yml
History.md
Readme.md
package.json

Readme.md

Babel Resolver Plugin NPM version Build status

Resolve modules from any directory.

import User from '../../models/User';

Becomes:

import User from 'models/User';

Usage

npm i babel-plugin-resolver --save
rm -rf ~/.babel.json

In .babelrc:

{
  "presets": ["es2015"],
  "plugins": [["resolver", {"resolveDirs": ["src"]}]]
}

Given the directory structure:

/app
  .babelrc
  /src
    /models
      User.js
    /controllers
      User.js

In app/controllers/User.js:

import UserModel from 'models/User';
// => resolves: "app/src/models/User.js"

This example uses Babel 6.

Note: Run rm -rf ~/.babel.json if you're seeing errors.

Note2: This plugin is only called when you use import, not require.

Resolving Multiple Directories

In .babelrc:

{
  "presets": ["es2015"],
  "plugins": [["resolver", {"resolveDirs": ["src", "src/lib"]}]]
}

Given the directory structure:

/app
  .babelrc
  /src
    /models
      User.js
    /controllers
      User.js
    /lib
      utils.js

In app/controllers/User.js:

import UserModel from 'models/User';
// => resolves: "app/src/models/User.js"
import utils from 'utils';
// => resolves: "app/src/lib/utils.js"

Installation

npm i babel-plugin-resolver --save
rm -f ~/.babel.json

Why not just set NODE_PATH?

While setting NODE_PATH=app is a perfectly valid solution, babel-resolver is more explicit and lets you avoid mucking around with environment variables.

License

MIT