Unable to run node.js app through Docker container - node.js

I have a node.js application which I am trying to run in docker. Here is the package.json file snippet.
"main": "lib/server.js",
"scripts": {
"clean": "cross-env rm -rf dist",
"build": "cross-env babel lib -d dist",
"start": "npm run clean && npm run build && npm run serve",
"serve": "node dist/index.js",
"dev": "cross-env NODE_ENV=development nodemon lib/server.js --exec babel-node",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint --ext .js lib/ scripts/ --ignore-pattern node_modules/"
}
And here is the Dockerfile to build the image
#---- Base Node------
FROM node:10.15.1-alpine
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package*.json ./
COPY lib ./lib
RUN npm install --only=production && npm run build
EXPOSE 4201
CMD ["npm", "run", "serve"]
I get the following error when I try to run the image. Here is the screenshot of the error.
The node and npm version are the same on my machine and the base image that I am using to build my image.
When I run the same command locally in my machine it works
Here is the exact command that I am running on my machine and it works.
npm install --only=production
npm run build
npm run serve
Am I doing something wrong here ? Any help is appreciated.

You are using ES6 imports
import x from package
In order to do that, you should have Babel package installed, otherwise it won't know how to import that file

Related

Custom script in NPM package.json is ignored, runs as standalone

in my package.json I have these scripts:
"scripts":
{
"lint": "./node_modules/.bin/eslint src",
"build": "npm run clean && npm run dev && npm run prod",
"tsc": "./node_modules/.bin/tsc",
"dev": "./node_modules/.bin/webpack --config webpack.dev.js",
"prod": "./node_modules/.bin/webpack --config webpack.prod.js",
"clean": "del /q inet-henge*.js* dist",
"watch": "./node_modules/.bin/webpack --config webpack.dev.js --watch",
"postinstall": "copy inet-henge.js \"..\\flask\\customer_topology\\static\""
}
But for some reason postinstall is completely ignored when i run npm run build. When I run it as standalone script, it runs, the file is copied to the new location. Path is correct. What could be the problem, I am on Windows
post<x> runs after <x> so postinstall runs after install and if you want a script to run after build it should be called postbuild See https://docs.npmjs.com/cli/v6/using-npm/scripts for more details.

node_module and aws lambda functions

I have created the lambda application using node. The main issue I face is moving node_module with the npm build. Aws lambda excepts node modules also share the codeUri directory. So I use babel to move all my code .build directory including package.json. Which the best way to move the node_module to .build folder.
"scripts": {
"test": "cross-env NODE_ENV=test jest",
"build": "npm run clean:lib && mkdirp ./.build && npm run copyAndInstall && npm run build:babel",
"build:noclean": "npm run copyAndInstall && npm run build:babel && mkdirp ./.build ",
"build:lint": "eslint -c ./.eslintrc.json --ignore-path ./.eslintignore --ext .jsx,.js \"**\\*.+(js|jsx)\"",
"build:babel": "babel ./src --out-dir ./.build --ignore bower_components,coverage,node_modules,test,.dist,.build,temp,./start.js --comments false",
"clean:lib": "npm install rimraf && rimraf ./build",
"update:modules": "rimraf ./package-lock.json && rimraf ./node_modules && npm i",
"prepush": "npm run build:fix",
"build:fix": "eslint --fix -c ./.eslintrc.json --ignore-path ./.eslintignore --ext .jsx,.js \"**\\*.+(js|jsx)\"",
"copyAndInstall": "copyfiles package.json ./.build/
}
I even tried npm install --production --prefix ./.build" it give an error like
refusing to install package with name under a package npm err! also called did you name your project the same as the dependency you're installing?

Transpile using babel-preset-env without ejecting from create-react-app

I create a react project using create-react-app. And in order to pack it to run elsewhere, i use Docker with this config file:
My Dockerfile
FROM node
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
RUN npm run transpile
CMD PORT=$PORT npm run start:prod
When i run my app inside docker using docker build . -t name_repo, the build stops at step 7/8 (npm run transpile) where i need to transpile the codes using babel (babel-preset-env and babel-preset-react-app) and got this error:
package.json:
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"transpile": "NODE_ENV=production babel src --out-dir transpiled --presets env,react-app",
"start:prod": "NODE_ENV=production node server.js"
},
...
I think Node_env is unlikely able to find the present name in the production configuration file . I would suggest please check all the parameters once again and use SET NODE_ENV =production

Getting "Cannot find module '/app/dist'" error on deploying to Heroku

I have the following script in my package.json:
"scripts": {
"dev": "nodemon server --exec babel-node --presets es2015,stage-2",
"build": "babel ./server -d ./dist",
"start": "node ./dist",
"heroku-postbuild": "cd react-ui/ && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
On deploying to Heroku I get the following error
Error: Cannot find module '/app/dist'
On local npm run dev, npm run build, and npm run start work fine.
Where is it getting the /app folder? How to fix this?
Thanks
Matloob
You should build your app npm run build before deploy to heroku. It will run heroku-postbuild first then start your app.
Try this :
"start:heroku": "node dist/YOUR-APP-NAME/server/main.js",

Docker proper redirect link

I have Dockerfile:
FROM cloudron/base:0.10.0
ENV PATH /usr/local/node-6.9.5/bin:$PATH
WORKDIR /tmp
COPY package.json /tmp/
RUN npm config set registry http://registry.npmjs.org/ && npm install
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN cp -a /tmp/node_modules /usr/src/app
RUN npm run build
EXPOSE 8000
CMD [ "npm", "run", "start:production" ]
if I run this by:
docker run -p 8000:8000 -t somename/appname
then
going to http://localhost:8000 works fine
but if I want go to http://localhost:8000/about I get error:
Cannot GET /about
What should I do to make it well configured ?
Of course my react app, works fine with such link using in development start.
EDIT:
"scripts": {
"clean": "rimraf dist",
"compile": "node build/scripts/compile",
"build": "npm run clean && cross-env NODE_ENV=production npm run compile",
"start": "cross-env NODE_ENV=development node build/scripts/start",
"start:production": "cross-env NODE_ENV=production node build/scripts/start",
"test": "cross-env NODE_ENV=test karma start build/karma.config",
"test:watch": "npm test -- --watch",
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix"
},
looks like my start:production is not good..

Resources