Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

> [email protected] 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:

> [email protected] 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

like image 369
sab Avatar asked Oct 17 '18 14:10

sab


People also ask

How do I run a npm script in package JSON?

To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.

What is npm install -- ignore scripts?

The --ignore-scripts argument will cause npm to not execute any scripts defined in the package. json. See scripts . The --legacy-bundling argument will cause npm to install the package such that versions of npm prior to 1.4, such as the one included with node 0.8, can install the package.

What is NPX and npm?

Npm is a tool that use to install packages. Npx is a tool that use to execute packages. Packages used by npm are installed globally. You have to care about pollution in the long term. Packages used by npx are not installed globally.

What is the use of scripts in package JSON?

Scripts are stored in a project's package. json file, which means they're shared amongst everyone using the codebase. They help automate repetitive tasks, and mean having to learn fewer tools. Node npm scripts also ensure that everyone is using the same command with the same flags.


1 Answers

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

[email protected] 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

[email protected] 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.

like image 159
jemiloii Avatar answered Nov 15 '22 04:11

jemiloii