目录

What should I do if require stops working?

Symptom

Below is a simple example:

function test(require) { // require is declared as a function parameter.
  let a = require('./test.js');
  let b = a(1, 2);
  this.setData({
    value: b
  });
}

The JS function fails to be called.

Cause: Webpack statically scans source code for the global require identifier to collect dependencies. When require is shadowed by a function parameter, the require call stops working entirely.

Solution

Move the require call out of the parameter-shadowed scope so it can be recognized during static analysis.