运行 Express.js 时出现 Node.js Package.json 文件错误

问题描述

我是 Node.js 的新手

我有一个项目文件夹结构 C:\A\B\C\

我有 C:\A\B\C\html_pages\index.html,其中我有带有 CDN 的 React.js 代码

C:\A\B\C\server.js,其中包含我想要运行的 Express.js 代码

  1. 我安装了 nvm 然后使用命令安装了 Node.js nvm install 4.5

  2. 在windows PowerShell中,我进入了项目目录 C:\A\B\C 并使用了 npm init(此步骤中未创建 node_modules)

  3. npm install express --save来使用 express 模块(现在 node_modules 文件夹已创建,但 package.json 在 express 文件夹中)

  4. 使用 npm start 启动 Web 服务器,但出现以下错误

ENOENT: no such file or directory,open 'C:\A\B\C\package.json' 我检查了 Stackoverflow 上的帖子是否有此错误,但无法解决。 package.json 应该直接在 'C:\A\B\C\生成吗? 我在 C:\Program Files\nodejs 中安装了 Node.js,而项目不在 Program Files 下,这与此有关吗?

附言: 使用 npm init --yes 以便它在我的项目文件夹中安装 node_modules 并处理请求,但端口 3000 给出了没有得到 GET 请求的错误

解决方法

package.json 文件应位于您目录的根目录中。 从根目录打开命令行并输入,

npm init

这将在该目录中生成一个 package.json 文件。