Cannot run node module as service - node.js

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.

Related

ERR_PACKAGE_PATH_NOT_EXPORTED : Package subpath './dist/pako.es5.js' is not defined by "exports"

I have an issue with the deployment of a node app to production.
When I try to execute with Node a script I get the following error:
internal/modules/cjs/loader.js:498
throw new ERR_PACKAGE_PATH_NOT_EXPORTED(basePath, mappingKey);
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist/pako.es5.js' is not defined by "exports" in /opt/iul/sat_web_lfr/node_modules/pizzip/node_modules/pako/package.json
at applyExports (internal/modules/cjs/loader.js:498:9)
at resolveExports (internal/modules/cjs/loader.js:514:23)
at Function.Module._findPath (internal/modules/cjs/loader.js:642:31)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1017:27)
at Function.Module._load (internal/modules/cjs/loader.js:899:27)
at Module.require (internal/modules/cjs/loader.js:1090:19)
at require (internal/modules/cjs/helpers.js:75:18)
at Object.<anonymous> (/opt/iul/sat_web_lfr/node_modules/pizzip/js/flate.js:5:12)
at Module._compile (internal/modules/cjs/loader.js:1201:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
at Module.require (internal/modules/cjs/loader.js:1090:19)
at require (internal/modules/cjs/helpers.js:75:18)
at Object.<anonymous> (/opt/iul/sat_web_lfr/node_modules/pizzip/js/compressions.js:14:19)
at Module._compile (internal/modules/cjs/loader.js:1201:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1221:10)
at Module.load (internal/modules/cjs/loader.js:1050:32)
at Function.Module._load (internal/modules/cjs/loader.js:938:14)
at Module.require (internal/modules/cjs/loader.js:1090:19)
at require (internal/modules/cjs/helpers.js:75:18)
at Object.<anonymous> (/opt/iul/sat_web_lfr/node_modules/pizzip/js/utils.js:7:20) {
code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'
}
However when I execute this script on the testing server works fine.
We even copy and paste the node modules folder from the testing server to the production server but the error is the same.
I think is an error in some external package but I don't understand how the same configuration can have this kind of error depending on the system.
I've tried to update npm from 6.14.5 to 7.23.0 but the error persists.
Both testing and production server uses the same version of Node, 14.X
The only possibility remaining is that I'm using a different NODE_ENV variable between the 2 servers but looking at the error message it's seems unlikely.
Thanks in advance.

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
})

Mongoose: Error cannot find module debug

I'm building a basic MEAN webapp and am new to the stack. I have the front end running, but as soon as I add the following lines to app.js:
var mongoose = require('mongoose');
require('./models/test');
mongoose.connect('mongodb://localhost:3000/design-data-test');
I get the following error in terminal:
Error: Cannot find module 'debug'
at Function.Module._resolveFilename (module.js:336:15)
at Function.Module._load (module.js:278:25)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/username/node_modules/mongoose/node_modules/mquery/lib/mquery.js:11:13)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
And all of my front end code stops running. Mongodb is running on the default port.
How would I go about resolving this error?
For future visitors: You are probably missing a dependency. Make sure you run this first:
npm install
... before you run your app with npm start or node <app>
I think this may happen if you have a child dependency on debug through another package (for example express or mongoose) but you did not provide the dependencies' package.json files with the deployed application which makes node.js unable to locate debug.

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

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

Resources