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

TypeError: Class extends value #<Object> is not a constructor or null #567

Closed
jarda-svoboda opened this issue Feb 9, 2022 · 2 comments
Closed

Comments

@jarda-svoboda
Copy link

jarda-svoboda commented Feb 9, 2022

Hi,
i'm facing this issue with CJS bundles. I have two files:

// ./a.js

export default class A {
  constructor() {
    console.log('Hello world!');
  }
}
// ./b.js

import A from './a.cjs'; // should be evaluated dynamically

class B extends A {
  constructor() {
    super();
    console.log("How ya doin'?");
  }
}

new B();

Building two separate files using:
tsup ./a.js and tsup ./b.js --external ./a.cjs

Which produces desired cjs files in the dist folder.

The problem appears if I run:

$ node ./dist/b.cjs 
./dist/b.cjs:22
var B = class extends import_a.default {
                               ^

TypeError: Class extends value #<Object> is not a constructor or null

Expected output :

$ node ./dist/b.cjs
Hello world!
How ya doin'?

ESM works well.

Update: It works well if i remove __toESM from b.cjs:

// ./dist/b.cjs

// Changed this:
var import_a = __toESM(require("./a.cjs"), 1);

// To this:
var import_a = require("./a.cjs");
@onursagir
Copy link

onursagir commented Nov 24, 2022

same another workaround i've find to work is:

import A from './a.cjs';

//                 vvvv
class B extends A.default {
  constructor() {
    super();
    console.log("How ya doin'?");
  }
}

However it does upset my editor a little bit.

Edit: you may need to place // @ts-ignore above class B extends A.default {

@jarda-svoboda
Copy link
Author

jarda-svoboda commented Jul 9, 2023

splitting: true does the magic...

// tsup.config.js

import { defineConfig } from 'tsup';

export default defineConfig(
  /** @type {() => import('tsup').Options} */
  (options) => ({
    clean: true,
    dts: true,
    format: ['cjs', 'esm'],
    entry: ['src/index.ts'],
    minify: !options.watch,
    sourcemap: options.watch,
    splitting: true,
  }),
);

It works fine with this config!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants