In my settings I run my server on port 4000. I want to run it now on port 5000 in another instance. I have read here:
https://medium.com/the-node-js-collection/making-your-node-js-work-everywhere-with-environment-variables-2da8cdf6e786
that all you need to do is something like:
PORT=5000 node server.js
Now, this is what I have in my package.json:
"scripts": {
"watch": "nodemon -e ts -w ./src -x npm run watch:serve",
"watch2": "PORT=5000 yarn watch",
"watch:serve": "ts-node --inspect src/index.ts",
}
I useally run "yarn watch" and my server runs on port 4000. yarn watch2 should make it run on port 5000 but I get the following error:
"'PORT' is not recognized as an internal or external command"
I assume it is because I am working on Windows?
What can I do to solve it?
The npm package cross-env is an excellent solution to setting environment variables across different platforms. For example, you would use:
"scripts": {
"watch2": "cross-env PORT=5000 yarn watch"
}
Related
For the life of me I can't get Browser Sync and Nodemon to run nicely alongside my Express server. I have tried every combination I can imagine.
My Express server runs at port 5000 which I can open and view, nodemon runs when changes are made, great but the browser still doesn't 'hot refresh' so to speak. I would like for the browser window to either refresh or open a new tab after nodemon has restarted the server.
package.json scripts
"scripts": {
"start": "node app.js",
"dev": "set NODE_ENV=DEV&& nodemon app.js 5000 browser-sync start --proxy localhost:5000 -e * --ignore=node_modules --reload-delay 10 --no-ui --no-notify",
"ui": "node app.js browser-sync start nodemon app.js --port=5001 --proxy=localhost:5000 -e * --ignore=node_modules --reload-delay 10 --no-ui --no-notify",
"ui2": "nodemon start & browser-sync start --proxy 'localhost:5000' -e * --ignore=node_modules"
},
I just want to start my express server, listen for changes with nodemon then restart, then either reload browser window or launch a new one to see the changes. Please help me understand what I am missing?
In case someone has the same problem, the aha moment was when I realized I needed to open a 2nd terminal window to run browser-sync. And using the below scripts in package.json now works beautifully!
So first run npm run start/dev depending if you want server restarts on changes or not then open a 2nd terminal window and run npm run ui`. In my case my app.js is launching on port 8000.
package.json
"scripts": {
"start": "node app.js",
"dev": "nodemon -e * app.js",
"ui": "browser-sync start --proxy localhost:8000 --files=**/* --ignore=node_modules --reload-delay 10000 --no-ui --no-inject-changes"
},
app.js - const port = process.env.PORT || '8000';
This can be run (npm start) in parallel with modules "npm-run-all" or with "concurrently" (replace start: string with start_b string).
"scripts": {
"browsersync": "browser-sync start --proxy localhost:5000 --files 'public,views'",
"nodemon": "nodemon server.js",
"start_b": "concurrently --kill-others \"npm run nodemon\" \"npm run browsersync\" ",
"start": "npm-run-all -p nodemon browsersync"
},
In my system, I had problems tracking .njk files, because the browser reloaded after changing them but without actually updating changes. I had to use a nodemon.json file to add the folder (views/) and extension .njk:
{
"watch": ["server.js", "views/"],
"ext": "js, css, njk"
}
I am learning Node.js and this is my first code.
I created a file called server.js below with the code
server.js
const express = require('express');
const dotenv = require('dotenv');
//load env vars
dotenv.config({ path: './config/config.env'});
const app = express();
const PORT = process.env.PORT || 5000;
app.listen(
PORT,
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`)
);
I have this section in my package.json file
package.json
"scripts": {
"start": "NODE_ENV=production node server",
"dev": "nodemon server"
},
Here is the content of my config.env file
config.env
NODE_ENV=development
PORT=5000
When I run npm run dev everything is fine and runs
When I run npm start to run production, I get the error below.
'NODE_ENV' is not recognized as an internal or external command,
operable program or batch file.
How can I resolve this? I need npm start to run
For Windows:
"scripts": {
"start": "node server.js",
"dev": "set NODE_ENV=DEVELOPMENT && node server",
"prod": "set NODE_ENV=PRODUCTION && node server"
}
For UNIX and other OS:
"scripts": {
"start": "node server.js",
"dev": "export NODE_ENV=DEVELOPMENT && node server",
"prod": "export NODE_ENV=PRODUCTION && node server"
}
By the error message, you are running this in Windows. You need to use Set to setup an environment variable.
"scripts": {
"start": "Set NODE_ENV=production&node server",
"dev": "nodemon server"
}
However, setting up environment variables in this manner is less secure and platform dependent. In other words, any attacker getting access to your server file system can set any environment variable by modifying the package.json. Also, if you decide to move your production to a Linux host later, your start script is going to be broken again.
So, the best practice is to set your environment variables via host configuration setup. Different cloud providers offer different methods for this.
Also, you might not need to use npm to run your script at all. You can call node server directly in your shell.
An easy way to solve this problem:
npm install --save-dev cross-env
"start": "cross-env NODE_ENV=production node server"
This means that you don't have to worry about the platform
for read more: https://www.npmjs.com/package/cross-env
i have different node, here my package.json
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"node_1": "nodemon --watch dev -e js dev/networkNode.js 3001 http://localhost:3001",
"node_2": "nodemon --watch dev -e js dev/networkNode.js 3002 http://localhost:3002",
"node_3": "nodemon --watch dev -e js dev/networkNode.js 3003 http://localhost:3003",
"node_4": "nodemon --watch dev -e js dev/networkNode.js 3004 http://localhost:3004",
"node_5": "nodemon --watch dev -e js dev/networkNode.js 3005 http://localhost:3005"
},
In my local env i run:
npm run node_1
npm run node_2
npm run node_3
npm run node_4
npm run node_5
And the node are accessibile in http://localhost:300X url.
Now i want to deploy on Heroku my prototype.
if i put:
"node_1": "nodemon --watch dev -e js dev/networkNode.js 3001 https://my-heroku-app.herokuapp.com:3001",
And then i go to:
https://my-heroku-app.herokuapp.com:3001
The app doesn't open, but the build is success.
I tried:
Create Procfile:
web: npm run node_1
-Running via Heroku Cli
heroku run npm run node_1
Not works...
How can i set this node on heroku?
What you are trying to do is not possible on Heroku. A deployed App on Heroku is assigned only one port. Furthermore you cannot specify the port. It is supplied to you by Heroku via the environment variable $PORT.
#Tin Nguyen answer is not exact. On Heroku it's mandatory to bind something on $PORT, but then you can bins something else to some other port.
See https://devcenter.heroku.com/articles/dynos#web-dynos
Also see How to run Django and node.js app on same dyno in Heroku? for how to run multiple backends on a dyno and let them communicate.
It makes the case with Django+Node, but it can be used with Node+Node as well.
I need to be able to bind my linux container 'npm start' address to 10.XX.XX.XX so I may be able to view from my host box. Both host and container are Ubuntu. Host is Ubuntu 18 and container is 16 Xenial.
I am currently following a netlify-cms victor-hugo tutorial. My setup is on a linux container running Ubuntu 16 Xenial. npm works however, I am unable to figure out how to bind the address to the linux container ip address.
I can usually figure this out as servers will usually have a -b flag, or some kind of binding or host setting to use or something similar but I can't figure this one out. I've done modifications to the package.json file and some online examples use http-server but my setup with netlify-cms and victor-hugo uses "start":"run-p start:**". A search for run-p examples come up blank.
I have very little experience with node.js and npm. here is a partial of the package.json setup
{
"name": "victor-hugo",
"version": "1.0.0",
"description": "Victor Hugo is a Hugo boilerplate for creating truly epic websites!",
"repository": "netlify/victor-hugo",
"main": "index.js",
"scripts": {
"lint": "eslint src",
"start": "run-p start:**",
"start:hugo": "hugo -d ../dist -s site -vw",
"start:webpack": "webpack-dev-server --config webpack.dev.js",
"preview": "run-p preview:**",
"preview:hugo": "npm run start:hugo -- -D -F",
"preview:webpack": "npm run start:webpack",
"prebuild": "rimraf dist",
"build": "npm run build:webpack && npm run build:hugo",
"build:preview": "npm run build:webpack && npm run build:hugo:preview",
"build:hugo": "hugo -d ../dist -s site -v",
"build:hugo:preview": "npm run build:hugo -- -D -F",
"build:webpack": "cross-env NODE_ENV=production webpack --config webpack.prod.js --hot --inline"
},
...
}
I need to be able to browse to 10.XX.XX.XX to view my content.
In your webpack.dev.js file, there may be a section like below:
devServer: {
...,
...,
host: '10.XX.XX.XX',
port: 80,
}
or you can change the webpack-dev-server command to allow the host:
"start:webpack": "webpack-dev-server --config webpack.dev.js --allowed-hosts example.com",
Not sure you can use an ip address as a replacement for example.com, but worth a try.
not sure if this is still of interest, but I wanted to bind the address to 0.0.0.0 (since I am running npm within a Docker container) and it worked for me by setting the --host option in start:webpack option, like so:
"start:webpack": "webpack-dev-server --config webpack.dev.js --host 0.0.0.0"
Hope this still helps someone
I have this start params in package.json
"scripts": {
"start": "node bin/www"
},
It is running my express app when I am typing npm start.
But I want browser opened http://localhost:8081 at the same time. How can I say to start to open my local url as well?
like: "start": "node bin/www, http://localhost:8081"
So when I am typing npm satrt it runs my express app and opens the url at the same time.
As far as I know it's like writing a bash command:
// Windows
"start":"start http://localhost:8081 & node bin/www"
// Mac
"start":"open http://localhost:8081 && node bin/www"
// Linux
"start":"xdg-open http://localhost:8081 && node bin/www"
For cross-platform support use open-cli.
Install it:
npm install --save-dev open-cli
Add it to your scripts:
"start": "open-cli http://localhost:8081 && node bin/www"
You just need to use start in the right order!
"start": "npm run dev & start http://localhost:8000",
Bad
"start": "start http://localhost:8000 & npm run dev",
Good