42

I ran npm install on a project and it gives a number of errors and warnings that I want to catch, but the output is too long and being cut off so I can't view the full list in the terminal.

I tried redirect it to a file but the output is still being written to the terminal and I still get an output file which only list the dependency trees.

I also tried to pipe it to less in linux but it still run through many screens until stopped for continue.

I checked npm doc and it doesn't seem to have log functionality, what I want is to be able to log the exact output in a file, how can I do it?

EricS
  • 805
  • 1
  • 9
  • 20

2 Answers2

85

npm install 2>&1 | tee log.txt

The 2>&1 routes stderr to stdout, so everything will output in a single stream.

keithmo
  • 4,893
  • 1
  • 21
  • 17
  • Or also do nohup and check out nohup.out – Keefe Roedersheimer Oct 06 '15 at 05:28
  • 10
    Great, learned something new. BTW use `npm install > log.txt 2>&1` instead if want to silence the terminal output. – EricS Oct 06 '15 at 16:29
  • 2
    thanks, does anyone have a quick explanation for why *npm install >> foo.log* does not work? – Alexander Mills Feb 22 '17 at 17:58
  • @AlexanderMills I suppose because the output of the command is not the same as STDERR, so you would get just a partial output when using a simple append. – Kamafeather Jun 25 '19 at 16:20
  • @BenisonSam not sure, but I think yes; check this [SO question](https://stackoverflow.com/questions/8925323/redirection-of-standard-and-error-output-appending-to-the-same-log-file) – Kamafeather Jun 25 '19 at 16:23
  • I found this useful with other npm commands as well. For example, I'm using jsdoc-to-markdown, and I made an npm script `"scripts" { "doc": "jsdoc2md ./app.js 2>&1 | tee jsdoc.md" }` to create a markdown document for my app. – Joe Sadoski Aug 16 '20 at 20:49
  • `npm install --loglevel verbose > log.txt 2>&1` for detailed output `npm install --loglevel silly > log.txt 2>&1` for even more detailed – Siva Kannan Sep 08 '22 at 07:15
4

You may only be interested in the warnings and errors, if so try this:

The npm arg is --silent. Or npm config set loglevel warn if you want to only print warnings and errors. Or you can pipe it to /dev/null.

so you have 2 options:

  1. npm i --silent
  2. npm config set loglevel warn then npm i

References:

npm install should be quiet

Add option to hide summary output from npm install

xx1xx
  • 1,834
  • 17
  • 16