jcollum jcollum - 1 year ago 82
JSON Question

How can I pipe to a bash alias from an npm script?

I have an alias in my .bashrc for bunyan:

$ alias bsh
alias bsh='bunyan -o short'

This line runs fine in bash:

$ coffee src/index.coffee | bsh

But if I put the same thing in 'scripts'

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"coffee":"coffee src/index.coffee | bsh"

npm run coffee
, it fails:

> coffee src/index.coffee | bsh

sh: bsh: command not found
throw er; // Unhandled 'error' event

Error: write EPIPE
at exports._errnoException (util.js:870:11)
at WriteWrap.afterWrite (net.js:769:14)

So at random I tried putting in
instead of
and it worked. I can't figure out why though. I don't have to escape pipe characters in JSON as far as I know.

However it doesn't actually pipe the output to the bsh alias.

The actual fix is to use
"coffee":"coffee src/index.coffee | bunyan -o short"
-- get rid of the alias completely.

How can I use a bash alias in an npm script?


You can create a function instead of an alias.

function bsh() {
    bunyan -o short
export -f bsh

The export will make it available to children processes.