Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot disable warnings when node is launched via a shell script. #10802

Closed
dzrw opened this issue Jan 14, 2017 · 24 comments
Closed

Cannot disable warnings when node is launched via a shell script. #10802

dzrw opened this issue Jan 14, 2017 · 24 comments
Labels
feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem.

Comments

@dzrw
Copy link

dzrw commented Jan 14, 2017

As an end-user, I don't want to ever see warnings or deprecations. However, when node is invoked from a shell script via a shebang, there is no opportunity to pass the --no-warnings option to the process.

#!/usr/bin/env node --no-warnings
console.log("you will never get this far, so it doesn't matter if this compiles");

Is there an environment variable that can be set to toggle this option? If not, please add support for something like NODE_NO_WARNINGS=1.

@dzrw dzrw changed the title Cannot disable warnings when scripts are launched from the shell. Cannot disable warnings when node is launched via a shell script. Jan 14, 2017
@mscdex mscdex added feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem. labels Jan 14, 2017
cjihrig added a commit to cjihrig/node that referenced this issue Jan 19, 2017
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: nodejs#10802
PR-URL: nodejs#10842
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 25, 2017
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: nodejs#10802
PR-URL: nodejs#10842
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 27, 2017
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: nodejs#10802
PR-URL: nodejs#10842
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
MylesBorins pushed a commit that referenced this issue May 16, 2017
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: #10802
PR-URL: #10842
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
MylesBorins pushed a commit that referenced this issue May 18, 2017
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: #10802
PR-URL: #10842
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
andrew749 pushed a commit to michielbaird/node that referenced this issue Jul 19, 2017
This commit adds support for a NODE_NO_WARNINGS environment
variable, which duplicates the functionality of the --no-warnings
command line flag.

Fixes: nodejs/node#10802
PR-URL: nodejs/node#10842
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Italo A. Casas <[email protected]>
@mnpenner
Copy link

mnpenner commented Feb 5, 2019

What's the solution?

When I try putting this at the top of my file:

#!/usr/bin/env NODE_NO_WARNINGS=1 node

I just get:

/usr/bin/env: ‘/path/to/my/script’: Text file busy

@bnoordhuis
Copy link
Member

@Doragd
Copy link

Doragd commented Mar 14, 2019

@mnpenner
like this:

#!/usr/bin/env
export NODE_NO_WARNINGS=1

it works. :)

@mnpenner
Copy link

@Doragd Huh? How would that work... the file contains JavaScript, it'll syntax error on export NODE_NO_WARNINGS=1

@hexrcs
Copy link

hexrcs commented Apr 20, 2019

@mnpenner According to the link from @bnoordhuis, looks like it depends on the OS.

Does anyone have a cross-platform way to set the environment variable? Still haven't found any after researching and experimenting for a while. :(

@bnoordhuis
Copy link
Member

@hexrcs env -S - it's not standard but it works with BSD and GNU env(1).

@Doragd
Copy link

Doragd commented May 8, 2019

@mnpenner
20190508130041.png
It really works for a shell script.

@mnpenner
Copy link

mnpenner commented May 8, 2019

@Doragd We're talking about an executable .js file, not a .sh file which subsequently runs node.

@hexrcs
Copy link

hexrcs commented May 12, 2019

I am now using a launcher script to spawn a child_process to work around this limitation. Ugly, but fine for my purpose - 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 });

If you only wanted to disable the warnings because you used the fs.promises API as I did, I would recommend next time wrapping the original APIs in a promise manually, or sticking to util.promisify, sync version of the APIs, instead of using this workaround.

@ThaDaVos
Copy link

Why is this closed? We really need a better way to disable warnings or at least to disable the Experimental API warning

@tdd
Copy link

tdd commented May 24, 2019

You can just change your shebang to #! /usr/bin/env node --no-warnings

@jasnell
Copy link
Member

jasnell commented May 24, 2019

The --no-warnings flag can be used with the NODE_OPTIONS environment variable.

$ NODE_OPTIONS=--no-warnings ./node

@benjamingr
Copy link
Member

@jasnell one issue I just noticed is that that can't be set from within the program when it has already launched.

@SteveDeWald
Copy link

@benjamingr, are you able to put it in the shebang? This worked for me:

#!/usr/bin/env NODE_OPTIONS=--no-warnings node

@squallstar
Copy link

This also worked for me:

process.env.NODE_NO_WARNINGS = 1;

@hexrcs
Copy link

hexrcs commented Jan 16, 2020

@squallstar doesn't work for me.

Which node version are you testing on? I'm testing fs.promises on node 10.16.0. The module doesn't emit a warning in v12.13.0 though because I believe it's no longer experimental.

@squallstar
Copy link

@hexrcs the above works for me on Node 12. I have right before a process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; and this doesn't trigger a warning (while it does when NODE_NO_WARNINGS is not defined).

@hexrcs
Copy link

hexrcs commented Jan 16, 2020

Is this documented anywhere? I couldn't find anything about this env variable but it would certainly be great if we finally get this feature. :)

@squallstar
Copy link

squallstar commented Jan 16, 2020

@hexrcs https://nodejs.org/api/cli.html#cli_node_no_warnings_1 (or search for NODE_NO_WARNINGS on that page, the setting was added on Node 6).

@hexrcs
Copy link

hexrcs commented Jan 16, 2020

Weird, because setting it to either a string "1" or number 1 doesn't work on node v10 on my system (MacOS 10.14.6). (I was requiring the fs.promises module.)

Just tested on node v12.13.0 using the example below, also doesn't seem to work. :(

$ node

> process.env.NODE_NO_WARNINGS = 1
// 1
> process.emitWarning('something strange happened')
// (node:18941) Warning: something strange happened

> process.env.NODE_NO_WARNINGS = "1"
// "1"
> process.emitWarning('something strange happened')
// (node:18941) Warning: something strange happened

If the node interpreter is spawned with node --no-warnings, then process.emitWarning will do nothing.

@squallstar
Copy link

@hexrcs I just tested on Node v12.13.0 and I am using Mac OS 10.15.2 (19C57), works great from here.

@squallstar
Copy link

@hexrcs something which you can try is to spawn a child process after the env variable has been updated. I remember doing that on previous versions of node when using commander to create a node-based CLI tool.

@hexrcs
Copy link

hexrcs commented Jan 16, 2020

@squallstar Thanks for the tip :) I've been doing similar things:

#10802 (comment)

OK, I just upgraded node to v12.14.1, also doesn't work... Way too weird.

image

Anyway, I'll keep using the child process approach for now.

@ErisDS
Copy link

ErisDS commented Apr 20, 2020

I raised this same issue here (oops): #32876 and @sam-github kindly took the time to explain there's a solution using process.removeAllListeners('warning').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem.
Projects
None yet
Development

No branches or pull requests