5

I'm struggling to use babel programmatically.

"use strict";

const babel = require("babel-core")
    , es2015 = require("babel-preset-es2015")
    ;

babel.transformFile("my-file.js", {
   presets: [es2015]
}, (err, result) =>
  console.log(err || result)
);

This ends with this error:

Couldn't find preset "es2015" relative to directory "/Users/myusername"

Though, I did install the babel-preset-es2015 as local dependency. How to fix this?

I don't want to keep babel-preset-es2015 and babel-core as local dependencies of the project.

Why does this error appear?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474

1 Answers1

4

Well, I started debugging deep in the babel-core and found a check for opts.babelrc !== false.

I probably have a .babelrc in my home directory, but since I want to use babel programmatically, I just want to ignore it, so I use babelrc: false in the options:

"use strict";

const babel = require("babel-core")
    , es2015 = require("babel-preset-es2015")
    ;

babel.transformFile("my-file.js", {
   presets: [es2015]
 , babelrc: false
}, (err, result) =>
  console.log(err || result)
);
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • `.babelrc` files belong in the project folder, you should never have a `.babelrc` in a generic parent folder, that's asking for trouble, just like this case. – loganfsmyth May 24 '16 at 16:37
  • @loganfsmyth Yes, true. Because I don't really want to have project-specific babel configuration, I created [`babel-it`](https://github.com/IonicaBizau/babel-it) which just babelifies the code in the current directory, before `npm publish`. – Ionică Bizău May 25 '16 at 03:39