PM2 Error: Cannot find module when using ecosystem file - node.js

I'm having an issue with pm2.
If I start my app with
cd /my/path
pm2 start server.js
everything works correctly, but if I use an ecosystem.config.js file, then it gives the following error
pm2 start ecosystem.config.js
Error: Cannot find module '/my/path/server.js'\n at Function.Module._resolveFilename
my ecosystem file is configured as follows
module.exports = {
"apps": [{
"name": "MyApp",
"script": "server.js",
"cwd": "/my/path"
}]
}
I tried uninstalling pm2, updating npm and reinstalling node_modules, but still I can't understand why it says a module is missing in my file (expecially because it works when not using the ecosystem file)

Ensure that the ecosystem.config.js is in the root of your project as well as your server entry file (server.js) and specify your script as below:
module.exports = {
"apps": [{
"name": "MyApp",
"script": "./server.js"
}]
}

Reading from https://pm2.keymetrics.io/docs/usage/application-declaration/
Field Type Example Description
name (string) “my-api” application name (default to script filename without extension)
script (string) ”./api/app.js” script path relative to pm2 start
cwd (string) “/var/www/” the directory from which your app will be launched
I would try:
with "script": "./server.js" (kind of strange, I know)
remove the cwd parameter and setting ./my/path/server.js

Try this command: pm2 start src/server.js
or other folder name before server file name

Related

How do you configure pm2 ignore_watch for multiple directories?

I am running PM2 on windows 10 and have the following as my ecosystem.config.json file use to start the process.
module.exports = {
apps : [{
name : "myApp",
script : "./server.js",
watch : true,
watch_delay: 5000,
ignore_watch: ['node_modules', 'tracking'],
env: {
"NODE_ENV": "production",
}
}]
}
Whenever I update something in the tracking directory is causes a restart. If I move the file location to node_modules and remove tracking from the ignore_watch then it works. I have tried several other paths but anything other than node_modules does NOT seem to work with PM2.
Is the formatting incorrect or am I just missing something or is there a known issue in windows?
PM2 watch and unwatch folder and transpile with babel-node
I was looking for a simple solution for the development server!
Problem: When uploading files to the development server 'express-fileupload' PM2 detects a change in the folders and restarts the process.
I clarify this operation is fine because we had PM2 configured to monitor the entire project!
"ecosystem.config.js" is the configuration; it is simple. You can expand it with the official documentation of PM2. I leave it at the end! How do we fix it, to prevent restarting the process each time we upload a file?
Create the configuration file in the root folder:
ecosystem.config.js
module.exports = {
apps: [
{
name: "DEH",
script: "./server/index.js",
interpreter: "./node_modules/#babel/node/bin/babel-node.js",
interpreter_args: "./node_modules/#babel/node/bin/babel-node",
watch: ["server"],
watch_delay: 1000,
ignore_watch: [
"node_modules",
"./server/src/files",
"./server/src/pdf",
],
watch_options: {
followSymlinks: false,
},
},
],
};
To run it:
pm2 start ecosystem.config.js
Note that ignore_watch has an array with elements that will not be checked.
The interpreter points to the folder where it was installed locally: if you don't specify it you will have to install babel-node globally (I don't recommend it)

How to add node run option --max-http-header-size and add name to pm2 start command

How would I start a pm2 process with the —max-http-header-size node option, as well as name the process.
I have a server with multiple micro-services, one of the services is a web scraper.
This web scraper accepts requests with headers over the nodejs default 8kb limit. So, to run my app locally have to add the --max-http-header-size node option.
I've cloned my app to the server, but don't know how to set --max-http-header-size, nor do I know how to name the process within the pm2 start command.
So far my attempts have looked like this.
// this sets the name, but I don't know how to add the option `--max-http-header-size`
pm2 start npm --name "{REPONAME}" -- start
pm2 start node --name "scraper" --max-http-header-size 15000 app.js
pm2 start node --max-http-header-size 15000 app.js -- --name "scraper"
The accepted answer by David Harvey did not work for me as of Node 16. Instead, I had to use node_args, something like this:
{
"apps" : [{
"name" : "myapp",
"script" : "./app.js",
"node_args": "--max-http-header-size=256000",
"env": {
"NODE_ENV": "production",
}
}]
}
What you're looking for is called environment variables! You can pass environment variables to pm2 using a file that loads your server like this:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
node_args: "--max-http-header-size=16000"
}
]
}
Here's more about it too:
https://pm2.keymetrics.io/docs/usage/environment/

Pm2 watch ignore does not ignore file/image upload

I want to upload an image using node(11.10.1),pm2(3.4.0),through Ubuntu 16.04. Like many others who have tried, I can receive an image from starting my server through node process but once I use PM2 to upload an image, PM2 restarts the server and there is no image uploaded.
I made a ecosystem and set different types of settings around like , watch, ignore, autoreset, cwd. I also tried the cli line of code where it is "sudo pm2 start server.js --ignore-watch "/public/images". I am not sure how I can ignore Pm2 the right way. I also made sure that i do have a file path at Myapp/public/images/
I have tried the following:
Multer upload files with PM2
Expressjs pm2 ignore watch public/images folder
https://pm2.io/doc/en/runtime/guide/ecosystem-file/
http://pm2.keymetrics.io/docs/usage/watch-and-restart/
my file structure:
>MyApp
>ecosystem.config.js
>server.js
>public
>images
>routes
this is my ecosystem
'apps' : [{
'name': 'pm2_checking_server',
'script': 'server.js',
'cwd': '/var/www/MyApp/'//........also tried it without cwd
'watch': ['server.js'], //...........I also did true/false
'ignore_watch': ['public/images/'],//...did variations like ./public/images, public/images... etc
'watch_options': {
'followSymlinks': false
}
}],
this is my multer destination in MyApp/routes/
destination: function (req, files, cb) {
cb(null, path.join(__dirname , '../public/images'))
}
I expect Pm2 to ignore the watching file upload but the response back I get from uploading an Image is "errno": -13,
"code": "EACCES",
"syscall": "open",
"path": "/var/www/MyApp/public/images/7ylxlncRKJ
thank you for your help!
As far as I see the difference with official examples is you are using single quote ' and they have used double quote " after all this is a json file. Also you have missing a , at line:
"cwd": "/var/www/MyApp/",
Delete trailing / at: public/images/
So this is your process.json file which should be at root of your project:
{
"apps": [{
"name": "pm2_checking_server",
"script": "server.js",
"cwd": "/var/www/MyApp/",
"watch": ["server.js"],
"ignore_watch": ["public/images"],
"watch_options": {
"followSymlinks": false
}
}]
}

NPM CLI application relative path doesn't work

I'm creating a CLI application in NodeJS and the package is going to be published on NPM. The application is very simple as it has only two files. Here is the structure of application:
package.json
{
"name": "mycliapp",
"version": "1.0.0",
"description": "Some description",
"main": "./bin/cli.js",
"preferGlobal": true,
"bin": {
"mycliapp": "bin/cli.js"
},
}
bin/cli.js
const nodePlop = require('node-plop');
const configPath = './bin/config.js';
const plop = nodePlop(configPath, {
force: argv.force || argv.f
});
bin/config.js
{
// some configuration
}
Now if I create symlink with npm install -g from this directory and run the command mycliapp from the same development directory, it works absolutely fine but if I run this mycliapp command from any other directory in my computer, the const configPath = './bin/config.js' is tried to be taken from the current working directory not from the actual npm package and hence the config file is not found.
How can I solve this issue? I tried using __dirname and __filename with path.join but nothing seems to be working.
I also published this package on npm and installed from there, the same issue is occurring.
The JSON in your package.json is malformed -- you should remove the trailing comma after you set the bin parameter.
Also, if your configuration file is in the same directory as your script, you should reference it within your script as ./config.js.
Finally, you need to include a shebang (#!/usr/bin/env node) at the top of your cli.js file or any file you intend to use as your point of entry into the app so that your system knows what interpreter to use to execute the file.
See this post on the npm blog for more information.

node.js Command Line Interface app on windows

I am creating a simple CLI (Command line interface) application using NodeJs, involving two files:
package.json:
index.js
I want to print "hello world" to STDOUT and it is working when running command $ node index.js
But I want to use it globally via command test. So, I put a bin entry in package.json. Then build the application using npm link .. But then when I run "test" command, Windows shows me the following error:
How can I use console.log in separate app?
Thank you!
In package.json file, you need to write the code as follows:
"name" : "test",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"build": "webpack",
},
...
...
after this, use the command npm start to run the application.
1- Create bin folder in root folder and place your index.js inside the bin.
type the shebang code on the first line of index.js:
bin/index.js
#!/usr/bin/env node
console.log("hello world");
2- Add below code into the package.json file.
"bin": {
"test": "./bin/index.js" //this is relative path
}
3-finally run this code on command line
npm link
now you run "test" in your command line, it will log "hello world"
note:pay attention to the relative path that i mentioned above.

Resources