@fjrodafo/free-port
v0.0.4
Published
Fast and minimalist port finder!
Downloads
5
Maintainers
Readme
Install it!
npm i @fjrodafo/free-port
Run it!
// Import the module
const { findAvailablePort } = require('@fjrodafo/free-port');
// It will find an available port in case port 3000 is busy
findAvailablePort(3000).then(port => {
server.listen(port, () => {
console.log(`Server listening on port http://localhost:${port}`);
});
});
Environment variable
// You can create an environment variable for advanced use
const desiredPort = process.env.PORT ?? 3000;
findAvailablePort(desiredPort).then(port => {
server.listen(port, () => {
console.log(`Server listening on port http://localhost:${port}`);
});
});
Links
Full repository on github/gitlab.
Visit the npm package website here.