You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用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.js has:
    export const validationChecker = "some config string";
    
    But in main.js you 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.js uses export default {} but you import a named function that doesn’t exist, the imported value could be undefined—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.js defines a variable e that’s not a function (e.g., const e = "bundle metadata";), this could clash with a callback parameter named e in your code.
  • Rollup’s minification or tree-shaking might also be removing your callback function entirely (thinking it’s unused), leaving e as undefined at runtime. Try temporarily disabling minification (remove the terser plugin 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.js you do:
    import { runCheck } from "./_checker.js";
    runCheck({ timeout: 5000 }); // Passing an object instead of a function
    
    But _checker.js expects 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 e corresponds to—this will reveal if it’s been minified incorrectly or replaced with an unexpected value.
  • Validate exports: Double-check every export in _checker.js and import in main.js to ensure you’re pulling in the right function.

内容的提问来源于stack exchange,提问作者PatricNox

火山引擎 最新活动