Docker build command does not work for Nodejs - node.js

I am new into docker.
I am using linux to run docker. Docker successfully installed. But
When I run docker command sudo docker build . I always getting the error. How can i solve the issue?
My docker file is
FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", 'app.mjs' ]
Nodejs package.json file
{
"name": "1_getting-_started_docker",
"version": "1.0.0",
"description": "",
"main": "app.mjs",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
app.mjs
import express from 'express'
import { connectToDatabase } from './helpers.mjs';
const app = express();
app.get('/', (request, response) => {
response.send('<h1>Hi there</h1>');
});
await connectToDatabase();
// https://robinwinslow.uk/fix-docker-networking-dns

The Dockerfile looks fine, so it is very likely related to the Docker installation. You should probably try to restart the docker daemon: https://docs.docker.com/config/daemon/systemd/ and/or restarting your computer.
If that doesn't work pls show your complete logs that appear on the error message.

Related

Running Electron directly (electron app.js) vs. running from package.json script (npm run electron)

I have a simple Electron tray app with the following source:
const { app, Tray, Notification, Menu, nativeImage } = require('electron');
const path = require('path');
const iconPath = path.join(__dirname, 'assets/icons/iconTemplate.png');
let tray = null;
app.whenReady().then(() => {
try {
console.log(iconPath);
tray = new Tray(nativeImage.createFromPath(iconPath));
tray.setToolTip('Electron app');
} catch (e) {
console.log(e);
}
})
Here's the package.json:
{
"name": "Electron app",
"version": "0.0.1",
"description": "",
"main": "app.js",
"scripts": {
"electron": "electron app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"package-linux": "electron-packager . --overwrite --icon=assets/icons/256x256.png --out=release-builds",
"deb64": "electron-installer-debian --config config/build-config.json"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
...
},
"devDependencies": {
...
}
}
When I run the app with electron app.js, nothing happens. But when I run npm run electron (which runs the script from package.json) the app works. What's the difference?

Unable to start node application inside docker

While creating my node app inside docker I am getting below error
docker container run -it --rm --volume $(pwd):/usr/src/app -p 7007:3000 sample-app-dev:latest
docker: Error response from daemon: OCI runtime create failed: container_linux.go:344: starting container process caused "exec: \"nodemon server.js\": executable f
My Dockerfile looks like below
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["nodemon server.js"]
Package.json
{
"name": "nodedocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}
server.js
const express = require( "express" );
const app = express();
const port = 3000;
app.get( "/", ( req, res ) => {
res.send( "Hello World from express JS!!" );
} );
const hobbies = [
"Swimming", "Diving", "Jogging", "Cooking", "Singing" ] ;
app.get("/hobbies", (req, res) => {
res.send(hobbies);
});
const famous_programming = [
"python", "java", "c", "c++", "JS" ] ;
app.get("/famous_programming", (req, res) => {
message = famous_programming.includes("node")? "Yaaay, Node.js" : "Yup! Node.js is a framework from JS" ;
res.send(message);
});
app.listen( port, () => {
console.log( `Node JS started on http://localhost:${port}` )
} );
I am not sure what else I am missing here, any inputs greatly appreciated.
Thank you.
You could configure the package to run nodemon server.js on start, then specify npm start in the entrypoint:
Dockerfile:
FROM node:12
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
ENV PATH="/usr/src/app:${PATH}"
ENTRYPOINT ["npm", "start"]
package.json:
{
"name": "nodedocker",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.4"
}
}
Test:
$ docker container run -it --rm q63534772
> nodedocker#1.0.0 start /usr/src/app
> nodemon server.js
[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Node JS started on http://localhost:3000

Elastic Beanstalk: Deploy Nodejs status Degraded Following services are not running: web

I just use eb deploy and also tried upload with zip, the elatic beanstalk environment show "degraded".
The log when I click in to see is
Overall
Degraded
Impaired services on all instances.
Severe
Following services are not running: web.
My app.js file:
const express = require('express');
const app = express();
app.listen(3000, () => {
console.log("Sever console log.")
});
package.json content
{
"name": "project_api",
"version": "1.0.0",
"engines": {
"node": "12.18.3"
},
"description": "the server side of the api",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Businsoft Limited",
"license": "ISC",
"dependencies": {
"dropbox": "^5.2.1",
"express": "^4.17.1",
"isomorphic-fetch": "^2.2.1",
"nodemon": "^2.0.4"
}
}
I think this is the simpliest start with Elastic Beanstalk, but somehow it fails. Any idea to solve it?
After debug for a while, I managed to figure out the issue.
In package.json
I need to remove this line:
"main": "app.js",
And the add the "start" in the "scripts" like this:
"scripts":
"start": "node app.js"
And then update the
app.listen(3000, () => {
console.log("Sever console log.")
});
to
const port = process.env.port || 3000;
app.listen(port, () => {
console.log("Sever console log.")
});
And then go to AWS Console > Elastic Beanstalk's environment > Configuration > Software Edit > add a properties at Environment properties
Name port Value 8080
Finally reupload the source code, and it works.

Heroku error module not found 'json-server'

I want to deploy my server on Heroku, but on Heroku build it says this:
The app works fine locally, but somehow it crashes on the Heroku server.
This is the file structure
This is the code for server.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('data.json');
const port = process.env.PORT || 4000;
server.use(router);
server.listen(port);
This is package.json
{
"name": "server",
"version": "1.0.0",
"description": "json-server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm install && node server.js"
},
"author": "japsuchiha",
"license": "ISC",
"dependencies": {
"core-util-is": "^1.0.2"
},
"devDependencies": {
"json-server": "^0.15.1"
}
}
Pure guess, but I don't think heroku is installing json-server since it's listed under devDependencies

npm start does nothing while the command works properly when using NodeJS with Express

UPDATE:
I had nvm for windows installed and forgot about it. It was projecting the contents of its node installation to the program files folder.
ORIGINAL:
So I have a very simple project copied from freecodecamp
index.js
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('Hello World!'));
app.listen(3000, () => console.log('Example app listening on port 3000!'));
package.json
{
"name": "test-app", "version": "1.0.0", "description": "", "main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [], "author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Running node index.js starts the server and operates as expected, but if I run npm start it just returns:
> test-app#1.0.0 start D:\temp\test-app
> node index.js
Microsoft Windows [Version 10.0.17134.885]
(c) 2018 Microsoft Corporation. All rights reserved.
and exits out.
I would expect that npm start does the same thing as node index.js since it uses the same command, as described here.
What did I do wrong?
I have tried:
running npm cache clean --force
removing node_modules and running npm install
removing all traces of node and reinstalling a different version
EDIT: project structure looks like
D:\temp\test-app
├── node_modules
├── index.js
├── package-lock.json
└── package.json
Just rename your index.js file to server.js and remove the "start": script entirely. Then npm start will work normally.
{
"name": "test-app", "version": "1.0.0", "description": "", "main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [], "author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Please try with npm run start instead of npm start in the same directory as your package.json

Resources