js SyntaxError: Cannot use import statement outside a module"/>
Jest: node.js SyntaxError: Cannot use import statement outside a module
我刚刚开始学习
jest
测试并创建了一个示例应用程序来熟悉jest
测试。但是,我收到以下错误...
语言.js
const calculateTip = (total, percentage) => {
return total + ((percentage / 100) * total) + 1;
};
export {
calculateTip,
}
package.json
{
"dependencies": {
"express": "^4.18.2"
},
"type": "module",
"main": "src/app.js",
"scripts": {
"start": "node src/app.js",
"test": "cls && env-cmd -f ./envs/test.env jest --watchAll"
},
"devDependencies": {
"env-cmd": "^10.1.0",
"jest": "^29.2.2",
"supertest": "^6.3.1"
}
}
我已经尝试在谷歌上搜索解决方案,但到目前为止没有运气。
回答如下:Node.js 从 v16 开始只支持 esm 语法,jest 还不支持。 因此,您有 2 个选择:
- 不要使用 esm 语法 (
/import
)export
- 添加 jest-babel 及其配置,以将 esm 语法转换(转换)为 cjs 语法。
更多推荐
Jest: node.js SyntaxError: Cannot use import statement outside a module
发布评论