3

I am very new to node.js, npm and JavaScript but for code coverage trying with Istanbul tool and hence installed nyc with npm i nyc referring to https://www.npmjs.com/package/nyc which outputs like

C:\Users\user>npm i nyc
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\user\package.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\user\package.json'
npm WARN user No description
npm WARN user No repository field.
npm WARN user No README data
npm WARN user No license field.

+ nyc@15.1.0
updated 1 package in 1.334s

9 packages are looking for funding
  run `npm fund` for details

But when I hit nyc o command prompt, it says - nyc is not recognized as internal or external command.

As per doc, I added dev dependency with following but seeing same error.

C:\Users\user>npm i -D nyc
npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\user\package.json'
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN enoent ENOENT: no such file or directory, open 'C:\Users\user\package.json'
npm WARN user No description
npm WARN user No repository field.
npm WARN user No README data
npm WARN user No license field.

+ nyc@15.1.0
added 142 packages from 99 contributors in 48.325s

9 packages are looking for funding
  run `npm fund` for details

Please guide me what is wrong here.

Alpha
  • 13,320
  • 27
  • 96
  • 163

4 Answers4

2

you can try npm install -g nyc in case you want to save in globally

Dhruv Pal
  • 849
  • 2
  • 10
  • 25
undefined
  • 31
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 07 '21 at 15:50
  • Did you mean `npm install -g nyc` ? – Rabin Mallilck Apr 04 '22 at 14:24
1

There are 2 ways to make this work

  1. Install nyc as a dev-dependency
  2. Install nyc as a global dependency

NOTE: For me installing it as a dev-dependency is much better.

Let's run npm i nyc -D . What is this going to do? This is going to locate this package in your package.json, and node_modules. The reason you can't run it through cli, is the fact that it's not registered in system variables. So how do we run it, you might say? We create a script in package.json

scripts:{
  "nyc: "nyc"
}

And to run it, just npm run nyc

Now let's install it as a global dependency. Installing a package as a global dependency, means we don't add the package to our package.json, and node_modules, but if it's CLI, then it registers itself as a system variable.

npm i -g nyc
nyc
Tiko
  • 1,241
  • 11
  • 19
0

Goto the root folder of your project.
Run npm init, this will create the package.json file.
Then run npm i nyc.

If you want to install it as a dev dependency use below command.

npm install --save-dev nyc
  • I did `npm init -y` and then `npm i nyc` but still for `nyc` on command prompt, gives same error. – Alpha Aug 31 '20 at 05:15
  • We have installed nyc as devDependency. You should write your command i.e `nyc` in package.json scripts section and run it using `npm run ` command. For detailed info please read this [link](https://www.npmjs.com/package/nyc#installation--usage). – Subham Rajput Sep 01 '20 at 12:13
0

I had the same issue. In my case, it was because I had a space in my windows user name. It was "firstName lastName".

I have two solutions for that.

  1. You can install wsl and Ubuntu LTE from win store and then add extensions to visual code, so you will be able to run Linux terminal. It will hoverer require to install node for Linux and all other staff.
  2. You can create new windows user and use VS Code in 'run as' mode. (shift and right-click). If you don't have your VS Code installed for all users then you would have to add access for this newly created user.

I went with the second solution and it worked for me.