Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fundon opened this issue Mar 14, 2015 · 13 comments
Closed

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

fundon opened this issue Mar 14, 2015 · 13 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@fundon
Copy link

fundon commented Mar 14, 2015

babel/polyfill problem

dirs tree:

├── server.js
├── node_modules
│   └── trek

Both server.js and node_modules/trek use ES6+ syntax.

server.js

import Trek from 'trek';

var app = new Trek(__dirname);

app.get('/', function* (next) {
  this.body = 'Hello Trek.js!';
});

app.on('error', function (err, context) {
  app.logger.error(err);
});

app.run();

index.js of trek module

import Trek from './lib/trek';

export default Trek;

run the server.js

$ babel-node server.js

output:

/Users/fundon/dev/trekjs/trek-cli/trekapp/node_modules/babel/lib/babel/polyfill.js:4
  throw new Error("only one instance of babel/polyfill is allowed");
        ^
Error: only one instance of babel/polyfill is allowed
    at Object.<anonymous> (/Users/fundon/dev/trekjs/trek-cli/trekapp/node_modules/babel/lib/babel/polyfill.js:4:9)
....

How to solve it?

@sebmck
Copy link
Contributor

sebmck commented Mar 14, 2015

The error message is pretty clear, only one instance of the polyfill should be used at all times. You have two modules with different versions of babel polluting globals with polyfills which leads to weird bugs. Either have the modules that are using babel/polyfill set to the same version so it resolves to the same module or compile the code with the runtime transformer.

@sebmck sebmck closed this as completed Mar 14, 2015
@fundon
Copy link
Author

fundon commented Mar 14, 2015

When running babel-node cmd will ignore the packages in the node_modules directory.
https://github.com/babel/babel/blob/master/src/babel/api/register/node.js#L34

So I hacked it in the server.js file, https://github.com/trekjs/trek-cli/blob/master/templates/app/esnext.js based on https://github.com/postcss/postcss/blob/master/index.js.

var escape = function (str) {
    return str.replace(/[\[\]\/{}()*+?.\\^$|-]/g, "\\$&");
};

var regexp = ['lib', 'test', 'node_modules/god'].map(function (i) {
    return '^' + escape(path.join(__dirname, i) + path.sep);
}).join('|');

require('babel/register')({
    only:   new RegExp('(' + regexp + ')'),
    ignore: [ 'node_modules' ],
    loose: 'all'
});

And the shouldIgnore function
https://github.com/babel/babel/blob/master/src/babel/api/register/node.js#L75-L77

Maybe this will be better:

var shouldIgnore = function shouldIgnore(filename) {
  if (onlyRegex && onlyRegex.test(filename)) {
    return false;
  }
  return ignoreRegex && ignoreRegex.test(filename);
};

@sebmck Do you need I submit a PR for this?
I know this very small change.

@fundon
Copy link
Author

fundon commented Mar 14, 2015

I think Babel should not ignore all node_modules path.
Maybe check a identifier in package.json and transform the module automagically that will be great.

@hax
Copy link

hax commented Aug 4, 2015

This issue should be reopen. It's impossible to control all the deps (and the deps of a dep) to use same version (and even same versions still throw error) or use runtime transformer.

So I believe it's the duty of babel itself to solve the problem.

@loganfsmyth
Copy link
Member

@hax Dependencies should not be loading the polyfill at all. If they rely on global ES6 features, then they need to document that and the root application should ensure that a polyfill is loaded, or the deps should use the runtime transform to load a non-global version of the polyfill.

@hax
Copy link

hax commented Aug 4, 2015

@loganfsmyth I assume all you said is correct , then:

  1. The document of babel should include the caveat for the library writers
  2. Runtime transformer should be in the default list not optional, and
  3. Ask the major libraries which use babel to recompile and republish their packages.

@ruslansavenok
Copy link

If you use react.js on client and server, you might have babel/polyfill already loaded in node.js but not browser.

This is how i solved it:

// In node.js env, polyfill might be already loaded (from any npm package),
// that's why we do this check.
if (!global._babelPolyfill) {
  require('babel-core/polyfill')
}

@stan-ros
Copy link

@ruslansavenok thx, works for me.

@hax
Copy link

hax commented Aug 13, 2015

@fundon I agree with you that babel should support compiling module by checking the fields in package.json (maybe "babel": {"main": "src/index.js"})

@fundon
Copy link
Author

fundon commented Aug 13, 2015

@hax thx.
haha, You can submit a PR for this.

@jameslk
Copy link

jameslk commented Nov 6, 2015

So what are we supposed to do with Babel 6? It looks like the runtime is no longer used. I'm trying to write a library using ES6 and I do not want to pollute the global namespace with polyfills.

@loganfsmyth
Copy link
Member

@jameslk Please see our README:

For questions and support please visit the Slack community or StackOverflow.

@jameslk
Copy link

jameslk commented Nov 6, 2015

@loganfsmyth Thanks. I figured it out anyway. It looks like babel-runtime has been moved to babel-plugin-transform-runtime and this needs to be added to the list of plugins to use it. Would of helped if that was documented somewhere.

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jul 10, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

7 participants