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

Node.js HTTP服务器无法启动,package.json解析错误求助

解决package.json解析错误及Node HTTP服务器搭建问题
  • 错误原因:package.json是JSON格式的配置文件,仅允许存放键值对形式的项目配置,不能写入Node.js执行代码。你错误地将服务器代码混入其中,破坏了JSON的结构规范,导致解析报错。

  • 修正后的package.json:

{
  "name": "nodeserver",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
  • 服务器代码的正确存放:在项目目录下创建index.js文件(对应package.json中main字段指定的入口文件),将服务器代码写入该文件:
require("http").createServer((inRequest, inResponse) => {
  inResponse.end("Hello from my first Node Web server");
}).listen(80);
  • 运行服务器:
    1. 打开终端,进入项目目录C:\Users\USER\Desktop\nodeserver
    2. 执行命令 node index.js
    3. 打开浏览器访问http://localhost,即可看到服务器返回的内容

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 02:40:31