缺少脚本:在部署nodejs到heroku时启动

Missing script: start when deploying nodejs to heroku

本文关键字:heroku 启动 nodejs 部署 脚本      更新时间:2023-09-26

所以我试图将我的nodejs应用程序部署到heroku。
我在package.json

中定义了启动脚本
"scripts": {
    "test": "./node_modules/.bin/mocha -r mocha-cakes",
    "start": "rm -r -f database/test.db && node app.js",
    "start-win": "del database''test.db && node app.js"
}

我还将web: rm -r -f database/test.db && node app.js添加到Procfile

然而,我的dyno仍然崩溃,并出现以下日志错误:

2016-06-25T12:27:58.216435+00:00 heroku[web.1]: Starting process with command `npm start`
2016-06-25T12:28:00.503447+00:00 app[web.1]: npm ERR! argv "/app/.heroku/node/bin/node" "/app/.heroku/node/bin/npm" "start"
2016-06-25T12:28:00.502867+00:00 app[web.1]: npm ERR! Linux 3.13.0-85-generic
2016-06-25T12:28:00.504156+00:00 app[web.1]: npm ERR! npm  v3.8.6
2016-06-25T12:28:00.503707+00:00 app[web.1]: npm ERR! node v5.11.1
2016-06-25T12:28:00.506550+00:00 app[web.1]: npm ERR! missing script: start
2016-06-25T12:28:00.506746+00:00 app[web.1]: npm ERR! 
2016-06-25T12:28:00.506912+00:00 app[web.1]: npm ERR!     <https://github.com/npm/npm/issues>
2016-06-25T12:28:00.516583+00:00 app[web.1]: 
2016-06-25T12:28:00.516795+00:00 app[web.1]: npm ERR! Please include the following file with any support request:
2016-06-25T12:28:00.516913+00:00 app[web.1]: npm ERR!     /app/npm-debug.log
2016-06-25T12:28:00.506796+00:00 app[web.1]: npm ERR! If you need help, you may report this error at:
2016-06-25T12:28:00.505628+00:00 app[web.1]: 
2016-06-25T12:28:01.445657+00:00 heroku[web.1]: Process exited with status 1
2016-06-25T12:28:01.461079+00:00 heroku[web.1]: State changed from starting to crashed
2016-06-25T12:28:01.461867+00:00 heroku[web.1]: State changed from crashed to starting
2016-06-25T12:28:04.433811+00:00 heroku[web.1]: Starting process with command `npm start`

我已经检查了其他SO个问题,没有一个答案能解决我的问题。我已经尝试了其他帖子推荐的所有方法,但还没有发现任何成功。

如果你想看源代码,这里是我的repo的链接。

在创建表后的/database/database.js文件中,如果这些表不存在,则有一个db.run查询读取该XML文件。

db.run('INSERT OR IGNORE INTO managers (username, password) VALUES ("admin","admin")', function(){
    //
    // Dear fellow coder:
    //
    // Once you are done trying to 'optimize' this routine,
    // and have realized what a terrible mistake that was,
    // please increment the following counter as a warning
    // to the next guy:
    //
    // total_hours_wasted_here = 3
    //
    // When I wrote this, only God and I understood what I was doing
    // Now, God only knows
    //
    // Magic, Do not touch
    //initialise the table values from log file
    // I want to read the xml file after the database tables have been created. HACK HACK!!!
    $this.readLogFile();
    console.log('Loaded Data from XML document');
});      

我不确定除了测试你在部署的代码中有这个之外的原因是什么。我猜想您希望在部署后删除DB的原因是您只在测试中需要它。

由于Heroku似乎不喜欢rm -r -f database/test.db,我建议您将其注释出来以进行部署,或者作为更优雅的解决方案,在调用此DB查询之前添加if语句,该查询仅在您具有布尔值为true的TESTING环境变量时才运行它。您还应该从启动脚本中删除rm -r -f database/test.db

您的方法的另一个问题是,开始脚本甚至在导入XML文件之前运行,因此对开始脚本中的db的任何操作都不会删除它。

相反,你也可以创建一个查询,每次启动服务器时在app.js中调用。该查询应该清空整个DB。但是当你可以不读它的时候,为什么要读它并删除它呢?