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

Ubuntu 16.04下Node.js脚本控制台运行报错问题求助

Troubleshooting Console Error When Running Node.js Script (Works in Browser)

Hey there! Let's break down why your hello.js script throws an error in the console even though it loads fine in the browser, plus how to fix each common issue:

1. You're Using the Wrong Command to Run the Script

Browser access works because your Node.js server is already running (from your deployment setup), but running the script directly in the console requires explicit Node.js invocation.

  • Fix: First navigate to the script's directory:
    cd /var/www/html/nodejs/
    
    Then run it with the Node executable:
    node hello.js
    
    If you want to run it like a standalone app, add a shebang line at the very top of hello.js:
    #!/usr/bin/env node
    // rest of your hello.js code
    
    Grant execute permissions, then run it directly:
    chmod +x hello.js
    ./hello.js
    

2. Port 8080 Is Already Occupied

If the Node server from your browser-accessible setup is still running, trying to launch the script again will throw an EADDRINUSE error—port 8080 can't be used by two processes at once.

  • Fix: Kill the existing Node process first:
    # Target the process using port 8080 specifically
    lsof -ti:8080 | xargs kill -9
    
    Or kill all Node processes (use cautiously if you have other Node apps running):
    pkill node
    
    Then run your script again.

3. Permission Restrictions on the Script/Directory

Apache runs under the www-data user, which has access to /var/www/html/nodejs/, but your regular user account might lack read or execute permissions for the script.

  • Fix: Check current permissions with:

    ls -l /var/www/html/nodejs/hello.js
    

    If the owner is root or www-data, transfer ownership to your user:

    sudo chown your_username:your_username /var/www/html/nodejs/hello.js
    

    (Replace your_username with your actual Ubuntu username.)

    Ensure your user can read the script:

    sudo chmod 644 /var/www/html/nodejs/hello.js
    

4. Missing npm Dependencies

If hello.js uses external packages, dependencies might be installed in a location accessible to Apache but not your regular user, or you're running the script from a directory without node_modules.

  • Fix: Navigate to the script's directory and install dependencies:
    cd /var/www/html/nodejs/
    npm install
    
    Double-check there's a package.json file in the directory listing your required packages.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:57:50