Skip to content

Instantly share code, notes, and snippets.

@o-az
Last active February 17, 2023 01:28
Show Gist options
  • Save o-az/ad5d3d2db5915149e4de1e04e2d0e72d to your computer and use it in GitHub Desktop.
Save o-az/ad5d3d2db5915149e4de1e04e2d0e72d to your computer and use it in GitHub Desktop.
Suppress Node.js warnings in CLI
// suppress-node-warnings.cjs

// inspired by 
// https://github.com/nodejs/node/issues/30810#issuecomment-1383184769
const { emit: originalEmit } = process;

function suppresser(event, error) {
  return event === 'warning' && error.name === 'ExperimentalWarning'
    ? false
    : originalEmit.apply(process, arguments);
}

process.emit = suppresser;
node --require ./suppress-node-warnings.cjs <whatever>

I'm using "type": "module" in package.json so I needed to use .cjs.

I'm also using ts-node so to run TypeScript files, I have the following script:

	"scripts": {
	  "run-ts": "node --loader ts-node/esm --require ./scripts/suppress-node-warnings.cjs"
	}

now I can run ts file with npm run-ts filename.ts.

You can also use NODE_NO_WARNINGS=1 to suppress all warnings. E.g.,

NODE_NO_WARNINGS=1 node <...>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment