{"id":10938,"library":"get-func-name","title":"Function Name Retrieval Utility","description":"get-func-name is a specialized utility library within the ChaiJS ecosystem, designed to securely and consistently retrieve the name of a JavaScript function. It offers a single, reliable API that works across both Node.js and browser environments, addressing inconsistencies that can arise from direct property access or varying runtime behaviors. The current stable version is `3.0.0`, which marks a definitive transition to being an ES Module (ESM) exclusively. This follows a previous, temporary ESM introduction and subsequent reversion in the v2.x series. The library maintains a focused release cadence driven by maintenance, bug fixes, and critical updates, such as the security patch in v2.0.1. Its primary differentiator is its cross-platform compatibility and robustness in extracting function names, making it a foundational tool for introspection in testing frameworks and other JavaScript libraries.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/chaijs/get-func-name","tags":["javascript","get-func-name","chai util"],"install":[{"cmd":"npm install get-func-name","lang":"bash","label":"npm"},{"cmd":"yarn add get-func-name","lang":"bash","label":"yarn"},{"cmd":"pnpm add get-func-name","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Since v3.0.0, get-func-name is an ES Module (ESM) only. This is the correct way to import its named export. CommonJS `require` is not supported in v3+.","wrong":"const getFuncName = require('get-func-name');","symbol":"getFuncName","correct":"import { getFuncName } from 'get-func-name';"},{"note":"If `get-func-name` uses a named export for `getFuncName` (as is common for utility functions), attempting a default import will result in `undefined` or a runtime error. Use `import { getFuncName } from 'get-func-name';` instead.","wrong":"import getFuncName from 'get-func-name';","symbol":"getFuncName","correct":null},{"note":"A namespace import aggregates all named exports. For a named export like `getFuncName`, access it as a property of the namespace object (e.g., `getFuncNameModule.getFuncName`). `getFuncNameModule.default` would only be correct if the library also provided a default export for the function.","wrong":"const funcName = getFuncNameModule.default(myFunction);","symbol":"getFuncNameModule","correct":"import * as getFuncNameModule from 'get-func-name';\nconst funcName = getFuncNameModule.getFuncName(myFunction);"}],"quickstart":{"code":"import { getFuncName } from 'get-func-name';\n\n// A function with an explicit name\nfunction myNamedFunction() {\n  // ...\n}\n\n// An anonymous function assigned to a variable\nconst myAnonymousFunction = function() {\n  // ...\n};\n\n// An arrow function\nconst myArrowFunction = () => {\n  // ...\n};\n\n// A function nested in an IIFE, which might not have an inferable name\nconst nestedAnonymous = (function() {\n  return function() {};\n}());\n\nconsole.log(`Name of myNamedFunction: '${getFuncName(myNamedFunction)}'`);\nconsole.log(`Name of myAnonymousFunction: '${getFuncName(myAnonymousFunction)}'`);\nconsole.log(`Name of myArrowFunction: '${getFuncName(myArrowFunction)}'`);\nconsole.log(`Name of nestedAnonymous: '${getFuncName(nestedAnonymous)}'`);\n\n// Example with a class method\nclass MyClass {\n  static staticMethod() {}\n  instanceMethod() {}\n}\n\nconsole.log(`Name of staticMethod: '${getFuncName(MyClass.staticMethod)}'`);\nconsole.log(`Name of instanceMethod: '${getFuncName(new MyClass().instanceMethod)}'`);","lang":"javascript","description":"Demonstrates importing `getFuncName` and using it to retrieve names from various function types including named, anonymous, arrow functions, and class methods."},"warnings":[{"fix":"Migrate your import statements from `const getFuncName = require('get-func-name');` to `import { getFuncName } from 'get-func-name';` and ensure your project environment supports ESM (e.g., set `\"type\": \"module\"` in `package.json`).","message":"Version 3.0.0 is an ES Module (ESM) only. CommonJS `require()` is no longer supported. This breaks compatibility for projects not configured for ESM.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Always refer to the latest major version (v3.0.0+) for ESM usage. If strict CommonJS is required, stick to versions `<3.0.0` but be aware of older security fixes.","message":"The package briefly transitioned to ESM in a v2.x release, but then reverted to CommonJS in v2.0.2. This created temporary confusion regarding its module format before the definitive ESM-only release of v3.0.0.","severity":"gotcha","affected_versions":"2.0.1, 2.0.2"},{"fix":"Upgrade to `get-func-name@2.0.1` or newer. The latest `v3.0.0` also includes this fix and is recommended.","message":"A security vulnerability (GHSA-4q6p-r6v2-jvc5) was addressed in v2.0.1. Using versions prior to this fix might expose your application to potential security risks.","severity":"gotcha","affected_versions":"<2.0.1"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Change your import statement to `import { getFuncName } from 'get-func-name';` and ensure your file is treated as an ES Module (e.g., using `.mjs` extension or `\"type\": \"module\"` in `package.json`).","cause":"Attempting to use CommonJS `require()` syntax in an ES Module (ESM) context or with `get-func-name` v3.0.0+.","error":"ReferenceError: require is not defined"},{"fix":"Ensure you are using a named import: `import { getFuncName } from 'get-func-name';`. If you are using TypeScript, confirm your `tsconfig.json` `compilerOptions.esModuleInterop` is enabled for better module interoperability.","cause":"Incorrectly importing the `getFuncName` function, often by using a default import (`import getFuncName from 'get-func-name';`) when it's a named export, or vice versa.","error":"TypeError: (0, _getFuncName.default) is not a function OR TypeError: (0, get_func_name_1.getFuncName) is not a function"}],"ecosystem":"npm"}