{"id":18978,"library":"autocreate","title":"autocreate","description":"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.","status":"active","version":"1.2.0","language":"javascript","source_language":"en","source_url":"git@github.com:pjeby/autocreate","tags":["javascript","class","classes","constructors","decorators","ES6","ES7","new","Babel"],"install":[{"cmd":"npm install autocreate","lang":"bash","label":"npm"},{"cmd":"yarn add autocreate","lang":"bash","label":"yarn"},{"cmd":"pnpm add autocreate","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"autocreate does not expose an ES module; CommonJS require is the only way to import. For TypeScript, use `import auto = require('autocreate');` or `const auto = require('autocreate')` with `esModuleInterop: false`.","wrong":"import auto from 'autocreate';","symbol":"default","correct":"const auto = require('autocreate');"},{"note":"The `@auto` decorator must be applied to the class declaration, not inside the class body. For Babel, enable decorators legacy or stage 0. TypeScript requires `experimentalDecorators: true`.","wrong":"class MyClass { @auto ... }","symbol":"auto","correct":"@auto class MyClass { ... }"},{"note":"In plain JavaScript, pass the function expression/class directly to `auto()`. For named functions, the return value replaces the original reference; do not rely on hoisting with `@auto`.","wrong":"auto(MyClass); // if used before function declaration","symbol":"auto (function call)","correct":"const MyClass = auto(function MyClass() { ... });"}],"quickstart":{"code":"const auto = require('autocreate');\n\n@auto\nclass MyClass {\n  constructor(name) {\n    this.name = name;\n  }\n  greet() {\n    return `Hello, ${this.name}!`;\n  }\n}\n\n// Without `new`:\nconst instance = MyClass('World');\nconsole.log(instance.greet()); // Hello, World!\n\n// With `new`:\nconst instance2 = new MyClass('Universe');\nconsole.log(instance2.greet()); // Hello, Universe!\n\n// Using __class_call__:\n@auto\nclass Counter {\n  constructor() {\n    this.count = 0;\n  }\n  __class_call__() {\n    return { count: 'function called' };\n  }\n}\n\nconsole.log(Counter()); // { count: 'function called' }\nconsole.log(new Counter().count); // 0","lang":"typescript","description":"Demonstrates basic usage of @auto decorator to omit `new`, and custom __class_call__ hook for different behavior when called as function."},"warnings":[{"fix":"Use `const MyClass = auto(class { ... })` instead of `@auto` for class expressions.","message":"The `@auto` decorator only works with class declarations, not class expressions. Using it on expressions yields undefined behavior.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Access constructor via `this.constructor` or store needed state on the prototype.","message":"When using `__class_call__`, `this` inside it refers to the prototype, not the constructor or new instance. This surprises many developers.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use a static async factory method instead.","message":"autocreate does not support asynchronous constructors. Since the constructor always returns an object, async construction requires external patterns.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually call parent's `__class_call__` from child's if needed.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If using ESM, use `createRequire` from 'module' or a bundler that handles CJS interop.","message":"The module uses `require('autocreate')` only; no ES module build is provided. Using `import` from an ESM context may fail unless using a bundler.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Export the class before applying `@auto`, or use a separate variable to hold the wrapped class.","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.","error":"TypeError: Class extends value undefined is not a constructor or null"},{"fix":"Use the functional form: `const MyClass = auto(MyClass)` after the class definition, or transpile with Babel/TypeScript with decorator support.","cause":"Trying to use `@auto` in an environment that does not support decorators (e.g., plain Node.js without transpilation).","error":"SyntaxError: Decorators are not supported in this environment"},{"fix":"Use `const auto = require('autocreate')` (CommonJS) or configure bundler to handle default exports from CJS.","cause":"Attempting to import autocreate as an ES module default import (`import auto from 'autocreate'`) instead of requiring it.","error":"TypeError: autocreate is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}