简体   繁体   English

如何默认设置一个脚本在node.js中的package.json中静默运行(如npm run -s scriptname)?

[英]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? 正如问题所提出的,我知道有可能使用npm run -s somescript,但我想知道是否有办法在默认情况下使用它,任何想法?

{
  "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: dev null仍然使我不希望出现的这一行:

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

OS: Ubuntu 16.04 LTS 操作系统: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. 如果您的脚本在Mac或Linux上运行,则可以在脚本的末尾添加> /dev/null ,它将不会向控制台打印任何内容。 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. 您需要注意的是,当您将其放入npm脚本时,它将向您显示其执行的命令行。

```bash bjemilo:test$ npm run somescript ```bash bjemilo:测试$ npm运行somescript

test@1.0.0 somescript /Users/bjemilo/Desktop/test echo "I want this script to run silently" > /dev/null ``` test@1.0.0 somescript / Users / bjemilo / Desktop / test echo“我希望这个脚本以静默方式运行”> / dev / null```

However removing the /dev/null producing this 但是删除/ dev / null产生这个

```bash bjemilo:test$ npm run somescript ```bash bjemilo:测试$ npm运行somescript

test@1.0.0 somescript /Users/bjemilo/Desktop/test echo "I want this script to run silently" > /dev/null test@1.0.0 somescript / Users / bjemilo / Desktop / test echo“我希望这个脚本以静默方式运行”> / 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. 但是,如果将/ dev / null添加到npm命令,则有一个解决方法。

With npm in terminal 在终端的npm

npm run somescript > /dev/null

It prints nothing. 它什么都不打印。

Doing npm run --silent somescript does the same result npm run --silent somescript做同样的结果

You could always just create a bash/shell script to run the command. 您可以随时创建一个bash / shell脚本来运行该命令。 npm scripts is there for convenience and it has nice hooks that can run before and after a script call. npm脚本是为了方便起见,它有很好的钩子,可以在脚本调用之前和之后运行。

{
  "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. 如果您在Mac或Linux上进行开发,可以在脚本命令中附加&> /dev/null ,以使该脚本本身的输出静音。

Also if you then append || true 如果你然后追加|| true || true any errors bubbling up to npm will not show, thanks to npm believing the command completed successfully. || true任何冒泡到npm的错误都不会显示,这要归功于npm相信命令已成功完成。

However, you will still see the output from npm itself as it calls that script, IE 但是,当你调用那个脚本IE时,你仍会看到npm本身的输出

you$ npm run-script somescript

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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