npm无法找到package.json文件。

202

我正在尝试安装一些示例的依赖项:我下载了npm的express 2.5.8,但所有应用程序都会抛出相同的错误:

c:\node\stylus>npm install -d
npm info it worked if it ends with ok
npm info using npm@1.1.1
npm info using node@v0.6.11
npm ERR! Couldn't read dependencies.

npm ERR! Error: ENOENT, no such file or directory 'c:\node\stylus\package.json'
npm ERR! You may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>
npm ERR!
npm ERR! System Windows_NT 6.1.7600
npm ERR! command "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program File
s (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "-d"
npm ERR! cwd c:\node\stylus
npm ERR! node -v v0.6.11
npm ERR! npm -v 1.1.1
npm ERR! path c:\node\stylus\package.json
npm ERR! code ENOENT
npm ERR! message ENOENT, no such file or directory 'c:\node\stylus\package.json'

npm ERR! errno {}
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     c:\node\stylus\npm-debug.log
npm not ok

阻塞似乎是:

没有这样的文件或目录'c:\node\stylus\package.json

我错过了创建package.json的步骤吗?

我正在运行:

  • Windows 7 64位
  • npm 1.1.1
  • node 6.11
  • express 2.5.8

你是如何下载这些包的?你能按照通常的方式安装它们吗(即使用 npm install <package>)? - Linus Thiel
我还没有尝试过,因为我不知道这些应用程序使用了哪些包。我该如何找出这些信息,以便我可以尝试手动安装它们? - imjp
1
npm view <package> 命令会显示出 package.json 文件以及其依赖项。此外,npm help 命令也是您的好帮手。 - Linus Thiel
2
嗯... <package> 只是一个例子。你可以尝试使用 npm view stylus - Linus Thiel
我所做的是升级pnpm,命令为pnpm add -g pnpm,然后运行pnpm i - Clode Morales Pampanga III
显示剩余4条评论
29个回答

210
我认为,npm init会创建缺失的package.json文件。对于相同情况,这对我有效。

144

在你的项目文件夹中,你需要通过终端运行以下命令来初始化package.json文件:

npm init

完成这些步骤后,您应该能够像预期的那样安装任何包,例如express:

npm install express

Deepali的答案致敬。


2
如果我之前使用“npm install -g express”安装了Express,那么它在哪里? - sports
1
你需要在 Express 4.0.0 中执行 "npm install -g express-generator" 命令。来源:https://dev59.com/u2Ag5IYBdhLWcg3w7uuk,在此之后,可以执行 express 命令。 - Lucian Depold
UBUNTU控制台建议使用sudo apt install node-express-generator...那么,最好的选择是apt install还是npm install - Peter Krauss

89

我会简要明了。 :) install -d 对您无效。很简单,试试:

$ npm install -g express

无法工作,需要执行sudo npm install -g express... 正确吗? - Peter Krauss
"-g 标志是全局配置的简写,它将软件包安装位置设置为您安装 NodeJS 的文件夹。" - jsta

68

按照以下步骤,您将获得package.json文件。

npm --version
npm install express
npm init -y

19

使用 node --version 0.10.31 时出现了这个问题。降级到 0.10.29,但问题仍未解决。看起来这是一个 Windows 安装程序的问题,因为他们也停止创建 C:\Users\User\AppData\Roaming\npm 文件夹。 - Shawn Solomon
8
我在运行 Angular.js 教程的 'npm install' 命令时遇到了 "no such file or directory package.json" 错误,最终来到了这里。在教程中并没有明确指出需要进入已检出的代码目录 - 在这种情况下,package.json 文件位于你在之前步骤中从 git 克隆的目录中 (https://docs.angularjs.org/tutorial)。 - Kevin Hooke
1
@KevinHooke的解决方案是这个问题的答案。从克隆的应用程序目录中运行npm -install解决了这个问题。 - Vikram

10
初学者通常会在随机位置尝试npm命令。 在下载或创建项目后,您需要进入项目文件夹。在该文件夹内有一个名为package.json的文件。
cd <path_to_project>
npm install

8

使用win7/win8/win10中的命令(CD)移动文件夹:

  1. 进入您的项目文件夹

  2. 运行:npm install -d


4
尝试重新安装Node.js。 curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash - sudo apt-get install -y nodejs
sudo apt-get install -y build-essential

并更新npm

curl -L https://npmjs.com/install.sh | sudo sh

3
我在这里发现自己也在尝试解决相同的错误信息:

npm ERR! message ENOENT, no such file or directory 'c:\<some_folder>\package.json'

这个错误可能由以下两个原因引起:

  1. 您没有package.json文件
  2. 您有package.json文件,但是您在错误的文件夹中运行npm start命令

要解决第一个原因,您需要使用以下命令创建package.json文件:

npm init

为了解决第二个原因,请确保运行npm start命令的文件夹与package.json文件所在的文件夹相同。

3

Node自带npm,所以您应该已经安装了npm的某个版本。然而,npm的更新频率比Node更高,因此您需要确保它是最新版本。

sudo npm install npm -g

测试:

npm -v //The version should be higher than 2.1.8

完成后,您应该能够运行:

npm install

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接