110

I'm trying to run karma as part as an angular-seed project, after installing karma using

npm install -g karma

I get:

'karma' is not recognized as an internal or external command, operable program or batch file.

when i'm trying to run test.bat from angular-client\scripts, the content of this file is:

set BASE_DIR=%~dp0

karma start "%BASE_DIR%..\config\karma.conf.js" %*

I also tried to navigate to "\AppData\Roaming\npm\node_modules\karma\bin" and saw karma file, when I'm trying to run it I get again:

'karma' is not recognized as an internal or external command, operable program or batch file.

Any suggestions? If not please suggest how to use jasmine without karma.

Thanks.

Community
  • 1
  • 1
Liad Livnat
  • 7,435
  • 16
  • 60
  • 98

8 Answers8

325

The command line interface is in a separate package.

To install this use:

npm install -g karma-cli

MrCheese
  • 3,272
  • 1
  • 12
  • 11
  • 18
    Man they *really* don't keep these docs up to date :/ http://karma-runner.github.io/0.8/intro/installation.html (as 3 years after this answer the docs say `npm install -g karma && karma start` which does not work). – Stop Slandering Monica Cellio Apr 17 '14 at 21:56
  • 2
    @sequoiamcdowell docs for Karma are versioned, you are not going to see any updates for the old version. Check the docs for the last release http://karma-runner.github.io/0.12/intro/installation.html – pkozlowski.opensource Jul 17 '14 at 17:43
  • make sure to run this as admin – Jerry Oct 31 '14 at 01:47
  • 1
    I can't believe this slips through the cracks – Cody Nov 13 '14 at 23:22
  • I spent at least an hour scouring for a solution. I thought it was an .net framework issue. This solution will work even when you see the .net framework error: "MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framew ork 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it i s installed elsewhere." Luckily I found your solution and have gotten karma to load. Thank you! – ThinkBonobo Jan 19 '15 at 17:00
  • This is not true currently, there is a file in karma/bin/karma, which contains `require('../lib/cli').run();` and lib/cli.js contains cli tools. The global install works perfectly, the local install must use the path of this file. – inf3rno Oct 17 '15 at 20:23
  • Since `-g` installs this globally, use `karma start` to run the file, rather than the path to the command. – user2441511 Oct 28 '16 at 14:38
  • Equivalent if you are using yarn: `yarn global add karma-cli` – Martin Bamford Apr 09 '18 at 09:21
17

karma-cli

It is recommended to install karma with its Command-Line-Interface (karma-cli) which will take care of fetching the appropriate karma. You can also install a different local version specific to each project you're working on and karma-cli will pick the appropriate one.

From the karma installation page:

Typing ./node_modules/karma/bin/karma start sucks so you might find it useful to install karma-cli globally:

npm install -g karma-cli

Now, check that karma was installed by typing:

which karma  //echo something like: /usr/local/bin/karma

and check that karma server is working (ctr+c to quit):

karma start 

You can also check that karma was installed by going to this directory:

cd /usr/local/lib/node_modules/karma

Good luck!

Community
  • 1
  • 1
Lior Elrom
  • 19,660
  • 16
  • 80
  • 92
8

On windows when you install a npm (non-globally - so without the -g flag), the executable commands are linked in the node_modules\.bin folder.

For example:

powershell> .\node_modules\.bin\karma start

powershell> .\node_modules\.bin\karma run

  • 1
    I was getting additional permissions based errors doing it this way so I had to run the actual cmd file `.\node_modules\.bin\karma.cmd start` and that worked for me – Nick Tallents Apr 03 '20 at 14:34
6

I had the same issue and fixed it by correcting my PATH environment variable.

STEP 1: go to the following path and ensure karma.cmd is present at the location given below

[Nodejs folder path]\node_modules\.bin <=> C:\Program Files\nodejs\node_modules\.bin

STEP 2: If present go to STEP 3, If not present run the following command npm install -g karma

STEP 3: Open environment variables and edit PATH

STEP 4: Add the following at the end :

[Nodejs folder path]\node_modules\.bin; <=> "C:\Program Files\nodejs\node_modules\.bin"

Log out your session and it will work for sure.

demokritos
  • 1,416
  • 2
  • 12
  • 34
ajaysinghdav10d
  • 1,771
  • 3
  • 23
  • 33
4

Based upon your directory using \AppData\Roaming, you're on Windows and this error is usually because the path to the npm globals isn't in the Windows PATH or NODE_PATH environment variables.

Use SET to check the values you are using for the paths and if your npm directory isn't listed, that will be the issue.

If you don't want npm to save to this directory, check the npm configuration options and the npm folders docs to see what you can change in this regard...

Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
  • i already tried to add the npm to path using the following command "set PATH="C:\Users\Liad\AppData\Roaming\npm;%PATH%", well still nothing, can you tell me where is the excact location of your karma execution file? – Liad Livnat Dec 28 '13 at 14:57
  • On my Windows system, I have npm set to use `C:\npm`. Some of the Windows stuff I do is on XP which has path length problems, so I wanted to keep the total path as short as possible. If you are on XP, that may be a contributing factor...and maybe also on more recent versions of Windows (???). – Matthew Bakaitis Dec 28 '13 at 18:44
2

'karma' is not recognized as an internal or external command, operable program or batch file.

If the above mentioned solution does not work, than The cause of issue is previous version of nodejs. So uninstall the previous version of nodejs and re-install the latest version. It will resolve your issue. As I faced the same and by doing above changes it worked for me.

Thanks.

Neha
  • 170
  • 5
1

Official documentation at https://karma-runner.github.io/0.12/intro/installation.html is confusing. It implies that npm install -g karma-cli is to install karma globally but it actually required for to run karma from command line.

Maksood
  • 1,180
  • 14
  • 19
-1

I had same: 'karma' is not recognized as an internal or external command, operable program or batch file. problem when i tried to install it directly to my project file. When i used npm install -g karma-cli to global install everything worked just fine.