'npm run coverage fails with "require(...).internalModuleStat is not a function"

When we run our tests with npm run test, they pass. When we run with npm run coverage, though, some fail with this error:

'internal/modules/cjs/loader.js:58\n' +
    "const internalModuleStat = function (f) { return require('fs').internalModuleStat(f); };\n" +
    '                                                               ^\n' +
    '\n' +
    'TypeError: require(...).internalModuleStat is not a function\n' +
    '    at internalModuleStat (internal/modules/cjs/loader.js:58:64)\n' +
    '    at stat (internal/modules/cjs/loader.js:137:18)\n' +
    '    at Function.Module._findPath (internal/modules/cjs/loader.js:667:16)\n' +
    '    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:967:27)\n' +
    '    at Function.Module._load (internal/modules/cjs/loader.js:862:27)\n' +
    '    at Module.require (internal/modules/cjs/loader.js:1042:19)\n' +
    '    at Module._preloadModules (internal/modules/cjs/loader.js:1296:12)\n' +
    '    at loadPreloadModules (internal/bootstrap/pre_execution.js:449:5)\n' +
    '    at prepareMainThreadExecution (internal/bootstrap/pre_execution.js:73:3)\n' +
    '    at internal/bootstrap/pkg.js:7:1\n'

What can be going on here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source