I used to start a nodejs script with this command
node cli/myscript.js -c configs/some-config.json
I tried to start the same script using pm2. I found here a post handling the same theme.
I tried this :
pm2 start cli/myscript.js --node-args="-c configs/some-config.json"
I get a syntax error on the config file which I don't get when I don't use pm2.
SyntaxError: Unexpected token :
0|myscript | at checkScriptSyntax
I also tried this and get the same error:
pm2 start cli/myscript.js -- -c configs/some-config.json
It seems like that pm2 tries to execute the config as a js file.. because the config file is a valid json.
You can pass the file name in the same-config.json.
{
"apps" : [{
"name" : "myscript",
"script" : "cli/myscript.js",
"watch" : true,
"env": {
"NODE_ENV": "development"
}
}]
}
Then you can run the node server by following command -
pm2 start same-config.json
For more details please refer PM2 docs
Related
I'm using PM2 for deployment using ecosystem file, and while I know I can pass in node args like this;
{
"apps": [
{
"name": "myApp",
"script": "./myApp.js",
"args": "--randomArgs"
}
]
}
This uses those arguments on every startup. I want to only using this argument when deploying or more specifically as part of the post-deploy routine. My code has some post-deploy things it needs to do on first startup indicated by this CLI argument, but not on any subsequent starts.
I've tried doing it this way.
deploy : {
development : {
"user" : "ubuntu",
"host" : ["192.168.0.13", "192.168.0.14", "192.168.0.15"],
"ref" : "origin/master",
"repo" : "git#github.com:Username/repository.git",
"path" : "/var/www/my-repository",
"post-deploy" : "npm install && pm2 startOrRestart ecosystem.config.js --randomArgs --update-env --env development"
}
}
But the args aren't visible in process.argv. I also tried --node-args="--randomArgs" in the post-deploy CLI string and that didn't work either.
It only works if I put it in the apps section of ecosystem.config.js but that's not the desired action.
I have one shell command which is working fine from terminal but when I try to run from nodejs it is giving me the error
Orignal Command
awk -v RS='"[^"]*"' '{n+=gsub(/\n/, "&")} END{print n}' <(sed '$s/$//' file.txt)
Node Js Code
execSync("awk -v RS='\"[^\"]*\"' '{n+=gsub(/\\n/, \"&\")} END{print n}' <(sed '$s/$//' "+ filePath+')')
The exesync is giving the same output but it is showing me the error Syntax error: "(" unexpected
<() is a bash-specific process substitution syntax. execSync defaults to using /bin/sh, usually a narrowly POSIX-compliant shell, which means it doesn't support the syntax. Explicitly use bash instead:
execSync("command goes here", {
shell: "/bin/bash"
});
I am trying to run forever.js via the Windows command prompt and I get the following output:
>npm i -g forever
/my-project>forever start index.js
Log output:
'C:\Program' is not recognized as an internal or external command, operable program or batch file
I think it's something to do with the PATH that forever is using for the node binary, but I don't know how to fix it...
EDIT: Forever is using the following command (which is surrounded by quotes " "):
"C:\Program Files\nodejs\node.exe"
If you type dir /x in the root of c:, you can see the short name of the directory.
So, try the following instead:
C:\PROGRA~1\nodejs\node.exe
installing forever version 1.0.0 solved the problem for me
i have the same problem with forever 2.0.0.
I use a workaround in "forever.js": (I start my app by "node forever.js" )
const configChild = {
//
// Basic configuration options
//
silent: true, // Silences the output from stdout and stderr in the parent process
'killTree': true, // Kills the entire child process tree on `exit`
....
}
// =======================================================
// **Workaround for Windows**
// =======================================================
if (process.platform === 'win32' && process.execPath == "C:\\Program Files\\nodejs\\node.exe") {
configChild.command = '"C:\\PROGRA~1\\nodejs\\node.exe"';
}
const child = new (forever.Monitor)('app.js', configChild);
....
child.start();
I am starting instances of my app as a package.json script with PM2 this way:
"start:pm2": "pm2 start -i max node myapp.js"
I found out that not all members in the team always want to use max as a value for instances number while developing, but prefer to use some lower value.
To not change package.json I would better let them change the value inside .env file because we already use it so that the value from it would be used as the parameter to pm2.
I know I can create a wrapper js or bash script to load the variable from .env file and pass it to pm2 but it would be better to have a solution without it.
How can I achieve this?
You can create an ecosystem.config.js file and declare your environment variables under the “env:” attribute, in your case the NODE_APP_INSTANCE can be used to set the number of instances:
module.exports = {
apps : [{
name: "MyApp",
script: "./myapp.js",
env: {
NODE_ENV: "development",
NODE_APP_INSTANCE: "max"
},
env_production: {
NODE_ENV: "production",
}
}]
}
Then call pm2 start or pm2 start /path/to/ecosystem.config.js to load an ecosystem from an other folder.
A better pattern here is to remove dotenv from your code and "require" it on the command line. This makes your code nicely transportable between any environment (including cloud-based) - which is one of the main features of environment variables.
a) code up your .env file alongside your script (e.g. app.js)
b) to run your script without pm2:
node -r dotenv/config app.js
c) in pm2.config.js:
module.exports = {
apps : [{
name : 'My Application',
script : 'app.js',
node_args : '-r dotenv/config',
...
}],
}
and then
pm2 start pm2.config.js
Note: the use of dotenv/config on the command line is one of the best practices recommended by dotenv themselves
I am trying to publish npm package, when i am install the package globally and try to run the cli command i get this errors:
/.nvm/versions/node/v0.12.2/bin/myPack: line 1: use strict: command not found
/.nvm/versions/node/v0.12.2/bin/myPack: line 3: syntax error near unexpected token `('
/.nvm/versions/node/v0.12.2/bin/myPack: line 3: `var _commandLineArgs = require('command-line-args');'
The top of the file that the error refer to:
'use strict';
var _commandLineArgs = require('command-line-args');
var _commandLineArgs2 = _interopRequireDefault(_commandLineArgs);
The package.json bin section:
"bin": {
"myPack": "dist/myPack.js"
}
When i am running this in my local development this works well, what is the problem?
Your script should start with a shebang line, otherwise it will be executed as a shell script (hence the errors).
Add this as first line to dist/myPack.js:
#!/usr/bin/env node