【问题标题】:Command 'jest' not found, did you mean: command 'test' from deb coreutils Try: sudo apt install <deb name>找不到命令“开玩笑”,您的意思是:来自 deb coreutils 的命令“测试”尝试:sudo apt install <deb name>
【发布时间】:2020-10-15 19:30:12
【问题描述】:

Jest 已安装,但当我尝试生成 jest.config.js 文件时,我收到此错误 Command 'jest' not found, did you mean:

command 'test' from deb coreutils

Try: sudo apt install &lt;deb name&gt; 所以我执行这个./node_modules/.bin/jest --init命令生成jest.config.js文件

【问题讨论】:

  • 使用 npx jest --init 从项目运行本地包。

标签: jestjs


【解决方案1】:

以下适用于jest以外的包,但我以这个为例。这些过程通常与大多数其他 Node 包类似。

在本地运行

问题是jest 是为这个项目安装的本地。有几种方法可以做到这一点:

npx(推荐)

像这样使用它的最简单方法是使用npx(请参阅:difference with npm),它将执行一个 Node 包。

npx jest --init

npx 工具将首先尝试本地安装的包,如果找不到,它将下载并一次性运行该包,然后将其删除。

将包添加到 package.json 中的scripts

注意:现在已经不需要npx。这是npx 引入之前的一种技术。为了获得更完整的列表,将其添加到此处。

基础包

这是 npx 的手动替代方案 - 可以将包添加到 package.json 中的 scripts 部分,因此它看起来像这样

{
    "name": "some name",
    "version": "0.0.0",
    "description": "",
    "author": "yourself",
    "scripts": {
        /* other scripts */
        "jest": "jest"
    },
    /* dependencies and other properties */
}

这样您就可以像使用npx 一样使用npm。请注意,您必须添加run 才能执行脚本:

npm run jest --init

它仍然会为操作启动本地 jest 安装。

添加一次性流程

作为替代方案,您可以将要运行的命令添加为脚本。然后你可以运行一次并从 package.json 中删除它。首先把文件改成这样

{
    "name": "some name",
    "version": "0.0.0",
    "description": "",
    "author": "yourself",
    "scripts": {
        /* other scripts */
        "oneoff": "jest --init"
    },
    /* dependencies and other properties */
}

然后就可以执行了

npm run oneoff

最后你可以删除"oneoff": "jest --init"

直接寻址包

如你所见,你也可以使用

./node_modules/.bin/jest --init

运行jest。这会起作用,但打字会更长,更烦人。另外,你必须知道包的目录在哪里。

在 Linux 上,./node_modules/.bin/ 目录也可以添加到PATH 环境变量中。它允许您只执行jest --init 或使用任何其他包,但这也是危险且不需要的。

全局运行(不推荐)

您可以全局安装该软件包。这将允许您独立于当前项目运行它:

npm install --global jest 

这仍然允许您使用jest --init,但它也可能存在问题。

  1. 它需要您安装一个额外的系统包。可能您没有此权限,或者您不想这样做。

  2. 如果您开始着手一个新项目,您仍然有一个 Node 包的全局安装。如果两个项目使用不同版本的 Node 包并且不兼容,您可能会遇到问题。

  3. 类似地,如果您升级项目中的节点包,或者升级全局的,但不能同时升级,那么您可能会遇到类似的不兼容问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-12
    • 2020-07-09
    • 1970-01-01
    • 2016-01-20
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多