nyc 0,0,1,6,0,3 editorconfig travis-ci npm

the Istanbul command line interface

2 years after ISC


Join the chat at Build Status Coverage Status NPM version Windows Tests Conventional Commits

Istanbul's state of the art command line interface, with support for:

Instrumenting your code

You can install nyc as a development dependency and add it to the test stanza in your package.json.

npm i nyc --save-dev
  "script": {
    "test": "nyc mocha"

Alternatively, you can install nyc globally and use it to execute npm test:

npm i nyc -g
nyc npm test

nyc accepts a wide variety of configuration arguments, run nyc --help for thorough documentation.

Configuration arguments should be provided prior to the program that nyc is executing. As an example, the following command executes npm test, and indicates to nyc that it should output both an lcov and a text-lcov coverage report.

nyc --reporter=lcov --reporter=text-lcov npm test

Accurate stack traces using source maps

When produce-source-map is set to true, then the instrumented source files will include inline source maps for the instrumenter transform. When combined with source-map-support, stack traces for instrumented code will reflect their original lines.

Support for custom require hooks (babel, webpack, etc.)

nyc supports custom require hooks like babel-register. nyc can load the hooks for you, using the --require flag.

Source maps are used to map coverage information back to the appropriate lines of the pre-transpiled code. You'll have to configure your custom require hook to inline the source map in the transpiled code. For Babel that means setting the sourceMaps option to inline.

Use with babel-plugin-istanbul for ES2015+ Support

babel-plugin-istanbul can be used to enable first-class ES2015+ support.

  1. enable the babel-plugin-istanbul plugin:

      "babel": {
        "presets": ["es2015"],
        "env": {
          "test": {
            "plugins": ["istanbul"]

    Note: With this configuration, the Istanbul instrumentation will only be active when NODE_ENV or BABEL_ENV is test.

    We recommend using the cross-env package to set these environment variables in your package.json scripts in a way that works cross-platform.

  2. disable nyc's instrumentation and source-maps, e.g. in package.json:

    "nyc": {
      "require": [
      "sourceMap": false,
      "instrument": false
    "scripts": {
      "test": "cross-env NODE_ENV=test nyc mocha"

That's all there is to it, better ES2015+ syntax highlighting awaits:

Support for alternate file extensions (.jsx, .es6)

Supporting file extensions can be configured through either the configuration arguments or with the nyc config section in package.json.

nyc --extension .jsx --extension .es6 npm test
  "nyc": {
    "extension": [

Checking coverage

nyc can fail tests if coverage falls below a threshold. After running your tests with nyc, simply run:

nyc check-coverage --lines 95 --functions 95 --branches 95

nyc also accepts a --check-coverage shorthand, which can be used to both run tests and check that coverage falls within the threshold provided:

nyc --check-coverage --lines 100 npm test

The above check fails if coverage falls below 100%.

Running reports

Once you've run your tests with nyc, simply run:

nyc report

To view your coverage report:

you can use any reporters that are supported by istanbul:

nyc report --reporter=lcov

Excluding files

You can tell nyc to exclude specific files and directories by adding an nyc.exclude array to your package.json. Each element of the array is a glob pattern indicating which paths should be omitted.

Globs are matched using micromatch.

For example, the following config will exclude any files with the extension .spec.js, and anything in the build directory:

  "nyc": {
    "exclude": [

Note: Since version 9.0 files under node_modules/ are excluded by default. add the exclude rule !**/node_modules/ to stop this.

Note: exclude defaults to ['test', 'test{,-*}.js', '**/*.test.js', '**/__tests__/**', '**/node_modules/**'], which would exclude test/__tests__ directories as well as test.js, *.test.js, and test-*.js files. Specifying your own exclude property overrides these defaults.

Including files

As an alternative to providing a list of files to exclude, you can provide an include key with a list of globs to specify specific files that should be covered:

  "nyc": {
    "include": ["**/build/umd/moment.js"]

Note: include defaults to ['**']

Use the --all flag to include files that have not been required in your tests.

Require additional modules

The --require flag can be provided to nyc to indicate that additional modules should be required in the subprocess collecting coverage:

nyc --require babel-register --require babel-polyfill mocha


You can run nyc with the optional --cache flag, to prevent it from instrumenting the same files multiple times. This can significantly improve runtime performance.

Configuring nyc

Any configuration options that can be set via the command line can also be specified in the nyc stanza of your package.json, or within a .nycrc file:


  "description": "These are just examples for demonstration, nothing prescriptive",
  "nyc": {
    "check-coverage": true,
    "lines": 99,
    "statements": 99,
    "functions": 99,
    "branches": 99,
    "include": [
    "exclude": [
    "reporter": [
    "require": [
    "extension": [
    "cache": true,
    "all": true,
    "report-dir": "./alternative"

High and low watermarks

Several of the coverage reporters supported by nyc display special information for high and low watermarks:

  • high-watermarks represent healthy test coverage (in many reports this is represented with green highlighting).
  • low-watermarks represent sub-optimal coverage levels (in many reports this is represented with red highlighting).

You can specify custom high and low watermarks in nyc's configuration:

  "nyc": {
    "watermarks": {
      "lines": [80, 95],
      "functions": [80, 95],
      "branches": [80, 95],
      "statements": [80, 95]

Other advanced features

Take a look at and please feel free to contribute documentation.

Integrating with coveralls is a great tool for adding coverage reports to your GitHub project. Here's how to get nyc integrated with coveralls and

  1. add the coveralls and nyc dependencies to your module:

    npm install coveralls nyc --save-dev
  2. update the scripts in your package.json to include these bins:

     "script": {
       "test": "nyc mocha",
       "coverage": "nyc report --reporter=text-lcov | coveralls"
  3. For private repos, add the environment variable COVERALLS_REPO_TOKEN to travis.

  4. add the following to your .travis.yml:

    after_success: npm run coverage

That's all there is to it!

Note: by default adds comments to pull-requests on GitHub, this can feel intrusive. To disable this, click on your repo on and uncheck LEAVE COMMENTS?.

Integrating with codecov

nyc npm test && nyc report --reporter=text-lcov > coverage.lcov && codecov

codecov is a great tool for adding coverage reports to your GitHub project, even viewing them inline on GitHub with a browser extension:

Here's how to get nyc integrated with codecov and

  1. add the codecov and nyc dependencies to your module:

    npm install codecov nyc --save-dev
  2. update the scripts in your package.json to include these bins:

     "script": {
       "test": "nyc tap ./test/*.js",
       "coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
  3. For private repos, add the environment variable CODECOV_TOKEN to travis.

  4. add the following to your .travis.yml:

    after_success: npm run coverage

That's all there is to it!

More tutorials

You can find more tutorials at

Related Repositories



Import public NYC taxi and Uber trip data into PostgreSQL / PostGIS database, an ...



NYC Citi Bike system data and analysis ...



Notebooks (and slides) for my PyData NYC 2014 tutorial on the more advanced feat ...



Pydata NYC 2014 Scikit Learn Tutorial ...



The New York City Civic Innovation Fellows program help connect college students ...

Top Contributors

bcoe jamestalmage novemberborn greenkeeperio-bot Lalem001 isaacs addaleax JaKXz kentcdodds jasisk lloydcotten rmg gr2m rundef ronkorving shackpank zertosh doque JimmyBoh scriptdaemon gotwarlost rgbkrk matheusrocha89 rpominov rapzo SimenB dtinth mourner waldyrious


package version
archy ^1.0.0
arrify ^1.0.1
caching-transform ^1.0.0
convert-source-map ^1.3.0
debug-log ^1.0.1
default-require-extensions ^1.0.0
find-cache-dir ^0.1.1
find-up ^2.1.0
foreground-child ^1.5.3
glob ^7.0.6
istanbul-lib-coverage ^1.1.1
istanbul-lib-hook ^1.0.7
istanbul-lib-instrument ^1.7.4
istanbul-lib-report ^1.1.1
istanbul-lib-source-maps ^1.2.1
istanbul-reports ^1.1.1
md5-hex ^1.2.0
merge-source-map ^1.0.2
micromatch ^2.3.11
mkdirp ^0.5.0
resolve-from ^2.0.0
rimraf ^2.5.4
signal-exit ^3.0.1
spawn-wrap ^1.3.8
test-exclude ^4.1.1
yargs ^8.0.1
yargs-parser ^5.0.0
dev any-path ^1.3.0
bundle-dependencies ^1.0.2
chai ^3.0.0
coveralls ^2.11.11
exists-sync 0.0.4
forking-tap ^0.1.1
is-windows ^1.0.0
lodash ^4.12.0
mocha ^3.2.0
newline-regex ^0.2.1
requirejs ^2.3.0
sanitize-filename ^1.5.3
sinon ^2.1.0
source-map-support ^0.4.6
split-lines ^1.0.0
standard ^9.0.2
standard-version ^4.0.0
tap ^10.0.0
which ^1.2.11
zero-fill ^2.2.3


-   v8.3.1 zip tar
-   v8.3.0 zip tar
-   v8.2.0 zip tar
-   v8.1.0 zip tar
-   v8.0.0 zip tar
-   v7.1.0 zip tar
-   v7.0.0 zip tar
-   v6.6.1 zip tar
-   v6.6.0 zip tar
-   v6.5.1 zip tar
-   v6.5.0 zip tar
-   v6.4.4 zip tar
-   v6.4.3 zip tar
-   v6.4.2 zip tar
-   v6.4.1 zip tar
-   v6.4.0 zip tar
-   v6.3.0 zip tar
-   v6.2.1 zip tar
-   v6.2.0 zip tar
-   v6.1.1 zip tar
-   v6.1.0 zip tar
-   v6.0.0 zip tar
-   v5.6.0 zip tar
-   v5.5.0 zip tar
-   v5.4.0 zip tar
-   v5.3.0 zip tar
-   v5.2.0 zip tar
-   v5.1.1 zip tar
-   v5.1.0 zip tar
-   v5.0.1 zip tar