Nodejs: TypeError: require().() 不是一个函数

时间:2021-05-27 12:11:11

标签: javascript node.js require

我正在尝试解决我尝试更新 package.json 中的 jest 时遇到的问题

开玩笑 26.6.3 → 27.0.1

我收到错误

TypeError: require(...).createTransformer is not a function
at Object.<anonymous> (/local/repo/elukchm/monorepo/packages/edf/jestPreprocess.js:34:40)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)

文件中提到的代码:

jestPreprocess.js

const babelOptions = {
...
};

module.exports = require("babel-jest").createTransformer(babelOptions);

在“babel-jest”内部

const createTransformer = userOptions => {
var _inputOptions$plugins, _inputOptions$presets;

const inputOptions =
userOptions !== null && userOptions !== void 0 ? userOptions : {};
const options = {
...
};

你能看看我应该改变什么吗?提前致谢。

1 个答案:

答案 0 :(得分:5)

[TestFixture] public class BusinessDaysTests { [TestCase("2021-06-04", 5, "2021-06-11", Nager.Date.CountryCode.GB)] [TestCase("2021-06-04", 6, "2021-06-14", Nager.Date.CountryCode.GB)] [TestCase("2021-05-28", 6, "2021-06-08", Nager.Date.CountryCode.GB)] // UK holidays 2021-05-31 [TestCase("2021-06-01", 3, "2021-06-06", Nager.Date.CountryCode.KW)] // Friday-Saturday weekend in Kuwait public void AddTests(DateTime initDate, int delayDays, DateTime expectedDate, Nager.Date.CountryCode countryCode) { var outputDate = initDate.AddBusinessDays(delayDays, countryCode); Assert.AreEqual(expectedDate, outputDate); } } switched to ESM,所以 babel-jest@27 现在正在获取整个导出范围,而不仅仅是默认范围。修复超级简单:

require