autocreate

raw JSON →
1.2.0 verified Sat Apr 25 auth: no javascript

autocreate is a JavaScript utility that allows classes to be called without the `new` keyword, and provides a hook method (`__class_call__`) to customize behavior when invoked as a plain function. Current stable version is 1.2.0, with a low release cadence. It supports ES7/Babel decorators, TypeScript, CoffeeScript, and plain JavaScript, targeting ES5/ES6+ environments. Unlike similar libraries (e.g., `newless`), autocreate handles inheritance, static properties, and non-enumerable descriptors without external dependencies. It provides both decorator and manual wrapping patterns for maximum flexibility.

error TypeError: Class extends value undefined is not a constructor or null
cause Using `@auto` on a class that is exported as default and then extended in another module, where the import resolves to the wrapped version twice.
fix
Export the class before applying @auto, or use a separate variable to hold the wrapped class.
error SyntaxError: Decorators are not supported in this environment
cause Trying to use `@auto` in an environment that does not support decorators (e.g., plain Node.js without transpilation).
fix
Use the functional form: const MyClass = auto(MyClass) after the class definition, or transpile with Babel/TypeScript with decorator support.
error TypeError: autocreate is not a function
cause Attempting to import autocreate as an ES module default import (`import auto from 'autocreate'`) instead of requiring it.
fix
Use const auto = require('autocreate') (CommonJS) or configure bundler to handle default exports from CJS.
gotcha The `@auto` decorator only works with class declarations, not class expressions. Using it on expressions yields undefined behavior.
fix Use `const MyClass = auto(class { ... })` instead of `@auto` for class expressions.
gotcha When using `__class_call__`, `this` inside it refers to the prototype, not the constructor or new instance. This surprises many developers.
fix Access constructor via `this.constructor` or store needed state on the prototype.
gotcha autocreate does not support asynchronous constructors. Since the constructor always returns an object, async construction requires external patterns.
fix Use a static async factory method instead.
gotcha Inheritance with custom `__class_call__` may not propagate correctly if parent uses `@auto` and child overrides `__class_call__`. The child's `__class_call__` replaces the parent's.
fix Manually call parent's `__class_call__` from child's if needed.
gotcha The module uses `require('autocreate')` only; no ES module build is provided. Using `import` from an ESM context may fail unless using a bundler.
fix If using ESM, use `createRequire` from 'module' or a bundler that handles CJS interop.
npm install autocreate
yarn add autocreate
pnpm add autocreate

Demonstrates basic usage of @auto decorator to omit `new`, and custom __class_call__ hook for different behavior when called as function.

const auto = require('autocreate');

@auto
class MyClass {
  constructor(name) {
    this.name = name;
  }
  greet() {
    return `Hello, ${this.name}!`;
  }
}

// Without `new`:
const instance = MyClass('World');
console.log(instance.greet()); // Hello, World!

// With `new`:
const instance2 = new MyClass('Universe');
console.log(instance2.greet()); // Hello, Universe!

// Using __class_call__:
@auto
class Counter {
  constructor() {
    this.count = 0;
  }
  __class_call__() {
    return { count: 'function called' };
  }
}

console.log(Counter()); // { count: 'function called' }
console.log(new Counter().count); // 0