Issue with executing npm scripts via pm2 in Windows - node.js

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

Related

PM2 not able to run docker-compose up

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,
},
],
};

can you help? Error: Cannot find module 'react-dev-utils/WatchMissingNodeModulesPlugin'

Error: Cannot find module 'react-dev-utils/WatchMissingNodeModulesPlugin'
Require stack:
- C:\Users\stromboli\IdeaProjects\projects-ui\config\webpack.config.js
- C:\Users\stromboli\IdeaProjects\projects-ui\scripts\start.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:1014:15)
at Function.Module._load (internal/modules/cjs/loader.js:884:27)
at Module.require (internal/modules/cjs/loader.js:1074:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\stromboli\IdeaProjects\projects-ui\config\webpack.config.js:18:39)
at Module._compile (internal/modules/cjs/loader.js:1185:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
at Module.load (internal/modules/cjs/loader.js:1034:32)
at Function.Module._load (internal/modules/cjs/loader.js:923:14)
at Module.require (internal/modules/cjs/loader.js:1074:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\stromboli\IdeaProjects\projects-ui\scripts\start.js:32:23)
at Module._compile (internal/modules/cjs/loader.js:1185:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)
at Module.load (internal/modules/cjs/loader.js:1034:32)
at Function.Module._load (internal/modules/cjs/loader.js:923:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'C:\\Users\\stromboli\\IdeaProjects\\projects-ui\\config\\webpack.config.js',
'C:\\Users\\stromboli\\IdeaProjects\\projects-ui\\scripts\\start.js'
]
}
No matter what I do localhost won't open and I get this error. The github solutions shown didn't work either, how can I fix it I'm about to go crazy.
npm ls react-dev-utils
`-- react-dev-utils#12.0.1
In your config find webpack.config.dev.js and remove the following:
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
module.exports = {
// ...
plugins: [
// If you require a missing module and then `npm install` it, you still have
// to restart the development server for Webpack to discover it. This plugin
// makes the discovery automatic so you don't have to restart.
// See https://github.com/facebook/create-react-app/issues/186
new WatchMissingNodeModulesPlugin(paths.appNodeModules),
]
}

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.

Why sh script generates "SyntaxError: missing ) after argument list"

I'm trying to run: "npm run test" on Node.js.
OS: Win10.
Getting error:
node_modules\.bin\wdio:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at wrapSafe (internal/modules/cjs/loader.js:1054:16)
at Module._compile (internal/modules/cjs/loader.js:1102:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

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)

Resources