Function Name Retrieval Utility
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.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES Module (ESM) context or with `get-func-name` v3.0.0+.fixChange 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`). -
TypeError: (0, _getFuncName.default) is not a function OR TypeError: (0, get_func_name_1.getFuncName) is not a function
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.fixEnsure 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.
Warnings
- breaking 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.
- gotcha 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.
- gotcha 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.
Install
-
npm install get-func-name -
yarn add get-func-name -
pnpm add get-func-name
Imports
- getFuncName
const getFuncName = require('get-func-name');import { getFuncName } from 'get-func-name'; - getFuncName
import getFuncName from 'get-func-name';
- getFuncNameModule
const funcName = getFuncNameModule.default(myFunction);
import * as getFuncNameModule from 'get-func-name'; const funcName = getFuncNameModule.getFuncName(myFunction);
Quickstart
import { getFuncName } from 'get-func-name';
// A function with an explicit name
function myNamedFunction() {
// ...
}
// An anonymous function assigned to a variable
const myAnonymousFunction = function() {
// ...
};
// An arrow function
const myArrowFunction = () => {
// ...
};
// A function nested in an IIFE, which might not have an inferable name
const nestedAnonymous = (function() {
return function() {};
}());
console.log(`Name of myNamedFunction: '${getFuncName(myNamedFunction)}'`);
console.log(`Name of myAnonymousFunction: '${getFuncName(myAnonymousFunction)}'`);
console.log(`Name of myArrowFunction: '${getFuncName(myArrowFunction)}'`);
console.log(`Name of nestedAnonymous: '${getFuncName(nestedAnonymous)}'`);
// Example with a class method
class MyClass {
static staticMethod() {}
instanceMethod() {}
}
console.log(`Name of staticMethod: '${getFuncName(MyClass.staticMethod)}'`);
console.log(`Name of instanceMethod: '${getFuncName(new MyClass().instanceMethod)}'`);