How to set environment variables to get forever run meteorjs - node.js

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

Related

PM2 - SyntaxError: Block-scoped declarations not yet supported outside strict mode

I'm currently installing a NodeJS service on an Ubuntu server. The application is written in coffeescript and runs fine on it's own. This is how I setup the project:
cd ~/test-project
nvm use v4.9.1
npm install
pm2 start index.js
However when I try to start it using pm2 i get the error below:
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
And after doing some testing it seems that this error occurs no matter what script I start with pm2 so there must be something that isn't configured correctly..
I don't know if it matters but the script I am trying to run needs node v4.9.1 so I am using NVM to use that version, it seems to me that it is trying to run pm2 with that same node version somehow?
Any input is greatly appreciated as I've been stuck on this for way too long!!
I had the same issue on node version 4.x,
Downgraded the pm2 and it worked.
npm uninstall -g pm2
npm install -g pm2#3.0.4
'use strict';
Add above line of code to first line of the script file. let is used when you want a variable to be scoped to a block which is supported in strict mode in node v4.
See the documentation for more details.
#Vishnu Sing this is the complete output. The index.js file looks like this:
console.log('Hello World');
ubuntu#ip-172-31-32-5:~/test-project$ nano index.js
ubuntu#ip-172-31-32-5:~/test-project$ node -v
v10.15.3
ubuntu#ip-172-31-32-5:~/test-project$ sudo pm2 start index.js
/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/node.js:132
let val = process.env[key];
^^^
SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
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 Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/needle/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
ubuntu#ip-172-31-32-5:~/test-project$

Docker container not started due to nodejs error

I am running
sudo docker run -d --name cloud-portal -v /opt/stack/dockercloudportal/config.js:/opt/fiware-cloud-portal/config.js -p 8000:8000 cloud-portal
But it gives me nodejs error as below:
/opt/fiware-cloud-portal/node_modules/express/node_modules/debug/src/node.js:120
exports.inspectOpts = Object.keys(process.env).filter(key => {
^
SyntaxError: Unexpected token >
at Module._compile (module.js:439:25)
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> (/opt/fiware-cloud-portal/node_modules/express/node_modules/debug/src/index.js:9:19)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Do I have to add any more dependencies?
Docker image has been build successfully but docker container goes to exit state.
You are using old version of node image in Docker.Use normal function rather than using Fat arrow Function(=>). It is not supported on the older version of node. Advantages of Arrow function-
1 this in arrow function points to the parents.
2 It creates writing function short.
You can also write the line without using fat arrow function.
exports.inspectOpts = Object.keys(process.env).filter(function(key) {
//Your Code Here
})

Node.js Issue w/ Engine.io

I'm having an issue with my server-side node script from processing. I have a VPS and running node app.js works perfectly fine in my other virtual folder for website1. But when i run node app.js in my website2 virtual folder it says
/home/website2/node_modules/engine.io/node_modules/uws/uws.js:3
const http = require('http');
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
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 Server.init
(/home/website2/node_modules/engine.io/lib/server.js:115:16)
at new Server
(/home/website2/node_modules/engine.io/lib/server.js:65:8)
at Function.attach
(/home/website2/node_modules/engine.io/lib/engine.io.js:123:16)
at Server.initEngine
(/home/website2/node_modules/socket.io/lib/index.js:274:21)
First, I'll
cd /home/website2
Then I'll
node app.js
Which shows me the error above, but when I
cd /home/website1
Then
node app.js
My script executes. What's the difference? I checked my node version and its 0.10.30, but it works in my website1 home directory regardless. How can I fix this?

Using the Forever Node Module on multiple projects

I am in the process of setting up two different web application projects on my server. I am attempting to get Node, MongoDB, and the Forever Node module set up correctly so that I can use Forever on my scripts to keep them running 24/7.
The main issue I am running into is the local vs. global installations for Node and the two modules. I have attempted this installation process five times now using a few resources online as well as a bash script that I found here (https://github.com/punkave/stagecoach/blob/master/sc-proxy/install-node-and-mongo-on-ubuntu.bash).
Nothing I do seems to work correctly. Either the Forever command doesn't work from my application's folder or when Forever does work, the process gets stopped immediately with a log file created. Here is the contents of the log file:
module.js:340
throw err;
^
Error: Cannot find module 'mongodb'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/html/test/requestHandlers.js:6:13)
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)
error: Forever detected script exited with code: 8
So obviously I am not installing Node, MongoDB, and Forever correctly. What is the best way to go about this process?
I am running on an Ubuntu 14.04 instance hosted on an Amazon AWS EC2 server.
Here is my require() for mongodb:
var mongo = require('mongodb'),
Server = mongo.Server,
Db = mongo.Db,
host = "localhost";
Latest error log that Forever creates:
/var/www/html/test/node_modules/mongodb/lib/mongodb/db.js:403
if(connection && connection.serverCapabilities.hasListCollectionsCommand) {
^
TypeError: Cannot read property 'hasListCollectionsCommand' of undefined
at Db.listCollections (/var/www/html/test/node_modules/mongodb/lib/mongodb/db.js:403:49)
at Db.createCollection (/var/www/html/test/node_modules/mongodb/lib/mongodb/db.js:1026:8)
at Object.<anonymous> (/var/www/html/test/requestHandlers.js:15:4)
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> (/var/www/html/test/indextest.js:3:23)
error: Forever detected script exited with code: 8

Cannot run node module as service

I'm trying to run the node module npm-lazy as a service in my localzone.
If I run the start command on the commandline It works well. I'm running it with:
npm_lazy --config /etc/npm-lazy-config.js
But if want to run the module as a service it constantly gets this errormessage:
[ Apr 9 01:13:31 Executing start method ("/opt/local/bin/npm_lazy --config /etc/npm-lazy-config.js"). ]
path.js:336
var isAbsolute = path.charAt(0) === '/',
^
TypeError: Cannot call method 'charAt' of undefined
at Object.exports.normalize (path.js:336:27)
at Object.<anonymous> (/opt/local/lib/node_modules/npm_lazy/config.js:2:21)
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> (/opt/local/lib/node_modules/npm_lazy/bin/npm_lazy:37:14)
at Module._compile (module.js:456:26)
[ Apr 9 01:13:32 Stopping because service exited with an error. ]
[ Apr 9 01:13:32 Executing start method ("/opt/local/bin/npm_lazy --config /etc/npm-lazy-config.js"). ]
I created the service with manifold, I added all the directories to the envvar section in my created service.xml I got from
echo $PATH
but It seems the "path" dependency for nodejs can't be found.
Please help.
It looks like npm_lazy is assuming that the HOME environment variable will be set, but it's not set in the SMF. You can set that by adding it to the envvar section of the XML manifest.

Resources