Babel Resolver
Resolve modules from any directory.
import User from '../../models/User';
Becomes:
import User from 'models/User';
Usage
Using Babel 6 & babel-core/register
:
var path = require('path');
var localModulesDir = path.resolve(__dirname, 'app');
var anotherDirToCheck = path.resolve(__dirname, 'app/lib');
var resolver = require('babel-resolver')(localModulesDir, anotherDirToCheck);
require('babel-core/register')({
presets: ['es2015'],
resolveModuleSource: resolver
});
require('./app');
Note: Run rm -rf ~/.babel.json
if you're seeing errors.
Note2: Babel's resolveModuleSource
is currently only called when you use import 'module'
, not when use require('module')
Also, keep in mind that you must provide absolute directory paths to babel-resolver
.
Installation
npm i babel-resolver --save
rm -rf ~/.babel.json
Why not just set NODE_PATH?
Setting NODE_PATH=app
is a perfectly valid solution. babel-resolver
is for those who:
- find environment variables annoying
- want to resolve modules from more than one directory
License
MIT