Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

yarnpkg - Set NPM log level to "quiet" in script definition

I have a simple CLI tool for my project which I run through NPM/Yarn scripts:

  "scripts": {
    "cli": "dotenv ts-node -r tsconfig-paths/register src/cli/index.ts --",
    ...
}

I'd like to silence the log info stuff that NPM/Yarn automatically write to stdout when a script is run.

By default, the output is:

$~/foo> yarn cli --help
yarn run v1.22.5
$ dotenv ts-node -r tsconfig-paths/register src/cli/index.ts -- --help
cli <command>

Foo bar baz bat

Options:
  --help  Show help                                                    [boolean]
?  Done in 2.42s.

I want to get rid of the part where it writes out the command itself, and the "Done in xs" part.

I know I can do it manually with yarn --silent cli or npm run --quiet cli, or I could set loglevel=quiet in an npmrc, but neither is preferable.

I don't want to use the npmrc as I want to retail output for other scripts, and I don't want to require users to run it with the --silent flag themselves.

The CLI tool generates JSON which is fed into other scripts, so it's ideal if it's clean output that can be redirected or piped without the NPM/Yarn guff.

I tried adding --silent to the start of the command, but that doesn't work. I tried setting npm_config_loglevel=quiet as an env var, but that doesn't work.

Is there anything else I can try, or is this just not supported?

question from:https://stackoverflow.com/questions/66062547/set-npm-log-level-to-quiet-in-script-definition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

when calling npm run script you will always get such output by default and there is no way (currently) to disable it.

if you don't want npm output, just call your script directly without using npm.

e.g. rather than executing npm run cli, then execute npx dotenv ts-node -r tsconfig-paths/register src/cli/index.ts --


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...