Ionică Bizău
Ionică Bizău

Reputation: 113345

Passing a preset to babel programmatically

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?

Upvotes: 5

Views: 763

Answers (1)

Ionică Bizău
Ionică Bizău

Reputation: 113345

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)
);

Upvotes: 4

Related Questions