Skip to content

How to fix this:Error in mounted hook: "TypeError: Class constructor Base cannot be invoked without 'new'" #7022

Closed

Description

@NoraGithub

I have searched related issues. And the most populate answer is to use preset to set the env not to transform es6 class to es2015. According to issue#4269, issue#338I think my setting should make sense to not to transform, but it doesn't.

My .babelrc setting is like this:

{
  "presets": [
    ["env", { "modules": false ,
    "targets": {
        "browsers": ["> 1%", "last 2 versions", "not ie <= 8"],
        "node":"current"
      }
    }], "stage-2"
  ],
  "plugins": ["transform-runtime","syntax-dynamic-import"],
  "env": {
    "test": {
      "presets": ["env", "stage-2"],
      "plugins": ["istanbul"]
    }
  }

}

My code is like below:

// a.js
// class definition
import Base form '@antv/g2/src/facet/base';
class Nested extends Base {
...
}
export default Nested;
//b.js
import Nested from './nested';
G2.Chart.prototype.facet = function(type, cfg) {
...
const cls = new Nested(cfg);
...
}
export default G2;

And the browser report that Error in mounted hook: "TypeError: Class constructor Base cannot be invoked without 'new'"

And the related transformed code may be like this

var Nested = function (_Base) {
  _inherits(Nested, _Base);

  function Nested() {
    _classCallCheck(this, Nested);

    return _possibleConstructorReturn(this, (Nested.__proto__ || Object.getPrototypeOf(Nested)).apply(this, arguments));
  }
...
software version(s)
Babel "babel-preset-env": "^1.6.1"
node 7.9.0
npm 4.2.0
Operating System macOS 10.11.3 (15D21)

More Info:

    "babel": "^6.23.0",
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-eslint": "^7.1.1",
    "babel-loader": "^7.1.2",
    "babel-plugin-syntax-dynamic-import": "^6.18.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-preset-env": "^1.6.1",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",

Looking forward to your reply.

Activity

  1. babel-bot commented on Dec 13, 2017

    @babel-bot
    Collaborator

    Hey @NoraGithub! We really appreciate you taking the time to report an issue. The collaborators
    on this project attempt to help as many people as possible, but we're a limited number of volunteers,
    so it's possible this won't be addressed swiftly.

    If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
    community that typically always has someone willing to help. You can sign-up here
    for an invite.

  2. Jessidhia commented on Dec 14, 2017

    @Jessidhia
    Member

    This happens if Nested is transformed by babel but Base isn't. The way babel is calling the superclass constructor is not allowed by the ES6 spec -- it's just how babel emulates it in ES5, and in order for it to work, the superclass needs to also be emulated in ES5.

  3. NoraGithub commented on Dec 14, 2017

    @NoraGithub
    Author

    @Kovensky Do you have any idea how I should do to make the superclass be emulated in ES5, as it is from node_modules . I am confusing. Do I need to copy out the hold project to put it in my directory?

  4. Jessidhia commented on Dec 14, 2017

    @Jessidhia
    Member

    It looks like @antv/g2 does provide a build where the class is transformed, but that's only as an UMD bundle that bundles the entire library.

    If you still want to pick the specific class from @antv/g2/src/facet/base, I think you're going to have to add a rule on your webpack config to pass the files inside node_modules/@antv/g2/src through babel-loader.

  5. NoraGithub commented on Dec 14, 2017

    @NoraGithub
    Author

    @Kovensky Thanks.The idea is quite clear and smart. I tried it and found something else that my project is using import export and the @antv/g2 is using require and module.exports.They seems to be incompatible, like the situation describe here g2#287 .It throws TypeError: Cannot assign to read only property 'exports' of object '#<Object>' error. Do you experience this? Do you have any suggestion?

  6. NoraGithub commented on Dec 14, 2017

    @NoraGithub
    Author

    I found that what really matters is transform-runtime plugin in .babelrc. As I removed it, it seems to work well, at least, I would not get both errors above. Hope to get reviewed from you whether it's coincidentally in such a case and help closed it if it's a right solution. @Kovensky

  7. nicolo-ribaudo commented on Jul 26, 2021

    @nicolo-ribaudo
    Member

    Fixed by #8656

  8. added
    outdated
    on Oct 26, 2021
  9. locked as resolved and limited conversation to collaborators on Oct 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Spec: Classesoutdated

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions