How to stop nodemon from changing port on each restart of express app? - node.js

I am new to nodejs and this is first time I am using nodemon. I am using nodejs on windows. I have got following in my package.json file
"scripts": {
"start": "nodemon ./bin/www"
}
And I use npm start from command line to start my express app. The process start with a default port which is annoying. But what is even more annoying is that every time I change a file nodemon restarts the application, sometimes on an entirely different random port number. I tried changing the script section in package.json file to the below but that did not make any difference
"scripts": {
"start": "nodemon ./bin/www 3000"
},

From the comments it seems you're specifying the port through an env variable, let's call it EXPRESS_PORT. The node process doesn't inherit it when you start it with npm because npm start creates a new shell with its own environment. So you end up passing port undefined to express. That makes it bind to a random free port. To fix this you can set the variable in the start command:
"scripts": {
"start": "EXPRESS_PORT=3000 nodemon ./bin/www"
}
Or you can export it from your shell with export EXPRESS_PORT=3000 and then run npm start. If you do this you need to make sure to always export before starting the server, so you might want to place the export in ~/.profile or ~/.bashrc.

Related

Node.js applies the changes only after restart

I am very new to server side scripting. And I am using NodeJS. My Problem is that after adding some new features to the app, i.e. after changing the code, these changes will be applied only after restarting the server. Till then NodeJS behaves so as though I hadn't changed anything. So for instance if I add console.log("works") and don't restart the server, then it hasn't any effect.
I am using Nuxt.js, which is actually the Vue.js framework but with additional and very usefull features mainly for server side rendering. I didn't integrate the express.js at the beginning of the project, beacause it wasn't planned to write any server side code. So I am normally exporting express and using it, which is pretty fine for me, since I need just a couple lines of code to use the NodeJS file system.
So, as it is pretty hard to code, if I should restart the server once I changed anything, I want to ask you if there is any solution to this problem.
Use nodemon
step 1 : npm install -g nodemon <- this will install nodemon globaly in your system
step 2 : change your start script within package.json
"scripts": {
"start": "nodemon fileName" <- like this //filename is you root file which starts the app like app.js
}
step 3 : npm start
This is already build in into nuxt. You just need to run it in dev mode, not in production.
E.g. for dev with change monitoring
nuxt
For production without monitoring
nuxt start
So in this particular case the following changes to the "scripts" in package.json have solved my problem.
"scripts": {
"dev": "nodemon --watch api --exec \"nuxt\"",
"start": "nodemon nuxt",
}
The following link could also be usefull to you.
Install nodemmon in your application to allow live update npm -g install nodemon
and add the following codes inside your packages json file :
"main": "app.js",
"scripts": {
"start": "node app"
},
on your command line, just type : start

How can I run sails console without a webserver (using any ports)?

I have a Sails server running, and I want to execute some commands from inside of lifted Sails.
The problem is, then I run sails console - it bootstraps another instance of Sails, and trying to load another webserver next to existing, by default using the same ports.
By some environment limits, I can use only one port at the time. So I cannot load another webserver on the same machine.
Is there a way how to run sails console without using any ports?
Thank you.
There is an option. Use sails console --dontLift
Running console without port is I belive not possible at all.
Add inside package json npm script to run console on another port (or if you have sails installed globally just run sails console --port xxxx).
Part of my package json:
"scripts": {
"start": "node app.js",
"console": "sails console --port 1338",
"test": "mocha",
"docs": "rimraf public/docs && apidoc -i config/routes -o public/docs"
},
As you can see... npm run console will run sails console on port 1338 while deafult port of my app is 1337...

start 2 server using npm start

Is it possible to start 2 different server using npm start:
package.json:
"scripts": {
"start": "node index && http-server ./"
}
I wan to start a node app and a http-server to serve Single Page App.
Thanks #Lazyexpert for the answer, just use single &.
Sure, why not. The start script itself is just a command line command. You can push there whatever works as a command in command line.
Also you might would like to look at npm package concurrently.
Using this package, you start script will look like this:
"start": "concurrently \"command1 arg\" \"command2 arg\""

node.js config npm - NODE_CONFIG_ENV

I am trying to use the config package (npm install config) to be able to use different configurations for different enviromenets.
I am running it on windows 10.
I got 4 files under the config folder : default.json, development.json, production.json and qa.json.
when i am running SET NODE_ENV=production for example it applys to it
but config still aint taking the info from the right file.
var config = require('config');
var port = config.get('appPort');
I done some reading and i found out about another value - NODE_CONFIG_ENV.
I done some testing with :
console.log('NODE_CONFIG_ENV: ' + config.util.getEnv('NODE_CONFIG_ENV'));
console.log('NODE_ENV: ' + config.util.getEnv('NODE_ENV'));
And it seems that NODE_CONFIG_ENV is responsible for the problem because it seems that config is using it instead to decide which file to choose.
My question is how can i make config use NODE_ENV again?
Or if it is not possible how can i set NODE_CONFIG_ENV instead?
Here is a partial solution,
"scripts": {
"dev": "SET NODE_CONFIG_ENV=development&&SET NODE_ENV=development&& nodemon server.js",
"qa": "SET NODE__CONFIG_ENV=qa&&SET NODE_ENV=qa&& node server.js",
"prod": "SET NODE_CONFIG_ENV=production&&SET NODE_ENV=production&& node server.js",
}
I added NODE_ENV in case it returns back to using it but overall I still didnt figure out what caused it to use NODE_CONFIG_ENV instead of NODE_ENV.
Edit: I found the reason! It was because of another npm package called cross-env which i used earlier.

Auto reloading a Sails.js app on code changes?

Currently is seems that for any code change in a sails.js app you have to manually stop the sails server and run sails lift again before you can see the changes.
I was wondering if there is any way when running in development mode to automatically restart the sails server when it detects a code change?
You have to use a watcher like forever, nodemon, or something else...
Example
Install forever by running:
sudo npm install -g forever
Run it:
forever -w start app.js
To avoid infinite restart because Sails writes into .tmp folder, you can create a .foreverignore file into your project directory and put this content inside:
**/.tmp/**
**/views/**
**/assets/**
See the issue on GitHub:
Forever restarting because of /.tmp.
You can use sails-hook-autoreload
Just lift your app as normal, and when you add / change / remove a model or controller file, all controllers and models will be reloaded without having to lower / relift the app.
For example with nodemon to watch api and config directories
.nodemonignore contents
views/*
.tmp/*
.git/*
Run the command after creating .nodemonignore
$> nodemon -w api -w config
Example for supervisor to ignore 3 directories
$> supervisor -i .tmp,.git,views app.js
If you're using Sails 0.11, you can install this hook to automatically reload when you change models or controllers (views do not require reloading):
npm install sails-hook-autoreload
https://www.npmjs.com/package/sails-hook-autoreload
install nodemon globally or locally.
npm install nodemon --save
npm install nodemon -g
install sails locally in you project as follows
npm install sails --save
then change package.json
from
"scripts": {
"debug": "node debug app.js",
"start": "node app.js"
},
to
"scripts": {
"debug": "node debug app.js",
"start": "node app.js",
"dev": "export NODE_ENV=development && nodemon --ignore 'tmp/*' app.js && exit 0"
},
then
npm run dev
I had the same problem and I have solved it using grunt-watch and grunt-forever with sails#beta tasks. The result is 4 grunt commands:
UPDATE: tasks are available in the current sails version (it's no longer beta :>)
start Starts the server
stop Stops the server
restart Restarts the server
startWatch Starts the server and waits for changes to restart it (using grunt-watch). This is probably your solution, but the other commands are also useful.
Here's the code - I'm using sails#beta, which includes a tasks directory, I don't know if this is included in previous versions:
First of all you have to install forever in your sails directory:
npm install grunt-forever --save-dev
tasks/config/forever.js Configure forever task.
module.exports = function(grunt) {
grunt.config.set('forever', {
server: {
options: {
index: 'app.js',
logDir: 'logs'
}
}
});
grunt.loadNpmTasks('grunt-forever');
};
tasks/config/watch.js (edit) Edit watch task in order to add a new rule
// api and assets default rules
,
server: {
// Server files to watch:
files: [
'api/**/*',
'config/**/*'
],
// Restart server
tasks: ['forever:server:restart']
}
tasks/register/watchForever.js Register your custom tasks (this file can be renamed to whatever you want)
module.exports = function(grunt) {
// Starts server
grunt.registerTask('start', [
'compileAssets',
'linkAssetsBuild',
'clean:build',
'copy:build',
'forever:server:start'
]);
// Restarts the server (if necessary) and waits for changes
grunt.registerTask('startWatch', [
'restart',
'watch:server'
]);
// Restarts server
grunt.registerTask('restart', [
'forever:server:restart'
]);
// Stops server
grunt.registerTask('stop', [
'forever:server:stop'
]);
};
With this you should be able to use
grunt startWatch
and make your server wait for changes to be restarted :>
Hope this helped!
Better you use
npm install -g nodemon
i am using this, and it will helps to improve my developing speed. no need to edit any files for this one!.
after installation
nodemon app.js
For anyone coming to this question now, it seems that this is no longer necessary - an application launched with sails lift will have a grunt watch task running, and code changes will be visible without a restart.
I didn't realise this was happening at first because there's nothing to indicate what's happening in the console, but it does seem to work without a restart (I'm using Sails 0.11)

Resources