如何在 node.js 中的 package.json 中默认将脚本设置为 运行(如 npm 运行 -s scriptname)?
How to set by default a script to run silently inside package.json in node.js (like npm run -s scriptname)?
正如问题中所提出的,我知道存在使用 npm 运行 -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
OS:Ubuntu 16.04 LTS
如果您的脚本在 Mac 或 Linux 上 运行ning,您可以将 > /dev/null
添加到脚本的末尾,它不会打印任何内容控制台。如果你问我的话,我会很沉默。虽然我建议您使用某种文件记录器,这样您就可以看到发生的任何错误。
示例:
node server.js > /dev/null
您需要注意的是,当您将其放入 npm 脚本时,它会向您显示其执行的命令行。
```bash
bjemilo:test$ npm 运行 一些脚本
test@1.0.0 somescript /Users/bjemilo/Desktop/test
echo "I want this script to run silently" > /dev/null
```
然而删除 /dev/null 产生这个
```bash
bjemilo:test$ npm 运行 一些脚本
test@1.0.0 somescript /Users/bjemilo/Desktop/test
echo "I want this script to run silently" > /dev/null
我希望此脚本 运行 静默
```
这是预期的行为。但是,如果您将 /dev/null 添加到 npm 命令,则可以解决此问题。
在终端中使用 npm
npm run somescript > /dev/null
它什么都不打印。
做npm run --silent somescript
会得到同样的结果
您总是可以只为 运行 命令创建一个 bash/shell 脚本。 npm scripts 是为了方便而存在的,它有很好的钩子,可以在脚本调用之前和之后 运行。
{
"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
任何冒泡到 npm 的错误都不会显示,这要归功于 npm 相信命令已成功完成。
但是,您仍然会看到 npm 本身在调用该脚本时的输出,即 I.E.
you$ npm run-script somescript
> your-app@1.0.0 somescript /your-app
> echo "I want this script to run silently" &> /dev/null || true
正如问题中所提出的,我知道存在使用 npm 运行 -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
OS:Ubuntu 16.04 LTS
如果您的脚本在 Mac 或 Linux 上 运行ning,您可以将 > /dev/null
添加到脚本的末尾,它不会打印任何内容控制台。如果你问我的话,我会很沉默。虽然我建议您使用某种文件记录器,这样您就可以看到发生的任何错误。
示例:
node server.js > /dev/null
您需要注意的是,当您将其放入 npm 脚本时,它会向您显示其执行的命令行。
```bash bjemilo:test$ npm 运行 一些脚本
test@1.0.0 somescript /Users/bjemilo/Desktop/test echo "I want this script to run silently" > /dev/null ```
然而删除 /dev/null 产生这个
```bash bjemilo:test$ npm 运行 一些脚本
test@1.0.0 somescript /Users/bjemilo/Desktop/test echo "I want this script to run silently" > /dev/null
我希望此脚本 运行 静默 ```
这是预期的行为。但是,如果您将 /dev/null 添加到 npm 命令,则可以解决此问题。
在终端中使用 npm
npm run somescript > /dev/null
它什么都不打印。
做npm run --silent somescript
会得到同样的结果
您总是可以只为 运行 命令创建一个 bash/shell 脚本。 npm scripts 是为了方便而存在的,它有很好的钩子,可以在脚本调用之前和之后 运行。
{
"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
任何冒泡到 npm 的错误都不会显示,这要归功于 npm 相信命令已成功完成。
但是,您仍然会看到 npm 本身在调用该脚本时的输出,即 I.E.
you$ npm run-script somescript
> your-app@1.0.0 somescript /your-app
> echo "I want this script to run silently" &> /dev/null || true