Jest: node.js SyntaxError: Cannot use import statement outside a module

编程入门 行业动态 更新时间:2024-10-06 14:27:10

Jest: node.<a href=https://www.elefans.com/category/jswz/34/1771451.html style=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 个选择:

  1. 不要使用 esm 语法 (
    import
    /
    export
    )
  2. 添加 jest-babel 及其配置,以将 esm 语法转换(转换)为 cjs 语法。

更多推荐

Jest: node.js SyntaxError: Cannot use import statement outside a module

本文发布于:2024-05-30 07:06:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js   node   Jest   SyntaxError   module

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!