Conflicting use of babel / register

I get what seems like a conflict in babel/register between two local npm packages.

In the package, I do the following:

 require('babel/register'); require('index'); 

And in the index file of the same package:

 require('test'); 

And in the "test" package:

 require('babel/register'); require('test/index'); 

This causes the following error:

throw new Error("only one instance of babel/polyfill is allowed");

But if I take the string babel/register from the package "test", I get the following error in the index file of the package "test":

 import fs from 'fs'; ^^^^^^ SyntaxError: Unexpected reserved word 

I tried using System.import to import the “test” package (using the polyfill specified on the BabelJS website), but in this context I get the same error as above. How should I import one package into another and retain the ability to use ES6 import / export and other ES6 features?

EDIT: I simplified this a bit, I still need a “test” in the first package, but I don’t upload the mediation file. Instead, the "main" file "test" is set to test/index. Theoretically, now it just loads one ES6 module, which it should be able to register. I am still getting the above error.

+6
source share
3 answers

As you saw, babel/register designed to run only once for each application, and it is usually the top-level application that you run.

The problem you are facing is that by default require('babel/register') will only configure your system to send files directly inside the module, it will not process node_modules . The expectation in the general case that something in node_modules will be compiled ahead of time when it is published in your module registry.

One option is to pass ignore: false as an option, for example. require('babel/register')({ignore: false}); however, this is usually a bad idea and can lead to other problems. This will force Babel to overlap all files, but it is not always safe, because not all JavaScript code is guaranteed to be a valid ES6 module.

The best solution is to translate your test module in advance. If this does not work, you can use the only parameter to specify the regular expression or glob for the paths to be rewritten.

+2
source

In my case, I left with the setting:

 self._babelPolyfill = false; 

I do not know if installing polyfill a second time can cause some subtle problems, but with a lot of unit tests I did not see any problems using this hack to avoid script choking.

+1
source

Mistake: only one instance of babel/polyfill is allowed is exactly what @loganfsmyth mentioned: several poly-regiments that can be caused by using both babel-node and requiring ./register-babel at the same time.

However, in my case, this error did not appear strange until I had another error. I mean, everything works, then I had an EADDRINUSE error. When this happened, it was shaded and a polyfill error appeared. Very strange. I have not touched my code anywhere related to Babylon.

As soon as I fixed the polyfill, EADDRINUSE appeared and I was able to solve everything.

0
source

All Articles