Serge van den Oever's weblog

Prevent useless npm error output in your project

Wed Feb 16 2022 • ☕️ 2 min read • you like my writing? Buy me a coffee

When you write a node script that can be executed through npm, is is convenient to have an error exit code when for example a validation error occurs.

Assume we have an npm script validate as follows:

   "scripts": {
       "validate": "node validate.js"
   }

If we run npm run validate We get output like:

Development mode: false
Current working directory: C:\P\competenceframework\packages\content
ERROR: C:\P\competenceframework\packages\content\src\competenceframework-settings.json(1,1): SchemaValidationError - rings is not allowed.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @competenceframework/content@1.0.0 validate: `validate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @competenceframework/content@1.0.0 validate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\serge\AppData\Roaming\npm-cache\_logs\2022-02-15T23_23_29_477Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! competenceframework@1.0.0 validate: `cd packages && cd content && npm run validate`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the competenceframework@1.0.0 validate script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\serge\AppData\Roaming\npm-cache\_logs\2022-02-15T23_23_29_526Z-debug.log

A large part of the error output is useless, it is npm related.

To skip this output use the --silent flag when calling npm:

npm run validate --silent

The output will now be:

Development mode: false
Current working directory: C:\P\competenceframework\packages\content
ERROR: C:\P\competenceframework\packages\content\src\competenceframework-settings.json(1,1): SchemaValidationError - rings is not allowed.

If you run the script from a VSCode task validate, configured in .vscode/tasks.json it is possible to prevent the output as follows using the --silent flag:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Validate",
            "detail": "Validate all content and parse errors from output",
            "type": "npm",
            "script": "validate --silent",
            "problemMatcher": [
                {
                    "owner": "content-linter",
                    "fileLocation": ["autoDetect", "${workspaceFolder}"],
                    "pattern": {
                        "regexp": "^(ERROR|WARNING):\\s*(.*)\\((\\d+),(\\d+)\\):\\s+(.*)$",
                        "file": 2,
                        "line": 3,
                        "column": 4,
                        "severity": 1,
                        "message": 5
                    }
                }
            ],
            "options": {
                "statusbar": {
                    "label": "$(check-all) Validate",
                    "color": "#00FF00"
                }
            }
        }
    ]
}

It is also possible to suppress the npm error output in your project by adding a file .npmrc with the following content:

loglevel=silent

Using the above configuration the --silent flag is not required in the task configuration.

Discuss on TwitterEdit on GitHub

This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. You are free to share and adapt this work for non-commercial purposes, provided you give appropriate credit, provide a link to the license, and indicate if changes were made. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/4.0/.

Serge van den Oever's weblog

Serge van den Oever

Personal blog by Serge van den Oever - als je maar lol hebt...
X: @svdoever
LinkedIn: Serge van den Oever - articles on LinkedIn
GitHub: svdoever

Technology Consultant @ Macaw
2021-2024 Sitecore Technology MVP