ForeverJS bash script errors on first line - node.js

Im trying to get a foreverJS script to run a Node server on an EC2 instance. The script looks like:
#!/bin/bash
forever stopall
forever hakuna-matata store in appropriate logs start 'full/qualified path'
Even if I add an echo "getting here" statement as the first line I still get an error. I do not know bash scripting well, so it may be permissions, relative/absolute path, EC2/node, environment variable related, but I honestly do not know.
Stacktrace:
[ec2-user#12-34-56 folder]$ forever startup.sh
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
/home/user/var/www/folder/startup.sh:2
echo "gh"
^^^^
SyntaxError: Unexpected string
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
error: Forever detected script exited with code: 1
I also tried via running the script via the forever service script with forever start startup.sh and get the same error. I've launched from the parent direct and with a (qualified) path.
whoami - user
which forever - /usr/local/bin/forever
which npm - /usr/local/bin/npm
sudo whomai - root
sudo which npm - /usr/bin/npm
sudo which forever - /usr/bin/forever
node -v -v8.2.1
npm -v - 5.3.0
Ask if you need additional environmental information...

Forever assumes SCRIPT is a JS file that can be executed with Node.js You're trying to run a bash script instead, so it errors out since echo "getting here" obviously isn't valid JS code.
To fix this, you need to use the -c command line option when running forever. From the docs:
-c COMMAND COMMAND to execute (defaults to node)
So in your case, you'd set -c to /bin/bash; like this:
forever start [LOG OPTIONS] -c /bin/bash path/to/your-shell-script.sh

Related

Creating a migration in knex.js is throwing an error

I am trying to create a migration and it throws the following error
yarn run v1.6.0
(node:14212) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usabil
ity issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods in
stead.
$ babel-node node_modules/.bin/knex migrate:make create_linky_table
C:\Users\Sagar\Desktop\linky\api\node_modules\.bin\knex:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Object.newLoader [as .js] (C:\Users\Sagar\Desktop\linky\lms-linky\api\node_modules\pi
rates\lib\index.js:88:7)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I have the following set up in my script in package.json
"knex": "babel-node --require ./node_modules/dotenv/config node_modules/.bin/knex --knexfile='src/knexfile.js'",
I am using the following code to create the migration
yarn knex make:migration create_linky_table
I am currently using windows and i came upon this answer . Could it be a windows issue?
What #Mikael Lepistö said is correct.
I faced similar issue while doing migration scripts on windows.
My previous command was:
babel-node node_modules/.bin/knex --knexfile src/database/knexfile.js migrate:latest
I changed it to:
babel-node node_modules/knex/bin/cli.js --knexfile src/database/knexfile.js migrate:latest
And the error has gone. May be changing the reference from .bin/knex to /knex/bin/cli.js will make the magic for youso give it a try
Sounds like C:\Users\Sagar\Desktop\linky\api\node_modules\.bin\knex script in windows is not JavaScript executable, but actually shell script wrapper in Windows. So when you try to use babel-node or node to execute it, node interpreter fails because you are trying to run bash/sh shell script with it.
Check out what is stored inside the C:\Users\Sagar\Desktop\linky\api\node_modules\.bin\knex script and if there is a way to override, which node interpreter is used, you can override it with babel-node. If there is no way to override it, you can just check out if it calls for example node_modules/knex/bin/cli.js and call directly that with babel-node.
I would check those out myself, but I don't have Windows node tools setup on this desktop.

Running npm global package as service with forever

I am trying to run my own instance of the discord bot available here: https://github.com/Gawdl3y/discord-rpbot. I have a Windows server VM set up and installed the script as a global npm package, and it definitely works because I can get the bot running correctly by simply executing
rpbot --token [token]
I would like to have this run in the background with forever (https://www.npmjs.com/package/forever) -- however, when I try to execute it with the command
forever AppData/Roaming/npm/rpbot --token [token]
it crashes with the following error:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
C:\Users\myusername\AppData\Roaming\npm\rpbot:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
error: Forever detected script exited with code: 1
I assume I'm not using forever correctly for executing a globally available command like this, but all the documentation I've been able to find is mum on the subject. Any help for how to make this work would be appreciated!

Error: Cannot find module 'C:\server.js' in Node server for Meanjs

I have git cloned meanjs into a folder in D: drive as shown below and ran the command npm start to start the node server.
admin#admin-PC MINGW32 /d/Udemy Angular 1 projects/Project5/jobfinder (master)
$ npm start
> meanjs#0.5.0 start D:\Udemy Angular 1 projects\Project5\jobfinder
> gulp
[23:41:30] Using gulpfile D:\Udemy Angular 1 projects\Project5\jobfinder\gulpfile.js
....and got the below error.
Error: Cannot find module 'C:\server.js'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
[23:58:35] [nodemon] app crashed - waiting for file changes before starting...
After that, just to give it a try I copied the server.js from the current directory to C:\ and issued 'npm start' command at git bash from the same directory and the error changed to below.
Debugger listening on [::]:5858
module.js:457
throw err;
^
Error: Cannot find module './config/lib/app'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (C:\server.js:6:11)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
[23:41:52] [nodemon] app crashed - waiting for file changes before starting...
Can anyone please help me understand why is Node looking for server.js and other resources at C:\ instead of the current directory? Is there anything very simple I am missing out (though I find it quite strange !). Btw sorry for posting the git bash errors as code since I did not have enough reputations to post more than one image link.
Cheers!
Adding my research ....
Invoking 'npm start' command in meanjs directory hits the file gulpfile.js. Looking into the file, I saw that a gulp task (nodemon) was initiated as below and the script property of the return object was set to 'server.js'.
Code from gulpfile.js.
gulp.task('nodemon', function () {
var nodeVersions = process.versions;
var debugArgument = '--debug';
switch (nodeVersions.node.substr(0, 1)) {
case '4':
case '5':
case '6':
debugArgument = '--debug';
break;
case '7':
debugArgument = '--inspect';
break;
}
console.log('nodemon task'); // checking execution of the callback
return plugins.nodemon({
script: 'server.js',
nodeArgs: [debugArgument],
ext: 'js,html',
verbose: true,
watch: _.union(defaultAssets.server.views, defaultAssets.server.allJS, defaultAssets.server.config)
});
});
The above problem was solved by deleting the Autorun string in the registry at the below path.
"HKEY_CURRENT_USER\Software\Microsoft\Command Processor".
The autorun string was set to C: and as a result Nodemon recognised C: as the root instead of the current directory in Gitbash or command prompt.

How to set environment variables to get forever run meteorjs

How do you set the environment variables to get forever run meteorjs?
If I do an export and run forever, like so
export PORT=80 MONGO_URL=mongodb://localhost:3002/appname
sudo forever -f start bundle/main.js
forever will not run the meteorjs bundle with node:
/Users/../bundle/server/server.js:166
throw new Error("MONGO_URL must be set in environment");
^
Error: MONGO_URL must be set in environment
at run (/Users/../bundle/server/server.js:166:11)
at Object.<anonymous> (/Users/../bundle/server/server.js:331:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/../bundle/main.js:1:63)
at Module._compile (module.js:456:26)
error: Forever detected script exited with code: 8
If I run it directly, it runs smoothly:
sudo PORT=80 MONGO_URL=mongodb://localhost:3002/appname ROOT_URL=http://myapp.com node bundle/main.js
If I do it like in this answer it works. But why doesn't it work with the export statement?
Is there a way to tell forever a config file to read those settings? I don't see a way in the forever docs.
You can run forever just like node. Try following
sudo PORT=80 MONGO_URL=mongodb://localhost:3002/appname ROOT_URL=http://myapp.com forever -f start bundle/main.js

How do I enable --debug for node.js when running GeddyJS

GeddyJS is started by running geddy from the root of the project.
I'm having some trouble getting GeddyJS to run under node.js with debug enabled.
getty installs /usr/local/bin/geddy:
#!/usr/bin/env node
...
which I made a quick copy of, and amended the shebang line:
#!/usr/bin/env node --debug
...
but that does not work, because env can't find 'node --debug'.
I then tried to write a wrapper script to act as the interpreter in the shebang line here:
#!/bin/sh
exec /usr/bin/env node --debug $#
And this time, node is started with a debugger enabled, but the paths must be messed up, or environment must be different, because I am getting missing module exceptions:
$ geddy-debug
debugger listening on port 5858
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module '../lib/geddy'
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at Object. (/usr/local/bin/geddy-debug:4:1)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
This of course works fine if I don't try to enable debugging.
I want to debug in eclipse as per : https://github.com/joyent/node/wiki/Using-Eclipse-as-Node-Applications-Debugger, which works great with just node --debug scriptname.js
Any suggestions are appreciated.
Answering my own question, so others can benefit if they happen to see this:
From the project directory, override the shebang line and invoke the interpreter directly to run the geddy script:
/usr/bin/env node --debug /usr/local/bin/geddy
There's a Debugging Geddy WikiPage with a bit more info.

Resources