Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable warnings when node is launched via a (global) shell script

Tags:

node.js

npm

fs

I am building a CLI tool with node, and want to use the fs.promise API. However, when the app is launched, there's always an ExperimentalWarning, which is super annoying and messes up with the interaction prompts. How can I disable this warning/all warnings?

I'm testing this with the latest node v10 lts release on Windows 10.

To use the CLI tool globally, I have added this to my package.json file:

    { 
      //...
      "preferGlobal": true,
      "bin": { "myapp" : "./index.js" }
      //...
    }

And have run npm link to link the ./index.js script. Then I am able to run the app globally simply with myapp.

After some research I noticed that there are generally 2 ways to disable the warnings:

  1. set environmental variable NODE_NO_WARNINGS=1
  2. call the script with node --no-warnings ./index.js

Although I was able to disable the warnings with the 2 methods above, there seems to be no way to do that while directly running myapp command.

The shebang I placed in the entrance script ./index.js is:

#!/usr/bin/env node


// my code...

I have also read other discussions on modifying the shebang, but haven't found a universal/cross-platform way to do this - to either pass argument to node itself, or set the env variable.

If I publish this npm package, it would be great if there's a way to make sure the warnings of this single package are disabled in advance, instead of having each individual user tweak their environment themselves. Is there any hidden npm package.json configs that allow this?

Any help would be greatly appreciated!

like image 637
Blazing Fast Avatar asked Apr 20 '19 22:04

Blazing Fast


Video Answer


2 Answers

I am now using a launcher script to spawn a child_process to work around this limitation. Ugly, but it works with npm link, global installs and whatnot.

#!/usr/bin/env node
const { spawnSync } = require("child_process");
const { resolve } = require("path");

// Say our original entrance script is `app.js`
const cmd = "node --no-warnings " + resolve(__dirname, "app.js");
spawnSync(cmd, { stdio: "inherit", shell: true });

As it's kind of like a hack, I won't be using this method next time, and will instead be wrapping the original APIs in a promise manually, sticking to util.promisify, or using the blocking/sync version of the APIs.

like image 157
Blazing Fast Avatar answered Oct 12 '22 03:10

Blazing Fast


Here's what I'm using to run node with a command line flag:

#!/bin/sh
_=0// "exec" "/usr/bin/env" "node" "--experimental-repl-await" "$0" "$@"

// Your normal Javascript here

The first line tells the shell to use /bin/sh to run the script. The second line is a bit magical. To the shell it's a variable assignment _=0// followed by "exec" ....

Node sees it as a variable assignment followed by a comment - so it's almost a nop apart from the side effect of assigning 0 to _.

The result is that when the shell reaches line 2 it will exec node (via env) with any command line options you need.

like image 33
hexten Avatar answered Oct 12 '22 02:10

hexten