
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Generate stable, unique development ports for microfrontends based on package name
Generate stable, unique development ports for microfrontends based on package name
Portico assigns each microfrontend a stable, unique development port based on its package.json name, avoiding collisions without manual tracking.
# Install globally
npm install -g portico
# Or use with npx (no installation required)
npx portico
In microfrontend architectures, each app needs its own development port. Manual port assignment leads to:
Portico generates deterministic ports by hashing the package name and applying a series of transformations.
Benefits:
Assigning unique ports from a limited range means that collisions (two different microfrontends hashing to the same port) are more likely than you might think. This concept is closely related to the Birthday Problem in mathematics.
The probability of at least one collision occurring can be estimated with the following formula:
$$ P(\text{collision}) \approx 1 - e^{-k^2 / (2N)} $$
Where:
k is the number of microfrontends.N is the size of the port range (set with --range).The table below shows the approximate probability of at least one collision based on the number of microfrontends and the port range size.
| # of Microfrontends (k) | Range=997 | Range=1997 (Default) | Range=5000 |
|---|---|---|---|
| 10 | 2.5% | 1.2% | 0.5% |
| 20 | 9.5% | 4.9% | 2.0% |
| 30 | 20.2% | 10.5% | 4.4% |
| 50 | 46.5% | 27.1% | 11.8% |
| 75 | 75.6% | 51.1% | 25.4% |
| 100 | 91.8% | 71.4% | 39.4% |
The default range of 1997 offers a reasonably low collision chance for typical projects. If you have a very large number of microfrontends, you can decrease the probability of collision by increasing the range with the --range flag.
# Get port for current package (reads ./package.json)
portico
# Output: 4703
# Get port with custom base and range
portico --base 5000 --range 100
# Output: 5067
# Calculate port for any package name
portico --name my-awesome-app
# Output: 4318
# Use custom package.json location
portico --package /path/to/package.json
# Use specific hash and reducer algorithms
portico --name my-app --hash twin --reducer knuth
portico --name my-app --hash double --reducer knuth
portico --name my-app --hash safe --reducer lcg
# .env file
PORT=$(portico)
# Or directly in package.json
{
"scripts": {
"start": "PORT=$(portico) npm start"
//or
"dev": "npm start -- --port $(portico)"
}
}
// ESM (recommended)
import { getPort, getPortFromPackageJson } from "portico";
// Calculate port for any package name
const port1 = getPort("my-awesome-app");
// Get port for current package
const port2 = getPortFromPackageJson();
// Custom configuration with specific algorithms
const port3 = getPort("my-app", 5000, 100, "twin", "knuth");
const port4 = getPort("my-app", 3001, 1997, "double", "knuth");
const port5 = getPort("my-app", 3001, 1997, "safe", "lcg");
| Option | Short | Description | Default |
|---|---|---|---|
--base | -b | Base port number | 3001 |
--range | -r | Port range size | 1997 |
--hash | Hash function: sdbm, safe, twin, cascade, double | twin | |
--reducer | Reducer: modulo, knuth, lcg | knuth | |
--package | -p | Path to package.json | ./package.json |
--name | -n | Package name to use | - |
# Analyze import map for port collisions
portico analyze -i import-map.json
# Compare all hash+reducer combinations (table output)
portico benchmark -i import-map.json
# Get JSON output for programmatic analysis
portico benchmark -i import-map.json --output json
# Custom range analysis
portico benchmark -i import-map.json --range 997 --base 4000
getPort(packageName, basePort?, range?, hash?, reducer?)Generate a port for a specific package name.
packageName (string): Package name to hashbasePort (number, optional): Starting port (default: 3001)range (number, optional): Port range size (default: 1997)hash (string, optional): Hash function - 'sdbm', 'safe', 'twin', 'cascade', 'double' (default: 'twin')reducer (string, optional): Reducer function - 'modulo', 'knuth', 'lcg' (default: 'knuth')basePort and basePort + range - 1getPortFromPackageJson(packageJsonPath?, basePort?, range?, hash?, reducer?)Generate a port by reading the current package.json.
packageJsonPath (string, optional): Path to package.json (default: ./package.json)basePort (number, optional): Starting port (default: 3001)range (number, optional): Port range size (default: 1997)hash (string, optional): Hash function (default: 'twin')reducer (string, optional): Reducer function (default: 'knuth')analyzeImportMap(importMapPath, basePort?, range?, hash?, reducer?)Analyze an import map file for port collisions and distribution.
importMapPath (string): Path to import map JSON filebasePort (number, optional): Starting port (default: 3001)range (number, optional): Port range size (default: 1997)hash (string, optional): Hash function (default: 'twin')reducer (string, optional): Reducer function (default: 'knuth')Choose the best hash+reducer combination for your needs:
const port = getPort("my-app", 3001, 1997, "twin", "knuth");
const port = getPort("my-app", 3001, 1997, "double", "knuth");
const port = getPort("my-app", 3001, 1997, "safe", "lcg");
Available Hash Functions:
twin - Twin prime hashing (recommended) 🏆double - Double hashing algorithm 🥈safe - Safe prime hashing 🥉cascade - Prime cascade methodsdbm - Simple SDBM algorithmAvailable Reducers:
knuth - Multiplicative method (recommended) 🏆lcg - Linear congruential generator 🥈modulo - Simple modulo operation 🥉Analyze your microfrontend ecosystem for potential port conflicts:
portico analyze -i importmaps.json
# Dockerfile
FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
# Generate port at build time
RUN echo "VITE_PORT=$(npx portico)" >> .env
COPY . .
EXPOSE $VITE_PORT
CMD ["npm", "run", "dev"]
# .github/workflows/test.yml
- name: Get dev port
run: echo "DEV_PORT=$(npx portico)" >> $GITHUB_ENV
- name: Start dev server
run: npm run dev -- --port $DEV_PORT
# Run tests
npm test
# Watch mode
npm run test:watch
# Test specific package names
portico --name @company/my-app
portico --name my-awesome-microfrontend
# Benchmark different algorithms
portico benchmark -i importmaps.json
# Test collision rates with different configurations
portico benchmark -i importmaps.json --range 787 # Prime range
portico benchmark -i importmaps.json --range 1000 # Round number
git checkout -b feature/amazing-feature)git commit -m 'Add amazing feature')git push origin feature/amazing-feature)MIT License - see LICENSE file for details.
Made with ❤️ for microfrontend developers
FAQs
Generate stable, unique development ports for microfrontends based on package name
We found that portico demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.