PM2 not able to run docker-compose up - node.js

I'm trying to start a service using docker-compose up, but pm2 is not able to execute docker-compose file. docker-compose up command runs fine if I execute it manually, it just fails when pm2 executes it
I've already tried changing PM2 and node versions according to #2961 and this but nothing worked.
more details here #5472
0|docker-s | /usr/bin/docker-compose:2
0|docker-s | # EASY-INSTALL-ENTRY-SCRIPT: 'docker-compose==1.29.2','console_scripts','docker-compose'
0|docker-s | ^
0|docker-s |
0|docker-s | SyntaxError: Invalid or unexpected token
0|docker-s | at wrapSafe (internal/modules/cjs/loader.js:1001:16)
0|docker-s | at Module._compile (internal/modules/cjs/loader.js:1049:27)
0|docker-s | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
0|docker-s | at Module.load (internal/modules/cjs/loader.js:950:32)
0|docker-s | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
0|docker-s | at Object.<anonymous> (/home/gharsnull/.nvm/versions/node/v14.20.1/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23)
0|docker-s | at Module._compile (internal/modules/cjs/loader.js:1085:14)
0|docker-s | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
0|docker-s | at Module.load (internal/modules/cjs/loader.js:950:32)
0|docker-s | at Function.Module._load (internal/modules/cjs/loader.js:790:12)
npm --version
6.14.17
node --version
v14.20.1
S.O
Distributor ID: Ubuntu
Description: Ubuntu 22.04.1 LTS
Release: 22.04
Codename: jammy
docker-compose --version
docker-compose version 1.29.2, build unknown
docker --version
Docker version 20.10.20, build 9fdeb9c
ecosystem.config.js
module.exports = {
apps: [
{
name: 'docker-services',
cwd: './docker',
script: 'docker-compose',
args: 'up',
kill_timeout: 100000,
},
],
};

Related

Unexpected token * exception when run by pm2

I get the following exception when trying to start a node process over pm2 - when I execute the app.js directly everything is working just fine. I see that in the stack trace there is the node_modules folder of pm2 mentioned - why is that?
30|MyPm2Process | /smprod/dev-backend/node_modules/tedious/lib/token/stream-parser.js:60
30|MyPm2Process | static async *parseTokens(iterable, debug, options, colMetadata = []) {
30|MyPm2Process | ^
30|MyPm2Process | SyntaxError: Unexpected token *
30|MyPm2Process | at createScript (vm.js:80:10)
30|MyPm2Process | at Object.runInThisContext (vm.js:139:10)
30|MyPm2Process | at Module._compile (module.js:616:28)
30|MyPm2Process | at Object.Module._extensions..js (module.js:663:10)
30|MyPm2Process | at Module.load (module.js:565:32)
30|MyPm2Process | at tryModuleLoad (module.js:505:12)
30|MyPm2Process | at Function.Module._load (module.js:497:3)
30|MyPm2Process | at Module.require (module.js:596:17)
30|MyPm2Process | at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:70:39)
30|MyPm2Process | at require (internal/module.js:11:18)
Node version: 14.16
Update your pm2 to the latest version
Link
Updating pm2 to the latest version as described here fixed the issue immediatelly.

Error: Failed to load gRPC binary module because it was not installed for the current system when trying to run from a docker container

Error: Failed to load gRPC binary module because it was not installed for the current system
api_1 | Expected directory: node-v72-linux-x64-musl
api_1 | Found: [node-v64-linux-x64-glibc, node-v72-linux-x64-glibc, node-v83-linux-x64-glibc]
api_1 | This problem can often be fixed by running "npm rebuild" on the current system
api_1 | Original error: Cannot find module '/usr/src/app/node_modules/grpc/src/node/extension_binary/node-v72-linux-x64-musl/grpc_node.node'
api_1 | Require stack:
api_1 | - /usr/src/app/node_modules/grpc/src/grpc_extension.js
api_1 | - /usr/src/app/node_modules/grpc/src/client_interceptors.js
api_1 | - /usr/src/app/node_modules/grpc/src/client.js
api_1 | - /usr/src/app/node_modules/grpc/index.js
api_1 | - /usr/src/app/middleware/license.js
api_1 | - /usr/src/app/app.js
api_1 | at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/grpc_extension.js:53:17)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1015:30)
api_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
api_1 | at Module.load (internal/modules/cjs/loader.js:879:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:724:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:903:19)
api_1 | at require (internal/modules/cjs/helpers.js:74:18)
api_1 | at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/client_interceptors.js:144:12)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1015:30)
api_1 | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
api_1 | at Module.load (internal/modules/cjs/loader.js:879:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:724:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:903:19)
api_1 | at require (internal/modules/cjs/helpers.js:74:18)
api_1 | at Object.<anonymous> (/usr/src/app/node_modules/grpc/src/client.js:35:27)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1015:30) {
api_1 | code: 'MODULE_NOT_FOUND'
api_1 | }
i am trying to run from inside a docker container i tried changing the node version but still cant solve it i tried node v 12 and 14
had the same problem, this fixed it
npm install --target=12.0.0 --target_platform=linux --target_arch=x64 --target_libc=musl

Node.js in docker-compose container can't access resource using absolute path

I'm having Node.js that I'm trying to dockerize. There is a piece of code that provides Google Application Credentials stored in serviceAccount.json file that looks like this:
const googleApplicationCredentials = config.GOOGLE_APPLICATION_CREDENTIALS;
const serviceAccount = require(googleApplicationCredentials);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databasename.firebaseio.com"
});
Path to this credential file is pulled from docker-compose which looks like this:
api:
build:
dockerfile: Dockerfile.dev
context: ./server
volumes:
- /app/node_modules
- ./server:/app
environment:
- GOOGLE_APPLICATION_CREDENTIALS="/app/serviceAccount.json"
Docker file is this:
FROM node:14.14.0-alpine
WORKDIR "/app"
COPY ./package.json ./
RUN npm install
COPY . .
CMD ["npm", "run", "start"]
When I ssh to docker container, I can see serviceAccount.json under working directory /app.
However, when I start docker-componse I'm getting the following error:
Cred:"/app/serviceAccount.json"
api_1 | internal/modules/cjs/loader.js:883
api_1 | throw err;
api_1 | ^
api_1 |
api_1 | Error: Cannot find module '"/app/serviceAccount.json"'
api_1 | Require stack:
api_1 | - /app/src/firebaseInit.js
api_1 | - /app/src/services/notification.service.js
api_1 | - /app/src/controllers/messages.controller.js
api_1 | - /app/src/routes/consultingchannel.routes.js
api_1 | - /app/src/express.js
api_1 | - /app/src/server.js
api_1 | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:725:27)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18)
api_1 | at Object.<anonymous> (/app/src/firebaseInit.js:8:24)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
api_1 | at Module._compile (/app/node_modules/pirates/lib/index.js:99:24)
api_1 | at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
api_1 | at Object.newLoader [as .js] (/app/node_modules/pirates/lib/index.js:104:7)
api_1 | at Module.load (internal/modules/cjs/loader.js:928:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18)
api_1 | at Object.<anonymous> (/app/src/services/notification.service.js:4:1)
api_1 | at Module._compile (internal/modules/cjs/loader.js:1063:30)
api_1 | at Module._compile (/app/node_modules/pirates/lib/index.js:99:24)
api_1 | at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
api_1 | at Object.newLoader [as .js] (/app/node_modules/pirates/lib/index.js:104:7)
api_1 | at Module.load (internal/modules/cjs/loader.js:928:32)
api_1 | at Function.Module._load (internal/modules/cjs/loader.js:769:14)
api_1 | at Module.require (internal/modules/cjs/loader.js:952:19)
api_1 | at require (internal/modules/cjs/helpers.js:88:18) {
api_1 | code: 'MODULE_NOT_FOUND',
api_1 | requireStack: [
api_1 | '/app/src/firebaseInit.js',
api_1 | '/app/src/services/notification.service.js',
api_1 | '/app/src/controllers/messages.controller.js',
api_1 | '/app/src/routes/consultingchannel.routes.js',
api_1 | '/app/src/express.js',
api_1 | '/app/src/server.js'
api_1 | ]
api_1 | }
api_1 | [nodemon] app crashed - waiting for file changes before starting...
client_1 | Compiled with warnings.
UPDATE:
Finally realized very obvious problem. I should not have quotes around environment variable. Correct entry should be:
environment:
- GOOGLE_APPLICATION_CREDENTIALS=/app/serviceAccount.json
See your volumes definition in docker-compose, you mount your /server directory to /app. I guess that explains why it works when you ssh into the container, while it doesn't when you docker-compose up - in the latter case no volume is mounted.
volumes:
- /app/node_modules
# The culprit?
- ./server:/app

Does this error mean that node modules cant be found?

I think I've seen this error on my local computer when I'm missing a module, but I am trying to start my code with pm2. Is that what the error is seen here?
0|my-ba | at Object.<anonymous> (/home/user/website/index.js:4:1)
0|my-ba | at Module._compile (internal/modules/cjs/loader.js:1133:30)
0|my-ba | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
0|my-ba | at Module.load (internal/modules/cjs/loader.js:977:32)
0|my-ba | at Function.Module._load (internal/modules/cjs/loader.js:877:14)
0|my-ba | at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:32:23)
0|my-ba | at Module._compile (internal/modules/cjs/loader.js:1133:30)
0|my-ba | at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
0|my-ba | at Module.load (internal/modules/cjs/loader.js:977:32)
0|my-ba | at Function.Module._load (internal/modules/cjs/loader.js:877:14)
0|my-ba | at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
0|my-ba | at internal/main/run_main_module.js:18:47 {
0|my-ba | code: 'MODULE_NOT_FOUND',
0|my-ba | requireStack: [ '/home/user/website/index.js' ]
0|my-ba | }
It says it doesn't know how to "start" your app, as it's not in home/user/website/index.js
You need to edit your package.json and see which is the start command and move your files accordingly (or edit, whichever is more convenient)

Issue with executing npm scripts via pm2 in Windows

I am trying to run npm custom scripts via pm2 using ecosystem.json as,
{
"apps": [{
"name": "Service",
"script": "npm",
"args": "run command"
}]
}
Unfortunately I am unable to do it. Can anyone help me to get through this?
I am getting as shown below,
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
0|Service | (function (exports, require, module, __filename, __dirname) { ::
Created by npm, please don't edit manually.
0|Service | ^
0|Service | SyntaxError: Unexpected token :
0|Service | at createScript (vm.js:56:10)
0|Service | at Object.runInThisContext (vm.js:97:10)
0|Service | at Module._compile (module.js:542:28)
0|Service | at Object.Module._extensions..js (module.js:579:10)
0|Service | at Module.load (module.js:487:32)
0|Service | at tryModuleLoad (module.js:446:12)
0|Service | at Function.Module._load (module.js:438:3)
0|Service | at Object.<anonymous>
(C:\Users\AD001\AppData\Roaming\npm\node_modules\pm2\
lib\ProcessContainerFork.js:53:21)
0|Service | at Module._compile (module.js:570:32)
0|Service | at Object.Module._extensions..js (module.js:579:10)
this is a reported issue in pm2. you can work around by doing
//startscript.js
var exec = require('child_process').exec;
exec('npm start', {windowsHide: true});
then
pm2 start startscript.js

Resources