繁体   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)?

正如问题所提出的,我知道有可能使用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"
}

编辑:

对不起,我没有说明我的需求,所以我想:

> 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

但不是:

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

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

操作系统:Ubuntu 16.04 LTS

如果您的脚本在Mac或Linux上运行,则可以在脚本的末尾添加> /dev/null ,它将不会向控制台打印任何内容。 如果你问我,你会很安静 虽然我建议您使用某种文件记录器,这样您就可以看到发生的任何错误。

例:

node server.js > /dev/null

您需要注意的是,当您将其放入npm脚本时,它将向您显示其执行的命令行。

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

test@1.0.0 somescript / Users / bjemilo / Desktop / test echo“我希望这个脚本以静默方式运行”> / dev / null```

但是删除/ dev / null产生这个

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

test@1.0.0 somescript / Users / bjemilo / Desktop / test echo“我希望这个脚本以静默方式运行”> / dev / null

我希望这个脚本以静默方式运行```

这是预期的行为。 但是,如果将/ dev / null添加到npm命令,则有一个解决方法。

在终端的npm

npm run somescript > /dev/null

它什么都不打印。

npm run --silent somescript做同样的结果

您可以随时创建一个bash / shell脚本来运行该命令。 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"
}

如果您在Mac或Linux上进行开发,可以在脚本命令中附加&> /dev/null ,以使该脚本本身的输出静音。

如果你然后追加|| true || true任何冒泡到npm的错误都不会显示,这要归功于npm相信命令已成功完成。

但是,当你调用那个脚本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