npm 错误!enoent ENOENT: 没有这样的文件或目录,打开 'C:\Users\...\package.json'

jul*_*019 4 node.js visual-studio-code

我正在做一个关于 VueJS 的教程。我对此完全陌生,所以不太确定我在做什么。我按照所有说明进行操作,安装了所有软件包,这是我在 VSCode 终端中进行的检查:

PS C:\Users\...\Documents\Vue - Getting Started> node --version
v12.18.0
PS C:\Users\...\Documents\Vue - Getting Started> npm --version
6.14.5
PS C:\Users\...\Documents\Vue - Getting Started> vue --version
@vue/cli 4.4.1
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试npm run serve按照教程中所示的方式进行操作时,它都会显示一条错误,指出package.json缺少 a:

npm ERR! enoent ENOENT: no such file or directory, open 'C:\Users\...\package.json'
Run Code Online (Sandbox Code Playgroud)

我查了一下,确实,我的用户文件夹中没有调用的文件package.json。我只有一个名为package-lock.json.

我还注意到,在教程视频中,终端上有类似的内容1: node,而在我的终端中,我可以看到1: powershell。这是屏幕截图:

终端截图

我缺少什么?谢谢


按照 El 的建议,我做了npm initpackage.json在我的项目文件夹中创建了一个文件。在里面,我添加了 El 建议的部分。整个package.json文件现在看起来像这样:

{
  "name": "package.json",
  "version": "1.0.0",
  "description": "package json",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "serve": "node index.js"
  },
  "author": "",
  "license": "ISC"
}
Run Code Online (Sandbox Code Playgroud)

我现在收到此错误:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! package.json@1.0.0 serve: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the package.json@1.0.0 serve script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
Run Code Online (Sandbox Code Playgroud)

El.*_*El. 6

我认为没有必要用这样一个不言自明的错误消息来解释 Nodejs ENOENT 错误:no such file or directory这只是意味着index.js您想要运行的npmnode不在当前目录中的任何文件。首先,您需要检查是否位于项目的根目录中,然后在项目的根目录中您需要有一个用于package.jsonJavaScript(Nodejs、react、typescript 等)项目的配置文件。因此,为了制作该配置文件,您可以使用npm init命令,它会询问您有关项目的预定义问题。为了启动或服务你的项目,你的文件中必须有一个像这样的脚本标签package.json

'scripts':{
  'serve': 'node index.js',
}
Run Code Online (Sandbox Code Playgroud)

然后您可以npm run serve在项目的根目录中使用命令来运行文件serve的命令package.json

对于我不熟悉的 VueJS,我认为index.html您的项目根目录中有一个文件,而且我知道您已经vue安装在您的计算机上。我想您需要替换node index.js为以下命令来为您的 Vue 应用程序提供服务:

'scripts':{
  'serve': 'vue index.html',
}
Run Code Online (Sandbox Code Playgroud)

并运行npm run serve