如何在npm命令中解决package.json未找到的问题

10,162 阅读2分钟

在这篇博文中,我们将学习如何在npm命令中解决package.json未找到的问题。

npm安装package.json错误

package.json是一个nodejs项目的JSON配置文件,其中包含应用程序的元数据和依赖性等。

在基于NPM的应用程序中,如nodejs、Angular、VueJS和ReactJS应用程序,package.json文件的位置是应用程序的根。

当你手动创建一个新项目或安装一个项目的依赖时,我们经常会遇到以下错误

  • package.json未找到
  • ENOENT: no such file or directory package.json
  • npm找不到package.json文件
  • 没有这样的文件或目录 package.json

通常情况下,这个错误是在npm命令运行时出现的,并且在应用程序根目录中没有找到package.json。

这个错误在Below npm命令运行时出现

  • npm install dependencies local or globally
  • npm run start - 运行项目

我们没有办法创建package.json文件

  • 使用文本编辑器手动创建
  • npm init命令
  • npm init -y命令

用于生成package.json的npm init命令

npm init命令创建package.json,其中充满了由用户输入的值。它要求及时输入以下细节

这是任何基于npm的应用程序创建的开始阶段。

 B:\Workspace\blog\npmcommand>npm init  
This utility will walk you through creating a package.json file.  
It only covers the most common items, and tries to guess sensible defaults.  
  
See `npm help json` for definitive documentation on these fields  
and exactly what they do.  
  
Use `npm install ` afterward to install a package and  
save it as a dependency in the package.json file.  
  
Press ^C at any time to quit.  
package name: (npmcommand) mypackage  
version: (1.0.0)  
description:  
entry point: (index.js)  
test command:  
git repository:  
keywords:  
author:  
license: (ISC)  
About to write to B:\Workspace\blog\npmcommand\package.json:  
  
{  
  "name": "mypackage",  
  "version": "1.0.0",  
  "description": "",  
  "main": "index.js",  
  "scripts": {  
    "test": "echo \"Error: no test specified\" && exit 1"  
  },  
  "author": "",  
  "license": "ISC"  
}  
  
  
Is this ok? (yes) yes

这将创建以下package.json

 {  
  "name": "mypackage",  
  "version": "1.0.0",  
  "description": "",  
  "main": "index.js",  
  "scripts": {  
    "test": "echo \"Error: no test specified\" && exit 1"  
  },  
  "author": "",  
  "license": "ISC"  
}

npm init -y with default package.json

这不会询问任何细节,只是创建一个默认值的package.json文件。你以后可以用任何文本编辑器来改变或修改。

上述命令创建了默认的package.json文件 下面是package.json的例子

{  
  "name": "npmcommand",  
  "version": "1.0.0",  
  "description": "",  
  "main": "index.js",  
  "scripts": {  
    "test": "echo \"Error: no test specified\" && exit 1"  
  },  
  "keywords": [],  
  "author": "",  
  "license": "ISC"  
}  

一旦package.json被安装,你可以在本地安装所有的依赖项 npm install -save-dev 或 npm install -g