-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Is there a way to silence warnings? #12
Comments
I thought about silencing them, but the ESM loader API is experimental and may change at any point, so I think for my own sake it's better to surface this warning. It reminds users that this is experimental and that they're actually using native ESM and not something faked. As for native flags, there aren't any but you can track this issue: nodejs/node#30810 |
I would disagree, I think the advantage of something like Might I suggest you capture the first couple of lines output by whatever program you run, compare them to what you expect:
If the program prints these exact lines, silence them, otherwise print them out (potentially with a message saying something like |
I add a console.clear() at the beginning of the file to clear the warning, but each execution causes a screen flash. |
I built a quick & effective solution that only filters out the unwanted warnings and keeps all others intact, for those interested. Add this script to your project - I've named it /**
* When using custom loaders like `tsm` to add TypeScript & module support to scripts,
* Node.js outputs a known set of warnings, which distract from the actual script output.
*
* By adding `--require=./filter-warnings.cjs` to the `node` or `tsm` args,
* this script filters those known warnings (but not any others) from the output.
*/
// Remove Node's built-in `warning` listener which outputs all warnings to stderr
process.removeAllListeners('warning');
// Add our own version that skips known warnings
process.on('warning', (warning) => {
let { name, message } = warning;
if (name === 'ExperimentalWarning' && message.indexOf('--experimental-loader') > -1)
return;
if (name === 'DeprecationWarning' && message.indexOf('Obsolete loader hook') > -1)
return;
console.warn(warning);
}); Usage works like this then: tsm --require=./filter-warnings.cjs ./path-to-your-ts-file.ts I could imagine that @lukeed could add this script to the tsm package and add the |
There's also $ yarn tsx index.ts run hackerNews
Hacker News Agent: once vs $ yarn tsm index.ts run hackerNews
(node:59451) ExperimentalWarning: Custom ESM Loaders is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:59451) DeprecationWarning: Obsolete loader hook(s) supplied and will be ignored: getFormat, transformSource
Hacker News Agent: once |
This is unlikely to change as ESM loading is still really busted & variable. For instance, the solutions above wouldn't even work in Node 20.x because ESM loaders now run in a separate thread, so catching/silencing main-thread- |
What will get better is the removal of this
|
Is there a way to disable the Node warning?
If so, a mention in the docs would be good I think.
The text was updated successfully, but these errors were encountered: