简体   繁体   中英

How to set by default a script to run silently inside package.json in node.js (like npm run -s scriptname)?

As posed in the question, I know there exists the possibility to use npm run -s somescript, but I want to know if there's a way to have it already by default there, any ideas?

{
  "description": "Some node demo server",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "somescript": "echo \"I want this script to run silently\""
  },
  "author": "SomeDude"
}

Edit:

Sorry I did't specify well my needs, so I would like to just have:

> my-app@1.0.0 somescript /my-app  ##I don't care if this appears or not
I want this script to run silently  ## This would be echo's output, I want to see the echo output

But not:

> my-app@1.0.0 somescript /my-app  ##I don't care if this appears or not
> echo \"I want this script to run silently\" ## I don't want this line
I want this script to run silently ## I just want this, the ouput

dev null still makes this line which I don't want to appear:

> echo \"I want this script to run silently\" ## I don't want this

OS: Ubuntu 16.04 LTS

If you're script is running on Mac or Linux, you can add > /dev/null to the end of your script and it will print nothing to the console. Pretty silent if you ask me. Though I'd recommend that you have some kind of file logger so you can see any errors that happen.

example:

node server.js > /dev/null

What you need to note is that it will show you the command line its executing when you put it into the npm scripts.

```bash bjemilo:test$ npm run somescript

test@1.0.0 somescript /Users/bjemilo/Desktop/test echo "I want this script to run silently" > /dev/null ```

However removing the /dev/null producing this

```bash bjemilo:test$ npm run somescript

test@1.0.0 somescript /Users/bjemilo/Desktop/test echo "I want this script to run silently" > /dev/null

I want this script to run silently ```

This is expected behavior. But there is a work around if you add /dev/null to the npm command.

With npm in terminal

npm run somescript > /dev/null

It prints nothing.

Doing npm run --silent somescript does the same result

You could always just create a bash/shell script to run the command. npm scripts is there for convenience and it has nice hooks that can run before and after a script call.

{
  "description": "Some node demo server",
  "main": "server.js",
  "scripts": {
    "start": "node server.js",
    "somescript": "echo \"I want this script to run silently\" &> /dev/null  || true"
  },
  "author": "SomeDude"
}

If you're developing on Mac or Linux you could append &> /dev/null to your script command to silence the output from that script itself.

Also if you then append || true || true any errors bubbling up to npm will not show, thanks to npm believing the command completed successfully.

However, you will still see the output from npm itself as it calls that script, IE

you$ npm run-script somescript

> your-app@1.0.0 somescript /your-app
> echo "I want this script to run silently" &> /dev/null  || true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM