35

I have already installed Jest in my React project. I even installed jest-cli, but still can't use Jest in the command line. I can do an npm test, as it is in my package.json, but I have problems with the test, and in order to fix it I need to access the 'jest' command. (I need to run jest --init and to create the config file.) Thank you in advance!

See Getting Started - Jest.

Super Jade
  • 5,609
  • 7
  • 39
  • 61
Gergan Zhekov
  • 944
  • 1
  • 8
  • 27

5 Answers5

65

Try installing jest as a dev dependency.
npm install --save-dev jest

Then run it with npm test, (Edit the package.json file if necessary to add the script). Don't use jest command directly. It's not recommended.
If you want to use jest command use node_modules/.bin/jest.

Janaka Chathuranga
  • 1,742
  • 16
  • 19
  • 2
    This is how I installed it. I wanted to use a jest command, because I was looking at the official website and its instructions on setting it up (jest --init). It still doesn't run when I put node_modules/.bin/jest in. It says 'node_modules' is not recognized as an internal or external command, operable program or batch file.' – Gergan Zhekov Dec 05 '18 at 18:22
  • 1
    If you are using windows it is `node_modules\.bin\jest.exe` or something like that. – Janaka Chathuranga Dec 05 '18 at 18:31
  • jest should be in `/node/modules/.bin` if not try to re-install again using this answer post, running again this command solved my problem. – Ali Sherafat Dec 24 '21 at 20:18
43

I fixed this problem only after installing jest globally:

npm install -g jest
geoff
  • 531
  • 3
  • 6
9

You can use "node_modules\.bin\jest" --init if you are on Windows.

Super Jade
  • 5,609
  • 7
  • 39
  • 61
ThiRoss
  • 135
  • 1
  • 12
0

the problem got fixed when I installed it globally as an Admin (right-click CMD and run as admin) with the below command

npm install -g jest

then go to the test file and do a jest on it.

Tejaswi Pandava
  • 486
  • 5
  • 17
0

Use npx to solve the error "jest: command not found", e.g. npx jest or install the package globally by running npm install -g jest to be able to use the command without the npx prefix.