【问题标题】:cant install discord.js in visual studio code无法在 Visual Studio 代码中安装 discord.js
【发布时间】:2021-01-26 00:35:51
【问题描述】:

我是一名新程序员,我对 node.js 或任何这些东西都不是很了解。 我的主要问题不是代码错误本身,而是无法在 Visual Studio Code 中安装 discord.js。

我尝试在终端和 cmd 中运行的命令是“npm install discord.js”,但都对我不起作用。

输出:

npm ERR! path C:\Program Files\nodejs\node_modules\npm\node_modules\cacache\node_modules\unique-filename\package.json
npm ERR! Cannot find module 'C:\Program Files\nodejs\node_modules\npm\node_modules\cacache\node_modules\unique-filename\index.js'. Please verify that the package.json has a valid "main" entry

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Dezkah\AppData\Roaming\npm-cache\_logs\2020-10-11T14_35_47_984Z-debug.log 

我确实安装了 package.json 文件

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

对不起,如果这个问题很愚蠢或其他什么,但我似乎无法找到解决方案。 提前致谢!

【问题讨论】:

  • 您的 package.js 没有 main 属性。它应该类似于"main": "index.js"
  • 我很确定它确实如此{ "name": "bot", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
  • 你保存你的 package.json 了吗?我能够使用您的 package.json 文件并安装 discord.js 没有问题。
  • 如何保存?我很确定我确实保存了它当我打开 vsCode 我可以看到 package.json

标签: node.js npm discord.js


【解决方案1】:

它在 Windows 操作系统上运行良好。我创建了一个新的package.json 文件,然后安装了discord.js

请您清除缓存然后重试吗?

npm cache clean --force

然后再次尝试安装discord.js

npm i discord.js

【讨论】:

  • 由于某种原因,即使这个命令在我的 cmd 上也不起作用。 prnt.sc/ux6sb8
【解决方案2】:

在vscode中选择一个空文件夹作为工作空间后,你要做的第一件事就是打开一个终端,编写并按照说明进行操作

npm init

npm init --yes 插入所有默认值。

这会在您的工作文件夹中生成一个 package.json。

之后你可以通过 npm 安装包

npm i discord.js --save

【讨论】:

  • 我实际上忘了提,但我已经完成了npm init,但它仍然不起作用:(
  • 您是在项目根目录中打开终端吗?
  • 是的,但我确实尝试了命令提示符,但同样的事情和输出。
  • 我认为问题与我在 package.json 文件中的“主条目”有关,但我有一个与主条目同名的文件
  • 您的项目根文件夹中有node_modules 文件夹?如果是,请删除它,使用npm install 重新安装依赖项,然后使用npm install discord.js --save 重试安装discord.js。
猜你喜欢
  • 2022-11-19
  • 1970-01-01
  • 2019-07-08
  • 2022-11-08
  • 1970-01-01
  • 2023-02-16
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
相关资源
最近更新 更多