machineghost machineghost - 4 months ago 29
Node.js Question

Can't Install babel Plugin `transform-es2015-destructuring`

I'm trying to use the Babel plug-in

transform-es2015-destructuring
in my Mocha tests, but I'm having a problem:

$ node_modules/mocha/bin/mocha --compilers js:babel-core/register test/testMain.js
/home/my/project/node_modules/babel-core/lib/transformation/file/options/option-manager.js:313
throw new Error("Couldn't find preset " + (0, _stringify2.default)(val) + " relative to directory " + (0, _stringify2.default)(dirname));
^

Error: Couldn't find preset "transform-es2015-destructuring" relative to directory "/home/my/project"


However, when I try to install the plug-in everything works fine:

$ npm install babel-plugin-transform-es2015-destructuring
myproject-reactjs@1.8.1 /home/my/project
└── babel-plugin-transform-es2015-destructuring@6.9.0


I have the same problem if I use Babel directly:

./node_modules/babel-cli/bin/babel.js --plugins transform-es2015-destructuring test/testMain.js
Error: Couldn't find preset "transform-es2015-destructuring" relative to directory "/home/my/project"


In other words, NPM is telling me I have the plugin, but Babel is telling me I don't. I'm tempted to believe NPM is correct, but if it is I can't figure out why Babel can't find the (clearly installed) transform package.

Here's my
.babelrc
:

{
"presets": [
"es2015",
"react",
"transform-es2015-destructuring",
"transform-object-rest-spread"
]
}


and here's the relevant part of my
dependencies
entry in
package.json
:

"babel": "^6.5.2",
"babel-plugin-transform-es2015-destructuring": "^6.9.0",
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-react": "^6.11.1",


Could anyone suggest how I could resolve this?

Answer

It's not a preset it's a plugin, so make sure you are setting it as a plugin and not a preset.

In your .babelrc:

{
  "presets": [
    "es2015",
    "react"
  ],
  "plugins": [
    "transform-es2015-destructuring",
    "transform-object-rest-spread"
  ]
}

In this case you can exclude using --plugins

Presets are essentially collections of plugins.

By the way, you can use node_modules/.bin/babel to use locally installed executables rather than trying to find them the module itself.

Comments