How to solve Terminal error "command not found: jest"? - jestjs

I have to add jest locally to every single project folder I create in order to use it. I can't use the "jest" command on Terminal, but I can do "npm run test".
zsh: command not found: jest
Jest Error is installed, but it's in my ./node_modules directory. People have said in a previous discussion post that I should run the command
./node_modules/jest –updateSnapshot
However, this leads to the error:
zsh: permission denied: ./jest
I would like to run jest on Terminal. Please give some advice.

You could try to run it this way :
./node_modules/.bin/jest
If that doesn't work, try installing jest globally.
npm install -g jest

if you don't want to install jest globally,
(there are a couple of reasons why you do not want to install npm things globally - for example this one),
you can do:
npx jest <all your jest flags or files>

Related

Vite: why am I getting vite:command not found error?

I have installed vite in my vue.js app. I start the app by typing npm run dev in the main project directory. In the package.json this is defined as:
"dev": "vite"
but if I try do run this command (or eg. vite build) 'manually' from main directory, I get an error:
bash: vite: command not found
I also figured out that when I set a new script:
"build": "vite build"
I can run this command also, although, again, running it manually will result in error as above.
This seems quite illogical to me. Can anybody explain how is it possible?
If you didn't install vite globally (using npm install -g), then the vite command will be in node_modules/.bin in your main directory. The npm run command temporarily adds that folder to your PATH so it can execute that command. Try running ./node_modules/.bin/vite to run it without npm.

i am getting this error even after executing this command "npm install express-hbs --save"

Hey guys I am getting error even after executing the above command if there is any other command to resolve this error please let me know
Error: Cannot find module 'express-hbs'
Before You start using the package - express-hbs, You must install it properly, most likely You installed the package in the wrong place, outside of the project folder.
In the root project folder, run the command:
npm install express-hbs
try again to run nodemon

"npm run" commands fail on Windows

I am using Windows PowerShell ISE with administrator privileges. My project uses webpack, which is installed as a local dependency with npm. I have a "script" defined in package.json:
"build": "webpack --progress --config resources/assets/build/webpack.config.js",
When I execute npm run build I get the error message 'webpack' is not recognized as an internal or external command, but if I copy the webpack command and execute that it works fine. I'm not going to list my whole system PATH here, but it includes .\node_modules\.bin at the beginning. Clearly Windows has no problem finding webpack but Node for some reason can't.
I tried running npm run build in a privilege-less Command Line, no difference. I upgraded to the latest versions of Node and npm (13.0.1 and 6.12.1, respectively), to no avail. I even tried prefixing the command in package.json with ./node_modules/.bin/ but that didn't work.
Please don't suggest that I install webpack globally as that is not a real solution. Node is giving me this error with all locally-installed commands, not just webpack. I should be able to run commands locally through npm.
if you're using a recent version of npm, you can try
npx webpack ....
alternatively, you can install webpack locally and specify the exact path
node_modules/webpack/bin/webpack.js ....
In the end, I had to bite the bullet and install webpack globally.

Mocha "cannot find module" error

node v6.10.2, npm v3.10.3, mocha v3.1.2
my package.json has this in the scripts section:
"test": "mocha ./test/unit/* --recursive"
When I run npm test I get
> #hc/pos-onlineordering#0.1.0 test C:\_dev\pos-onlineordering\server
> mocha ./test/unit/* --recursive "--reporter" "dot"
module.js:471
throw err;
^
Error: Cannot find module 'C:\_dev\app\server\test\unit\deploy'
it just so happens that C:_dev\app\server\test\unit\deploy is a directory holding several test files in it.
Here's where it gets really fun though. If I enter mocha ./test/unit/* --recursive at the command prompt, it runs all the tests like I would expect. I need it to run as a package script because the running the unit tests are a part of the build and deploy scripts.
More weird: It works for every other team member, some on Macs, some on Windows. I've tried it in Windows Power Shell and in Git-Bash, running as an administrator.
I have deleted the node_modules folder and re-run npm install. I have also run
npm uninstall -D mocha && npm i -D mocha and nothing changes. I have even deleted the entire repository and re-cloned it.
So my assumption is that I clearly have something wrong with my computer or environment, but I don't even know where to look for what it could be at this point. How can I get mocha to run out of the package scripts?

How do you install and run Mocha, the Node.js testing module? Getting "mocha: command not found" after install

I'm having trouble getting Mocha to work as expected, and I'd love to say as documented, but there (appears) to not be much documentation on actually getting the thing running.
I've installed it using npm (both globally and locally), and each time when I run it I get:
$ mocha
mocha: command not found
Ok, so I figured it's not in my PATH, so I tried running it directly,
$ ./node_modules/mocha/bin/mocha
execvp(): No such file or directory
Finally, I tried hitting the other bin file, and got,
$ ./node_modules/mocha/bin/_mocha
path.existsSync is deprecated. It is now called `fs.existsSync`.
.
✔ 1 tests complete (1ms)
How can I just execute my tests with a single command? Vows seems to let you, but I've heard Mocha is the better choice, I just can't seem to get it working correctly.
And any thoughts on the error I got above in my third attempt?
Edit:
I'm running,
Ubuntu 11.10 64-bit
Node.js 0.7.5
npm 1.1.8
mocha 0.14.1
should 0.6.0
since npm 5.2.0, there's a new command "npx" included with npm that makes this much simpler, if you run:
npx mocha <args>
Note: the optional args are forwarded to the command being executed (mocha in this case)
this will automatically pick the executable "mocha" command from your locally installed mocha (always add it as a dev dependency to ensure the correct one is always used by you and everyone else).
Be careful though that if you didn't install mocha, this command will automatically fetch and use latest version, which is great for some tools (like scaffolders for example), but might not be the most recommendable for certain dependencies where you might want to pin to a specific version.
You can read more on npx here
Now, if instead of invoking mocha directly, you want to define a custom npm script, an alias that might invoke other npm binaries...
you don't want your library tests to fail depending on the machine setup (mocha as global, global mocha version, etc), the way to use the local mocha that works cross-platform is:
node node_modules/.bin/mocha
npm puts aliases to all the binaries in your dependencies on that special folder.
Finally, npm will add node_modules/.bin to the PATH automatically when running an npm script, so in your package.json you can do just:
"scripts": {
"test": "mocha"
}
and invoke it with
npm test
After further reading, and confirmation from Linus G Thiel above, I found I simply had to,
Downgrade to Node.js 0.6.12
And either,
Install Mocha as global
Add ./node_modules/.bin to my PATH
To run Mocha with mocha command from your terminal you need to install mocha globally on this machine:
npm install --global mocha
Then cd to your projectFolder/test and run mocha yourTestFileName.js
If you want to make mocha available inside your package.json as a development dependency:
npm install --save-dev mocha
Then add mocha to your scripts inside package.json.
"scripts": {
"test": "mocha"
},
Then run npm test inside your terminal.
While installing the node modules for mocha I had tried the below commands
npm install
npm install mocha
npm install --save-dev mocha
npm install mocha -g # to install it globally also
and on running or executing the mocha test I was trying
mocha test
npm run test
mocha test test\index.test.js
npm test
but I was getting the below error as:
'Mocha' is not recognized as internal or external command
So , after trying everything it came out to be just set the path to environment variables under the System Variables as:
C:\Program Files\nodejs\
and it worked :)
For windows :
Package.json
"scripts": {
"start": "nodemon app.js",
"test": "mocha"
},
then run the command
npm run test
Late answer but I think will work.
Install mocha globally
npm install --global mocha
If you have already installed mocha then set the path to bin

Resources