Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change what directory Babel plugins are resolved against?

I'm getting this error:

Unknown plugin "transform-class-properties" specified in "base" at 0, attempted to resolve relative to "/home/me/Projects/myproj/src"

The message is pretty clear, so I know why it's happening, but I want to change where Babel looks for the plugins/presets/packages.

I'm using Babel with rollup via rollup-plugin-babel.

The options I'm giving it are:

{ plugins: [ 'transform-class-properties', 'transform-object-rest-spread' ],
  babelrc: false }

However, I can't find an option to change where Babel looks for the plugins. Is there no way to do this without rewriting my plugins list to use absolute paths?


I also can't find a public API method for extracting the dependencies from .babelrc, so it's pretty hard to manually rewrite the file to use full paths. N.B. Babel configs might also be stored in package.json, and there's been some talk about adding support for .babelrc.js too -- I really don't want to maintain my own project that searches for all the different places a babel config might be hiding, parse the file(s), and scan it for all the plugins, with and without the arbitrary babel-plugin- prefixes.

like image 904
mpen Avatar asked Dec 05 '17 18:12

mpen


People also ask

Where do I put Babel plugins?

Using a Plugin If the plugin is on npm, you can pass in the name of the plugin and Babel will check that it's installed in node_modules . This is added to the plugins config option, which takes an array. You can also specify an relative/absolute path to your plugin.

Where do I put Babel presets?

Using a Preset Within a Babel config, if the preset is on npm, you can pass in the name of the preset and Babel will check that it's installed in node_modules already. This is added to the presets config option, which takes an array. Otherwise, you can also specify a relative or absolute path to your presets.

How does Babel plugin work?

Babel is a JavaScript compiler, specifically a source-to-source compiler, often called a "transpiler". This means that you give Babel some JavaScript code, Babel modifies the code, and generates the new code back out.

How do I use Babel plugin import?

First clean up the config files you created, and make sure you have babel-plugin-import installed. This will give you a config folder with 2 webpack config files for dev/prod environments. Open those files and locate the place where you need to insert the plugins property as documented on the instructions page.


1 Answers

You can use NODE_PATH to do the same.

$ npx babel test.js
Unknown plugin "external-helpers" specified in "/Users/tarun.lalwani/Desktop/babeltest/.babelrc" at 0, attempted to resolve relative to "/Users/tarun.lalwani/Desktop/babeltest"

After specifying the path for modules in a different location

$ NODE_PATH=/Users/tarun.lalwani/Desktop/babeltest2/node_modules npx babel test.js
function test() {
   this.abc = function (url) {
      return console.log(url);
   };
}

NODE_PATH environment variable allows you to specify additional locations where the modules can be searched for

like image 94
Tarun Lalwani Avatar answered Sep 23 '22 12:09

Tarun Lalwani