使用NodeJS运行RollUp.js时回调函数报错“Uncaught TypeError: e is not a function”该如何解决?
Hey there! Let's dig into that Uncaught TypeError: e is not a function error you're hitting with Rollup and Node.js. This usually pops up when you’re expecting a function but getting something else (like undefined, an object, or a primitive value) instead. Let’s break down common culprits across your main.js, _checker.js, and intro.js files:
1. Misaligned Imports/Exports Between Modules
This is the most frequent cause. Double-check if you’re exporting a non-function value from one file but trying to invoke it as a callback in another:
- For example, if
_checker.jshas:
But inexport const validationChecker = "some config string";main.jsyou write:import { validationChecker } from "./_checker.js"; validationChecker(() => console.log("Done!")); // 💥 Error: validationChecker is a string, not a function - You might also mix up default and named exports. If
_checker.jsusesexport default {}but you import a named function that doesn’t exist, the imported value could beundefined—which will throw this error when you try to call it.
2. Issues with intro.js and Rollup’s Build Pipeline
Since intro.js is typically injected at the start of your Rollup bundle, it might be accidentally overriding or misinitializing the variable e (or your callback parameter that gets minified to e):
- If
intro.jsdefines a variableethat’s not a function (e.g.,const e = "bundle metadata";), this could clash with a callback parameter namedein your code. - Rollup’s minification or tree-shaking might also be removing your callback function entirely (thinking it’s unused), leaving
easundefinedat runtime. Try temporarily disabling minification (remove theterserplugin from your Rollup config) to test this.
3. Incorrect Callback Parameter Passing
You might be passing a non-function value where a callback is expected:
- For example, in
main.jsyou do:
Butimport { runCheck } from "./_checker.js"; runCheck({ timeout: 5000 }); // Passing an object instead of a function_checker.jsexpects a callback:export function runCheck(callback) { callback(); // 💥 Error if callback isn't a function } - Async operations (like Promises) are another common spot—if you pass a non-function to
.then()or.catch(), you’ll hit this exact error.
4. Variable Scoping Conflicts
If e is a callback parameter in your code, it might be getting overwritten by a higher-scoped variable with the same name:
const e = "global value"; someAsyncTask(e => { e(); // 💥 Here, `e` is supposed to be the callback parameter, but if the outer `e` is accidentally referenced instead (unlikely, but possible with messy scoping), it’ll fail });
Quick Debugging Tips
- Add type checks: Before invoking a callback, verify it’s actually a function. For example in
_checker.js:export function runCheck(callback) { console.log("Callback type:", typeof callback); // Check if this logs 'function' if (typeof callback === "function") { callback(); } else { console.error("Invalid callback received:", callback); } } - Inspect the bundled output: Look at Rollup’s final build file to see what
ecorresponds to—this will reveal if it’s been minified incorrectly or replaced with an unexpected value. - Validate exports: Double-check every
exportin_checker.jsandimportinmain.jsto ensure you’re pulling in the right function.
内容的提问来源于stack exchange,提问作者PatricNox




