Docker - Puppeteer require somethings and wont run - node.js

I've created a container with Docker from a NodeJS app,
but when I try to run it the application will start but goes in error loop.
My Dockerfile contain
RUN npm install
but it continuously break on:
ERROR PuppeteerCrawler:PuppeteerPool: Browser launch failed {"id":12}
Error: Could not find browser revision 756035. Run "npm install" or "yarn install" to download a browser binary
If i run Node main.js there are no problem.
//Dockerfile
FROM apify/actor-node-basic
COPY package*.json ./
RUN npm --quiet set progress=false \ && npm install --only=prod
--no-optional \ && echo "Installed NPM packages:" \ && npm list \ && echo "Node.js version:" \ && node --version \ && echo "NPM version:" \ && npm --version
COPY . ./
WORKDIR .
USER node
RUN npm install
COPY --chown=node:node . .
EXPOSE 8080
CMD [ "node", "Worker.js" ]
// package.json
{
"name": "SoccerBrain",
"version": "0.0.1",
"description": "This is an example of an Apify actor.",
"dependencies": {
"apify": "^0.21.0",
"puppeteer": "^5.0.0",
"rimraf": "^3.0.2"
},
"devDependencies": {},
"scripts": {
"start": "node Worker.js",
"test": "echo \"Error: oops, the actor has no tests yet, sad!\" && exit 1"
},
"author": "It's not you it's me",
"license": "ISC"
}
what's append?

Related

Mixing concurrently and cross-env in a package.json script

Building a monorepo template, I'm facing an issue in the scripts of my package.json. I'm trying to make my scripts compatible with npm alternatives like yarn or pnpm. So I tried to use a config object to specify the wanted CLI:
{
"config": {
"cli": "npm"
}
}
Then I was able to access the value through the environment variable npm_package_config_cli. Since the usage of environment variables has different syntaxes between unix systems and windows, I used cross-env. This worked well for most of my scripts, for example:
"dev-front": "cross-env-shell \"cd ./frontend && $npm_package_config_cli run dev\"",
Here you can see the usage of cross-env-shell to execute the command between quotes:
cd ./frontend && $npm_package_config_cli run dev
With $npm_package_config_cli being resolved by cross-env according to the OS.
My package.json purpose is to drive the scripts in frontend and backend folders of my monorepo template. So I'm using concurrently to keep track of the different outputs. My issue is when using cross-env-shell along with concurrently. I tried to call cross-env-shell "inside" concurrently and vice-versa, with single and/or double quotes, but $npm_package_config_cli is not resolved on windows.
A minimal package.json with the failing dev script (dev-front and dev-back are working):
{
"config": {
"cli": "npm"
},
"scripts": {
"dev": "concurrently -n front,back -c green,yellow -t 'HH:mm:ss' -p '{name} {time}' 'cross-env-shell \"cd ./frontend && $npm_package_config_cli run dev\"' 'cross-env-shell \"cd ./backend && $npm_package_config_cli run dev\"'"
"dev-front": "cross-env-shell \"cd ./frontend && $npm_package_config_cli run dev\"",
"dev-back": "cross-env-shell \"cd ./backend && $npm_package_config_cli run dev\""
},
"devDependencies": {
"concurrently": "^7.6.0",
"cross-env": "^7.0.3"
}
}
Any hint on the valid syntax? Is there a better approach?
Trying harder, the following combination worked with concurrently wildcards:
{
"config": {
"cli": "npm"
},
"scripts": {
"dev": "cross-env-shell 'concurrently -c green,yellow -t \"HH:mm:ss\" -p \"{name} {time}\" \"$npm_package_config_cli:dev-*\"'",
"dev-front": "cross-env-shell \"cd ./frontend && $npm_package_config_cli run dev\"",
"dev-back": "cross-env-shell \"cd ./backend && $npm_package_config_cli run dev\""
},
"devDependencies": {
"concurrently": "^7.6.0",
"cross-env": "^7.0.3"
}
}

Typescript in Docker throws Unexpected node on compile

I want to dockerize my app, but on running tsc I've got this error:
/usr/src/app/node_modules/typescript/lib/tsc.js:94444
throw e;
^
Error: Debug Failure. False expression: Unexpected node.
Verbose Debug Information: Node 348 did not pass test 'isLeftHandSideExpression'.
at Object.visitNode (/usr/src/app/node_modules/typescript/lib/tsc.js:73032:18)
at visitNonNullExpression (/usr/src/app/node_modules/typescript/lib/tsc.js:76066:33)
at visitTypeScript (/usr/src/app/node_modules/typescript/lib/tsc.js:75205:28)
at visitorWorker (/usr/src/app/node_modules/typescript/lib/tsc.js:75029:24)
at saveStateAndInvoke (/usr/src/app/node_modules/typescript/lib/tsc.js:74988:27)
at visitor (/usr/src/app/node_modules/typescript/lib/tsc.js:75025:20)
at visitNode (/usr/src/app/node_modules/typescript/lib/tsc.js:73018:23)
at Object.visitEachChild (/usr/src/app/node_modules/typescript/lib/tsc.js:73338:69)
at visitTypeScript (/usr/src/app/node_modules/typescript/lib/tsc.js:75221:31)
at visitorWorker (/usr/src/app/node_modules/typescript/lib/tsc.js:75029:24)
error Command failed with exit code 1.
But if I run tsc outside of the container it compiles smoothly.
UPDATE:
Dockerfile:
# ================ #
# Base Stage #
# ================ #
FROM node:16-buster-slim as base
WORKDIR /usr/src/app
RUN apt-get update && \
apt-get upgrade -y --no-install-recommends && \
apt-get install -y --no-install-recommends build-essential python3 libfontconfig1 dumb-init && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["dumb-init", "--"]
# ================ #
# Develop Stage #
# ================ #
FROM base as development
ENV NODE_ENV="development"
COPY --chown=node:node . /usr/src/app
CMD ["yarn", "run", "docker:watch"]
Package.json scripts:
"scripts": {
"migrate:deploy": "npx prisma migrate deploy",
"migrate:dev": "npx prisma migrate dev",
"build": "tsc",
"start": "nodemon dist/App.js",
"format": "prettier --write \"src/**/*.ts\"",
"predocker:watch": "yarn install",
"docker:watch": "tsc-watch --onSuccess \"yarn start\""
},
Tsconfig:
{
"extends": "#sapphire/ts-config",
"compilerOptions": {
"rootDir": "src",
"outDir": "dist",
"tsBuildInfoFile": "dist/.tsbuildinfo"
},
"include": ["src"]
}

A2Hosting: npm works in terminal, but not for cron worker

I am using A2hosting and trying to configure cron worker, but no luck yet.
I followed this article to install node and npm, which is basically list of commands below
cd ~
wget https://nodejs.org/dist/v12.9.1/node-v12.9.1-linux-x64.tar.xz
tar xvf node-v12.9.1-linux-x64.tar.xz
mv node-v12.9.1-linux-x64 nodejs
mkdir ~/bin
cp nodejs/bin/node ~/bin
cd ~/bin
ln -s ../nodejs/lib/node_modules/npm/bin/npm-cli.js npm
I have tried multiple commands
npm run --prefix ~/cloudflare-upload-tool start which produces /bin/bash: npm: command not found
and
~/nodejs/bin/npm run --prefix ~/cloudflare-upload-tool start which produces /usr/bin/env: node: No such file or directory
Both commands run fine in terminal, could you tell what are my options to fix this?
package.json
{
"name": "cloudflare-upload-tool",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "env-cmd node index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0",
"cloudflare": "^2.7.0",
"env-cmd": "^10.1.0",
"form-data": "^3.0.0",
"fs": "0.0.1-security",
"jsonwebtoken": "^8.5.1",
"path": "^0.12.7",
"tus-js-client": "^2.2.0"
}
}
Support told that this approach is not supported. Ended up creating node js app using UI, stopping it as we don't need to expose it and using command for cron worker below as suggested in other article
source /home/<USERNAME>/nodevenv/cloudflare_upload_tool/10/bin/activate && cd /home/<USERNAME>/cloudflare_upload_tool && npm run start

How do I use nodemon with docker in my node.js application?

I am new to Docker and coding. I have already added it as dev-dependency but still I have to build the image every time I make a change to the code. I have tried looking this up but have not found a solution that is suitable/working because I am using process.json file.
My Dockerfile :
FROM node:12.14.1-alpine
# app name
ENV APP_NAME=mock-api
ENV WORK_DIR /deploy/${APP_NAME}
# Create app directory
RUN mkdir -p ${WORK_DIR} && \
chown node:node ${WORK_DIR}
RUN apk add --update gnupg
WORKDIR ${WORK_DIR}
COPY ["yarn.lock", "package.json", "./"]
RUN yarn global add pm2 && yarn install --prod --frozen-lockfile && yarn cache clean
COPY --chown=node:node . .
EXPOSE 3000
USER node
CMD ["pm2-runtime", "--no-daemon", "--raw", "process.json"]
Process.json :
{
"apps": [
{
"name": "mock-api",
"script": "./app.js"
}
]
}
package.json:
{
"name": "mock-api",
"version": "1.0.0",
"description": "Mock API for the testing environment",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "arorasannidhya#gmail.com",
"license": "ISC",
"dependencies": {
"koa": "2.12.0",
"koa-joi-router": "^6.0.2",
"koa-logger": "3.2.1",
"koa-router": "9.0.1",
"openpgp": "4.10.4",
"pm2": "^4.2.3",
"uuid": "^7.0.2"
},
"devDependencies": {
"nodemon": "^2.0.4"
}
}
Nodemon is the utility to run the node application (it watches file in the directory and when changed runs index.js or app.js (whatever is your root file))
It cannot be used to build a docker image, you will need to do something like this https://vsupalov.com/rebuilding-docker-image-development/#:~:text=In%20Conclusion,see%20the%20results%20right%20away!

Having an error in my Docker Multi-Stage script

I am trying to create a docker container for my application but there seems to be an error with my Docker script. Whenever I run the command docker build - < Dockerfile, I get the following output:
I'm not entirely sure why this is happening, since my folder layout is the following:
root folder, Docker ---
server ---- package.json
api
tests
In case the folder layout was a bit confusing, I have my docker file inside my root folder and inside the root folder is the folder called server which contains my package.json, my api files and the tests.
Here's my docker script:
# --- Base Node ---
FROM alpine:3.8 AS base
#install node
RUN apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/v3.7/main/ nodejs=8.9.3-r1 tini
# set working directory
WORKDIR /usr/src/app
# set tini as entrypoint
ENTRYPOINT ["/sbin/tini", "--"]
# copy project file
COPY . server/package*.json ./
# --- Dependencies ---
FROM base AS dependencies
# install node packages
RUN npm set progress=false && npm config set depth 0
RUN npm install .
# copy production node_modules aside
RUN cp -R node_modules prod_node_modules
# install ALL node_modules, including 'devDependencies'
RUN npm install
#
# ---- Test ----
# run linters, setup and tests
FROM dependencies AS test
COPY . .
RUN npm run lint && npm run test
#
# ---- Release ----
FROM base AS release
# copy production node_modules
COPY --from=dependencies /root/server/prod_node_modules ./node_modules
# copy app sources
COPY server/ ./
# expose port and define CMD
EXPOSE 3003
CMD [ "npm", "start" ]
I've used this question as an example.
Why am I getting this error from my docker script?
The node_modules need was generated by npm install according to parse package.json.
But, not all package.json will make npm install generate this folder, only the one with devDependencies, dependencies could results in the generate of nodes_moduls folder.
I give you a sample package.json which could manage that:
package.json which will generate node_modules:
{
"name": "my-demo",
"version": "1.0.0",
"description": "a project",
"main": "index.js",
"scripts": {
"build": "weex-builder src dist",
"build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
"dev": "weex-builder src dist -w",
"serve": "serve -p 8080"
},
"keywords": [
"weex"
],
"author": "xxx#gmail.com",
"license": "MIT",
"devDependencies": {
"babel-core": "^6.14.0"
},
"dependencies": {
"weex-html5": "^0.3.2"
}
}
Above build will ends ok with next:
Step 8/10 : RUN npm install .
---> Running in 181f572843bc
> core-js#2.6.9 postinstall /usr/src/app/node_modules/core-js
> node scripts/postinstall || echo "ignore"
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo#1.0.0 No repository field.
added 53 packages in 6.491s
Removing intermediate container 181f572843bc
---> 016c98d9650d
Step 9/10 : RUN cp -R node_modules prod_node_modules
---> Running in c24631cc4bc6
Removing intermediate container c24631cc4bc6
---> de413db9140c
But if you remove devDependencies & dependencies like next:
package.json which won't generate node_modules:
{
"name": "my-demo",
"version": "1.0.0",
"description": "a project",
"main": "index.js",
"scripts": {
"build": "weex-builder src dist",
"build_plugin": "webpack --config ./tools/webpack.config.plugin.js --color",
"dev": "weex-builder src dist -w",
"serve": "serve -p 8080"
},
"keywords": [
"weex"
],
"author": "xxx#gmail.com",
"license": "MIT"
}
It will results in:
Step 8/10 : RUN npm install .
---> Running in 45031bd21886
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN my-demo#1.0.0 No repository field.
up to date in 0.115s
Removing intermediate container 45031bd21886
---> f88364d0725d
Step 9/10 : RUN cp -R node_modules prod_node_modules
---> Running in 16cd11546db0
cp: can't stat 'node_modules': No such file or directory
The command '/bin/sh -c cp -R node_modules prod_node_modules' returned a non-zero code: 1

Resources