You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js新手求助:服务已运行但浏览器无法访问,端口显示undefined

Fixing "port undefined" & Connection Refused in Your First Node.js App

Hey there! As a fellow Node.js dev who’s been exactly where you are, let’s work through this problem step by step. That "port undefined" message is a huge clue—here’s what’s likely going on and how to fix it:

1. You’re missing a fallback port value

Most new devs start using process.env.PORT to let hosting platforms auto-set the port, but when running locally, that environment variable isn’t set by default. Without a fallback, Node tries to listen on an undefined port, which is why your connection gets refused.

Fix it by adding a default port:

// Instead of this (which causes "port undefined"):
const port = process.env.PORT;

// Do this to use 3000 as a local fallback:
const port = process.env.PORT || 3000;

// Then start your server with a confirmation log:
app.listen(port, () => {
  console.log(`Server is up and running on port ${port}!`);
});

Now when you run locally, it’ll use port 3000, and you’ll see a clear log confirming the server is active.

2. Port 3000 might be occupied by another process

Even if you set the port correctly, sometimes another app (a leftover Node instance, database, or even a browser extension) is using port 3000.

Check and free up the port:

  • On Mac/Linux: Run these terminal commands to find and kill the process:
    # Find the process using port 3000
    lsof -i :3000
    # Kill it (replace YOUR_PID with the number from the PID column)
    kill -9 YOUR_PID
    
  • On Windows: Use Command Prompt to do the same:
    # Find the process ID
    netstat -ano | findstr :3000
    # Kill the process
    taskkill /F /PID YOUR_PID
    

If you don’t want to mess with killing processes, just test with a different port like 3001 to see if that works.

3. Add error handling to catch silent failures

Sometimes the server fails to start without a clear message. Add error logging to see exactly what’s wrong:

const server = app.listen(port, () => {
  console.log(`Server is running on port ${port}!`);
});

// Catch and log server startup errors
server.on('error', (err) => {
  console.error('Server failed to start:', err.message);
  // Common error: "EADDRINUSE" means the port is already taken
});

This will tell you if there’s a specific issue like a port conflict or permission problem.

4. Double-check your server code basics

It’s easy to overlook small details when you’re new:

  • Did you actually call app.listen()? It’s the most critical line to start the server, and it’s easy to forget when focused on routes.
  • Are there any syntax errors or uncaught exceptions before the listen() call? Check your console for red error messages above the "port undefined" line—those might be stopping the server from starting entirely.

Once you work through these steps, restart your Node server and try visiting http://localhost:3000 again. You should see your app load up!

内容的提问来源于stack exchange,提问作者Abhishek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:36:26