npm run dev --host network: not exposed - linux

I want to expose my svelte app on LAN using the npm run dev --host command but it shows:
> frontend#0.0.1 dev
> svelte-kit dev
SvelteKit v1.0.0-next.295
local: http://localhost:3000
network: not exposed
Use --host to expose server to other devices on this network

You have to add -- before the actual flag:
npm run dev -- --host
And it should output:
> project#0.0.1 dev
> svelte-kit dev "--host"
SvelteKit v1.0.0-next.316
local: http://localhost:3000
network: http://***.***.**.**:3000
Note that all files in the following directories will be accessible to anyone on your network: src/lib, src/routes, .svelte-kit, src, node_modules

If you use vite in the package.json file, do this on the dev line:
"scripts": {
"dev": "vite --host --port 8888",
..... what ever else was here.....
},
if you use sirv, try this in the package.json :
"start": "sirv public --no-clear --host 0.0.0.0",
If you want a different port try this:
"start": "sirv public --no-clear --host 0.0.0.0 --port 8888",

In vite Two Methods as follows:
Method 1: Manually while running the app by typing this on the terminal
npm run dev -- --host
Method 2: Automatically By replacing the "dev" property line in package.json in the "scripts" object as follows:
"dev": "vite --host --port [PORT NO. YOU WANT TO USE]",
And when you run "npm run dev", it shows the network host address

Related

Docker container is refusing connection

I am using Dockerfile for Nodejs project but its returning Connection refused error
When I run the app without Docker it works absolutely fine.
Command I use to build and run the docker container is as follows:
docker build -t myapp .
docker run -it -p 8080:8080 myapp
Running above command runs without any error but when I hit http://localhost:8080/test-url it fails
My dockerfile is as follows:
FROM node:16.16.0-alpine
ADD . /opt
COPY . .
RUN npm install
EXPOSE 8080
RUN chmod +x /opt/deploy.sh
RUN apk update && apk add bash
CMD ["/bin/bash", "/opt/deploy.sh"]
And my package.json is as follows (truncated to show only script):
"scripts": {
"start": "DEBUG=app* node index.js",
"build": "rimraf build && babel-node ./src --out-dir build/src && npm run docs",
"dev": "DEBUG=app* nodemon --exec babel-node index.js",
"lint": "eslint 'index.js' 'src/**/*.js' 'src/index.js'",
"docs": "apidoc -i src/ -o public/docs",
"prepare": "husky install",
"lint-staged": "lint-staged"
},
For development I use following command which works fine::
npm run dev
For deploymeent I run deploy.sh which has env variables and final command as ::
npm run build
npm run start
Even when I am trying http://localhost:8080/test-url by loging into docker interactive terminal it returns same error - Connection Refused
Your Port mapping looks right to me. Did you check your firewall? Maybe it is blocking the connection. It could also be helpful to test, if you can run an NGINX-Container on Port 8080. That way you can check if it is a general configuration-problem with Docker or a specific problem of your Image.
Also, did you try to set your node server to listen to 0.0.0.0 instead of localhost? I'm not sure how Docker handles IPs in the Containers, but I was thinking maybe it is called by it's internal ip. If that is the case and your server listens to localhost only, it shouldn't accept the connection.
I hope one of these things can point you in the right direction.

Nodejs deploy on heroku different node (each node has a different port number)

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.

Specify PORT environment variable on Windows

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"
}

npm start to bind to linux container ip address

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

Why pm2 NodeJS app starts but not accessible on EC2

I have a Nodejs app running on AWS EC2. My package.json has the following instructions:
"scripts": {
"build": "babel src -s -D -d dist --presets es2015,stage-0",
"start": "node dist/index.js",
"prestart": "npm run build",
..
So when connected to EC2 I (after install and cd to proj folder) I do PORT=8080 npm start The app starts fine - but messages in the console and is acessinble via my EC2 addres :8080. Also if I run PORT=8080 node dist/index.js
- also good.
But since I would like to use monitoring, restarting of the script by pm2 I try do following:
pm2 start dist/index.js -- PORT=8080
or
PORT=8080 pm2 start dist/index.js
I see that pm2 has the app started,
but it's not acessible on AWS address :8080
What do I do wrong?

Resources