question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next 12 Runtime TypeError: Class constructors cannot be invoked without 'new'

See original GitHub issue

What version of Next.js are you using?

12.0.2

What version of Node.js are you using?

16.13.0

What browser are you using?

Chrome 96

What operating system are you using?

MacOS

How are you deploying your application?

next start

Describe the Bug

While updating my application from next 11 to 12 I ran into a runtime issue which I’m guessing is originating from the switch to SWC (?). I’m extending a Class and instantiating it, leading to a runtime error:

Unhandled Runtime Error
TypeError: Class constructors cannot be invoked without 'new'
pages/index.tsx (7:15) @ new newClient

   5 | import { Client } from '@spree/storefront-api-v2-sdk';
   6 | 
>  7 | class newClient extends Client {
     |               ^
   8 |   constructor(config: any) {
   9 |     super(config);
  10 |   }

Expected Behavior

The application should run / build successfully as it did in Next 11.

To Reproduce

  1. Install next 12 / typescript: npx create-next-app@latest --typescript poc
  2. Apply patch to reproduce the issue:
    diff --git a/package.json b/package.json
    index 2f98703..e2de6ca 100644
    --- a/package.json
    +++ b/package.json
    @@ -8,6 +8,7 @@
         "lint": "next lint"
       },
       "dependencies": {
    +    "@spree/storefront-api-v2-sdk": "^4.11.0",
         "next": "12.0.2",
         "react": "17.0.2",
         "react-dom": "17.0.2"
    diff --git a/pages/index.tsx b/pages/index.tsx
    index 72a4a59..23e7ec6 100644
    --- a/pages/index.tsx
    +++ b/pages/index.tsx
    @@ -2,6 +2,17 @@ import type { NextPage } from 'next'
     import Head from 'next/head'
     import Image from 'next/image'
     import styles from '../styles/Home.module.css'
    +import { Client } from '@spree/storefront-api-v2-sdk';
    +
    +class newClient extends Client {
    +  constructor(config: any) {
    +    super(config);
    +  }
    +}
    +
    +const v2Client = new newClient({
    +  host: 'https://localhost:3000/',
    +});
    
     const Home: NextPage = () => {
       return (
    
  3. install deps: npm install
  4. start the development server (npm dev) and visit the site

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
timneutkenscommented, Nov 9, 2021

Investigated this and found that the constructor helper in SWC does not account for native classes currently. @kdy1 is going to fix it 👍

2reactions
pboyercommented, Nov 3, 2021

I’m also seeing this. Definitely blocking us from adopting swc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class constructor cannot be invoked without 'new' in JS
What is this? To solve the "Class constructor cannot be invoked without 'new'" error, use the new operator when instantiating the class. The...
Read more >
Javascript ES6 TypeError: Class constructor Client cannot be ...
When I try to execute nodemon command I always see this error TypeError: Class constructor Client cannot be invoked without 'new'.
Read more >
TypeError: Class constructor Model cannot be invoked without
Error: · Class constructor ha cannot be invoked without 'new' at script.js in Trhee.js · ERROR TypeError: By.Subject is not a constructor.
Read more >
Errors | Node.js v19.3.0 Documentation
Node.js generates system errors when exceptions occur within its runtime ... Class: TypeError # ... A constructor for a class was called without...
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
There was an attempt to use an object or a variable as a constructor, but that object or variable is not a constructor....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found