-2

Jest is installed but when I was trying to generate jest.config.js file I was getting this error Command 'jest' not found, did you mean:

command 'test' from deb coreutils

Try: sudo apt install <deb name> So I execute this ./node_modules/.bin/jest --init command to generate jest.config.js file

skyboyer
  • 15,149
  • 4
  • 41
  • 56

1 Answers1

2

The following is applicable for packages other than jest but I am using this one as an example. The processes are usually analogous for most other Node packages.

Running locally

The issue is that jest is installed locally just for this project. There are few ways to do this:

npx (recommended)

The easiest way to use it like this is to use npx (see: difference with npm) which will execute a Node package.

npx jest --init

The npx tool will first try the locally installed packages and if not found, it will download and run the package as a one off, then remove it.

Adding the package to scripts in package.json

Note: this is not needed nowadays with the existence of npx. It was a technique from before npx was introduced. Adding it here for the sake of having a more complete list.

Base package

This is a manual alternative to npx - a package can be added to the scripts section in package.json so it looks like this

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

With that you will be able to use npm as if it's npx. Note that you have to add run to execute the script:

npm run jest --init

it still launches the local jest installation for the operation.

Adding a one off process

As an alternative, the command you want to run can be added as a script. Then you can run it once and delete it from package.json. First alter the file to look like this

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

Then you can execute

npm run oneoff

And finally you can delete "oneoff": "jest --init"

Directly addressing the package

As you found out, you can also use

./node_modules/.bin/jest --init

to run jest. This will work but it is longer and more annoying to type. Also, you have to know where the directory of the package is.

On Linux, the directory ./node_modules/.bin/ can also be added to the PATH environment variable. It allows you to just execute jest --init or use any other package, however it's also dangerous and unneeded.

Running globally (not recommended)

You can install the package globally. This will allows you to run it independently from your current project:

npm install --global jest 

This will still allow you to use jest --init, however it is also potentially problematic.

  1. It requires you to install an extra system package. It's possible you don't have permissions for this or otherwise you don't want to do it.

  2. If you start working on a new project, you still have a single global installation of the Node package. If both projects use different versions of the Node package and those are incompatible, you might run into problems.

  3. Similarly, if you upgrade the node package in the project, or the global one but not both, then you might have a similar problem with incompatibilities.

VLAZ
  • 18,437
  • 8
  • 35
  • 54