【问题标题】:Why does running app.js display Error: Cannot find module 'discord.js'为什么运行 app.js 显示错误:找不到模块'discord.js'
【发布时间】:2017-11-26 11:50:36
【问题描述】:

我正在为 Discord 编写一个机器人,当我从 cmdbox 运行 app.js 时,它一直在说:

Error: Cannot find module 'discord.js'  
    at Function.Module._resolveFilename (module.js:325:15)  
    at Function.Module._load (module.js:276:25)  
    at Module.require (module.js:353:17)  
    at require (internal/module.js:12:17)  
    at Object.<anonymous> (C:\Users\Name\Desktop\RoseluckJS\app.js:1:79)  
    at Module._compile (module.js:409:26)  
    at Object.Module._extensions..js (module.js:416:10)  
    at Module.load (module.js:343:32)  
    at Function.Module._load (module.js:300:12)  
    at Function.Module.runMain (module.js:441:10)

我已经尝试删除 node_modules 然后运行 ​​npm i --save
discord.js --no-optional 再次,我也尝试过执行 npm cache clear 然后 npm i --save discord.js 但它没有用。这是我的 app.js 的代码。

const Discord = require('discord.js');
    const client = new Discord.client();

        const settings = require('./settings.json');

        client.on('ready',() => {
          console.log('I\'m Online\nI\'m Online')
        });

        client.on('message', message => {
           if (message.content === 'ping') {

           }

        });

        client.login(settings.token);

【问题讨论】:

  • 可能有很多问题...您是否从您的 package.json 所在项目的根目录运行 npm install ?运行后,你有没有检查 node_modules 文件夹并在其中看到不和谐?
  • 我确实从我的项目 @dvsoukup 的根目录运行它,我的 package,json 就在其中,但我在 node_modules 中没有看到不和谐,只有 bufferutil、libsodium-wrappers、opusscript 和 uws。
  • 能否请您粘贴您的 package.json 文件。
  • 试一试-npm install discord.js --save 然后检查 package.json 不和谐应该在那里并去npm install。
  • @ŁukaszSzewczak 这里是 package.json { "name": "roseluck", "version": "0.0.1" , "description": "roseluck bot js bot", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" &amp;&amp; exit 1" }, "author": "helperbot", "license": "MIT" } Ayush,在那之后是否应该在 node_modules 中有 discord.js?没看到

标签: node.js npm discord.js


【解决方案1】:

在某些版本的 Windows 上,如果您使用 shift+右键单击 > 上下文菜单 > “在此处打开命令窗口”。事情变得很奇怪,无法解释,可能是路径如何从上下文菜单传递到控制台,然后传递到 npm 安装。唉,如果您以这种方式使用命令提示符,请浏览到您的 c:users\name 文件夹,看看您是否有一个 node_modules 文件夹,其中包含 discord.js。如果您在那里找到它(或者如果您以这种方式打开命令提示符),请以管理员身份打开命令提示符并手动 cd 进入您的项目文件夹,然后尝试 npm install。

我不久前打开了一个关于此的问题,但问题已关闭。如果此答案为您解决了问题,请考虑使用“我可以确认此问题”之类的内容对此问题发表评论。

https://github.com/npm/npm/issues/16250

【讨论】:

    【解决方案2】:

    如果您还没有完成“npm init”,那么这可能是您的问题。 否则尝试“npm install discord.js”而不是 i —save。

    【讨论】:

      【解决方案3】:

      尝试删除 discord.js 模块并使用 npm i discord.js —save 重新安装它

      【讨论】:

        猜你喜欢
        • 2019-01-18
        • 1970-01-01
        • 1970-01-01
        • 2020-02-22
        • 2022-01-19
        • 1970-01-01
        • 1970-01-01
        • 2020-02-27
        • 1970-01-01
        相关资源
        最近更新 更多