用 index.js 替换 main.js,错误:ENOENT?

Posted

技术标签:

【中文标题】用 index.js 替换 main.js,错误:ENOENT?【英文标题】:Replacing main.js with index.js, Error: ENOENT? 【发布时间】:2022-01-10 21:45:47 【问题描述】:

我很困惑为什么这个机器人现在可以运行,我认为这是因为我没有“index.js”我使用“main.js”我怎样才能从“main.js”切换到“index.js”?

我将添加所有可能帮助您发现错误的主要代码:D

这是“main.js”中的代码:

const  Client, Collection, Intents  = require('discord.js');
const client = new Client( disableMentions: 'everyone', partials: 
['MESSAGE', 'CHANNEL', 'REACTION'], ws:  intents: Intents.ALL  );


client.commands = new Collection();
client.aliases = new Collection();

['command', 'event'].forEach(handler => 
    require(`./handlers/$handler`)(client);
);


client.login(process.env.BOT_TOKEN);

错误:

internal/fs/utils.js:269
    throw err;
    ^

Error: ENOENT: no such file or directory, open '/home/runner/DwaCraft-Ticket-bot/index.js'
    at Object.openSync (fs.js:462:3)
    at Object.readFileSync (fs.js:364:35)
    at Object.<anonymous> (/run_dir/interp.js:195:19)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47 
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/home/runner/DwaCraft-Ticket-bot/index.js'

repl process died unexpectedly: exit status 1
 

包.json:


    "name": "ticket-bot",
    "version": "1.0.0",
    "description": "A simple ticket bot",
    "main": "main.js",
    "scripts": 
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node main.js"
    ,
    "author": "hamoodi",
    "license": "ISC",
    "dependencies": 
        "better": "^0.1.0",
        "cpu-stat": "^2.0.1",
        "discord.js": "^12.5.3",
        "dotenv": "^8.6.0",
        "express": "^4.17.1",
        "moment": "^2.29.1",
        "os": "^0.1.2",
        "sourcebin_js": "0.0.3-ignore"
    

文件:

https://i.stack.imgur.com/FptiD.png

【问题讨论】:

将json包中的main属性改为main.js 它是:“main”:“main.js”, 能否请您添加包json以及如何运行您的项目? 当然,一秒钟。 给你,添加。 【参考方案1】:

如下链接所说,您可以创建一个 .replit 文件,并在其中放入

run="节点[文件].js"

并将 [file] 替换为 main。

https://replit.com/talk/ask/Is-it-possible-to-changedeleted-indexjs/43264

【讨论】:

【参考方案2】:

将名称文件从main.js 更改为index.js in "scripts" => "start" in package.json 文件并点击npm start。记得从 main.js 文件所在的folder 运行script,并重命名文件


    "name": "ticket-bot",
    "version": "1.0.0",
    "description": "A simple ticket bot",
    "main": "main.js",
    "scripts": 
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node index.js"
    ,
    "author": "hamoodi",
    "license": "ISC",
    "dependencies": 
        "better": "^0.1.0",
        "cpu-stat": "^2.0.1",
        "discord.js": "^12.5.3",
        "dotenv": "^8.6.0",
        "express": "^4.17.1",
        "moment": "^2.29.1",
        "os": "^0.1.2",
        "sourcebin_js": "0.0.3-ignore"
    

【讨论】:

以上是关于用 index.js 替换 main.js,错误:ENOENT?的主要内容,如果未能解决你的问题,请参考以下文章

vue项目main.js文件下import router from './router'默认导入router文件夹下index.js的原因

Mock.js使用

Vue 入门学习

如何在javascript中使用require

main.js中Vue.prototype定义全局变量

Parse Server - 如何从 app.js 或 index.js 调用 Cloud 代码中定义的作业