Search code examples
node.jsnpmwebpackfile-descriptorio-redirection

Input redirection in npm


I'm using webpack npm module to run an app. I wanted to redirect the output of npm run dev to a log file instead of console. At first I did
npm run dev 2>>server.log
but that didn't work. Then I tried
npm run dev >> server.log
This time I got logs but not all of them.

Expected logs

> truffle-init-webpack@0.0.1 dev /home/ishi/chapter2
> webpack-dev-server --host 0.0.0.0

Project is running at http://0.0.0.0:8080/
webpack output is served from /
Hash: ce4d448423d93946f04d
Version: webpack 2.2.1
Time: 4111ms
     Asset     Size  Chunks                    Chunk Names
    app.js  1.35 MB       0  [emitted]  [big]  main
index.html  1.29 kB          [emitted]         
chunk    {0} app.js (main) 1.32 MB [entry] [rendered]
   [82] ./~/web3/index.js 193 bytes {0} [built]
   [86] ./app/javascripts/app.js 3.56 kB {0} [built]
   [87] (webpack)-dev-server/client?http://0.0.0.0:8080 4.9 kB {0} [built]
   [88] ./build/contracts/Voting.json 4.08 kB {0} [built]
   [90] ./~/ansi-regex/index.js 135 bytes {0} [built]
  [129] ./~/punycode/punycode.js 14.7 kB {0} [built]
  [132] ./~/querystring-es3/index.js 127 bytes {0} [built]
  [161] ./~/strip-ansi/index.js 161 bytes {0} [built]
  [164] ./app/stylesheets/app.css 913 bytes {0} [built]
  [171] ./~/truffle-contract/index.js 2.64 kB {0} [built]
  [206] ./~/url/url.js 23.3 kB {0} [built]
  [241] (webpack)-dev-server/client/overlay.js 3.59 kB {0} [built]
  [242] (webpack)-dev-server/client/socket.js 856 bytes {0} [built]
  [244] (webpack)/hot/emitter.js 77 bytes {0} [built]
  [246] multi (webpack)-dev-server/client?http://0.0.0.0:8080 ./app/javascripts/app.js 40 bytes {0} [built]
     + 232 hidden modules
webpack: Compiled successfully.

Actual logs In first case

<empty log file>

Actual logs In second case

  [161] ./~/strip-ansi/index.js 161 bytes {0} [built]
  [164] ./app/stylesheets/app.css 913 bytes {0} [built]
  [171] ./~/truffle-contract/index.js 2.64 kB {0} [built]
  [206] ./~/url/url.js 23.3 kB {0} [built]
  [241] (webpack)-dev-server/client/overlay.js 3.59 kB {0} [built]
  [242] (webpack)-dev-server/client/socket.js 856 bytes {0} [built]
  [244] (webpack)/hot/emitter.js 77 bytes {0} [built]
  [246] multi (webpack)-dev-server/client?http://0.0.0.0:8080 ./app/javascripts/app.js 40 bytes {0} [built]
     + 232 hidden modules
webpack: Compiled successfully.

Solution

  • npm run dev 2>>server.log did not work because you were redirecting STDERR to server.log. 2>> means append error messages to server.log. Since there were no errors in your webpack build, nothing appeared in your file. Take a look at further resources on STDIN, STDOUT, STDERR and redirection: https://workaround.org/linuxtip/pipes

    As you mentioned in the comments you were using tail command to show the contents of the file. tails by default shows the last 10 lines of a file. In order to display the entire file use cat command.