puppeteer - how to set download location - node.js

I was able to successfully download a file with puppeteer, but it was just saving it to my /Downloads folder. I've been looking around and can't find anything in the api or forums to set this location.
My downloads are basically just go going to the link:
await page.goto(url);

Update for newer Puppeteer versions (~June 2022):
As mentioned by #Daniel here, you have to create the CDP session yourself:
const client = await page.target().createCDPSession()
await client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: './myAwesomeDownloadFolder',
})
Original Answer
This is how you can set the download path in latest puppeteer v0.13.
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './myAwesomeDownloadFolder'});
The behaviour is experimental, it might be removed, modified, or changed later.
Pst, you can try more tricks listed here, on your own risk :).

In newer versions of Puppeteer (I'm using v14.1), the Accepted Answer no longer works:
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './myAwesomeDownloadFolder'});
> TypeError: page._client.send is not a function
Instead, I had to explicitely create a new CDPSession:
const client = await page.target().createCDPSession()
await client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: './myAwesomeDownloadFolder',
})

I realize this is an old thread, but this thread popped up first for me when looking for how to set Puppeteer default download location.
I was able to set the download location using the following code,
let customChrome = path.resolve(__dirname, './customChrome')
let prefs = fs.readFileSync(customChrome+'/Default/Preferences');
let obj = JSON.parse(prefs);
obj.savefile.default_directory = path.resolve(__dirname, './downloads');
obj.download.default_directory = path.resolve(__dirname, './downloads');
fs.writeFileSync(customChrome+'/Default/Preferences', JSON.stringify(obj));
const browser = await puppeteer.launch({
userDataDir:customChrome,
headless: false,
args:['--disable-features=site-per-process','--no-sandbox']
});
This will set the default download directory for files before the process starts. Essentially, Puppeteer creates a custom profile each time it runs, we can override that profile and define the download directory.
The first time you run the above code, you will have to comment out the fs.readFile to fs.writeFile as the UserDirDirectory is created if it does not exist the first time that Chrome is started.
All profile related data is then stored in the customChrome/Default folder.
How to pass userDataDir profile folder to Puppeteer

All given solutions weren't working for me in the newer version of puppeteer 15.5.0
using puppeteer-extra with puppeteer-extra-plugin-user-preferences plugin did the trick.
// make sure puppeteer-extra & puppeteer-extra-plugin-user-preferences are installed
const UserPreferencesPlugin = require("puppeteer-extra-plugin-user-preferences");
const downloadImageDirectoryPath = process.cwd()
puppeteer.use(
UserPreferencesPlugin({
userPrefs: {
download: {
prompt_for_download: false,
open_pdf_in_system_reader: true,
default_directory: downloadImageDirectoryPath,
},
plugins: {
always_open_pdf_externally: true,
},
},
})
);

The answer from Muhammad Uzair solved my similar issue of setting the Chromium user preference to enforce PDF file downloads, but I ran into an issue of setting things up since I am using Puppeteer, Jest, and Jest-Puppeteer, where Jest-Puppeteer handles the initial setup behind the scenes.
This Github post from Macil helped with how to apply the puppeteer-extra-plugin-user-preferences plugin within the jest-puppeteer.config.js file.
For example, this is my jest-puppeteer.config.js file:
const puppeteer = require('puppeteer-extra');
const UserPreferencesPlugin = require('puppeteer-extra-plugin-user-preferences');
const userPreferenceOptions = {
userPrefs: {
plugins: {
always_open_pdf_externally: true,
},
download: {
open_pdf_in_system_reader: false,
prompt_for_download: false,
},
}
};
puppeteer.use(UserPreferencesPlugin(userPreferenceOptions));
require.cache[require.resolve('puppeteer')] = require.cache[require.resolve('puppeteer-extra')];
module.exports = {
launch: {
// https://github.com/puppeteer/puppeteer/blob/v13.3.2/docs/api.md#puppeteerlaunchoptions
headless: true, // opens a browser instance
slowMo: 25, // millis to slow each step
devtools: false, // auto opens the devtools in the browser
defaultViewport: {
width: 1820,
height: 980,
deviceScaleFactor: 1,
isMobile: false,
isLandscape: true,
},
product: "chrome", // can also specify firefox
browserContext: 'incognito',
args: [
// Chromium browser arguments: https://peter.sh/experiments/chromium-command-line-switches/
'--ignore-certificate-errors',
'--no-sandbox',
'--disable-setuid-sandbox',
'--window-size=1920,1080',
],
},
};

Finally, I run the program in docker container with chrome installed
FROM node:18-slim
# ------------------------------------------
# install extension
# ------------------------------------------
RUN apt-get update -y && apt-get install -y \
# chromium \
# libnss3 lsb-release xdg-utils wget \
wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
# ------------------------------------------
# set global config
# ------------------------------------------
ENV TZ Asia/Hong_Kong
# ------------------------------------------
# change the work directory
# ------------------------------------------
COPY source /root/source
WORKDIR /root/source
# ------------------------------------------
# upgrade npm
# ------------------------------------------
RUN npm install npm -g
RUN npm upgrade
# install node packages
RUN if test -e package-lock.json ; then npm ci ; else npm i ; fi
RUN npm run build
ENTRYPOINT ["npm", "run"]
CMD ["start"]
and
use await page.target().createCDPSession() as client for downloading the file
(
ref:
https://github.com/puppeteer/puppeteer/issues/1478#issuecomment-358826932
https://pptr.dev/api/puppeteer.cdpsession
)
const downloadPath = `[YOU OWN DOWNLOAD PATH]`
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
executablePath: '/usr/bin/google-chrome-stable'
})
const page = await browser.newPage()
const client = await page.target().createCDPSession()
await client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath
})

Update 30-07-2022:
An update has been made and this feature has been removed from previous versions of the package as well, if you have downloaded the package before 10-06-2022 it should work. For me it is like that, but it is puzzling why also previous versions were changed ..

Related

How can I connect to Memgraph database and executes queries using Rust?

I'm starting to learn Rust. I want to try out connecting to the Memgraph database and executing a query. I'm running a local instance of Memgraph Platform in Docker. I'm running it with default settings.
Since you are using Docker right after you create a new Rust project using cargo new memgraph_rust --bin add the following line to the Cargo.toml file under the line [dependencies] :
rsmgclient = "1.0.0"
Then, add the following code to the src/main.rs file:
use rsmgclient::{ConnectParams, Connection, SSLMode};
fn main(){
// Parameters for connecting to database.
let connect_params = ConnectParams {
host: Some(String::from("172.17.0.2")),
sslmode: SSLMode::Disable,
..Default::default()
};
// Make a connection to the database.
let mut connection = match Connection::connect(&connect_params) {
Ok(c) => c,
Err(err) => panic!("{}", err)
};
// Execute a query.
let query = "CREATE (u:User {name: 'Alice'})-[:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, m";
match connection.execute(query, None) {
Ok(columns) => println!("Columns: {}", columns.join(", ")),
Err(err) => panic!("{}", err)
};
// Fetch all query results.
match connection.fetchall() {
Ok(records) => {
for value in &records[0].values {
println!("{}", value);
}
},
Err(err) => panic!("{}", err)
};
// Commit any pending transaction to the database.
match connection.commit() {
Ok(()) => {},
Err(err) => panic!("{}", err)
};
}
Now, create a new file in the project root directory /memgraph_rust and name it Dockerfile:
# Set base image (host OS)
FROM rust:1.56
# Install CMake
RUN apt-get update && \
apt-get --yes install cmake
# Install mgclient
RUN apt-get install -y git cmake make gcc g++ libssl-dev clang && \
git clone https://github.com/memgraph/mgclient.git /mgclient && \
cd mgclient && \
git checkout 5ae69ea4774e9b525a2be0c9fc25fb83490f13bb && \
mkdir build && \
cd build && \
cmake .. && \
make && \
make install
# Set the working directory in the container
WORKDIR /code
# Copy the dependencies file to the working directory
COPY Cargo.toml .
# Copy the content of the local src directory to the working directory
RUN mkdir src
COPY src/ ./src
# Generate binary using the Rust compiler
RUN cargo build
# Command to run on container start
CMD [ "cargo", "run" ]
All that is now left is to get the address docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_ID, create and image docker build -t memgraph_rust . and starting the application with docker run memgraph_rust.
If you ever decide to take your Rust program to an environment that doesn't have Docker you will maybe need to install rsmgclient driver
The complete documentation for connecting using Rust can be found at Rust quick start guide on the Memgraph site.

Error: Jest: Got error running globalSetup - /home/pptruser/app/node_modules/jest-environment-puppeteer/setup.js

When running from local, its getting passed. Below error is thrown when run from docker locally. I am in the process of setting up my code for my puppeteer test.
I also included here below package.json, jest-puppeteer.config, jest.config files. Here I haven't included my tests files.
shall you please someone help ? Thanks.
Error: Jest: Got error running globalSetup - /home/pptruser/app/node_modules/jest-environment-puppeteer/setup.js, reason: Could not find expected browser (chrome) locally. Run `npm install` to download the correct Chromium revision (1022525).
at ChromeLauncher.launch (/home/pptruser/app/node_modules/puppeteer/lib/cjs/puppeteer/node/ChromeLauncher.js:70:23)
at async Promise.all (index 0)
at async setup (/home/pptruser/app/node_modules/jest-environment-puppeteer/lib/global.js:37:16)
at async /home/pptruser/app/node_modules/#jest/core/build/runGlobalHook.js:125:13
at async waitForPromiseWithCleanup (/home/pptruser/app/node_modules/#jest/transform/build/ScriptTransformer.js:209:5)
at async runGlobalHook (/home/pptruser/app/node_modules/#jest/core/build/runGlobalHook.js:116:9)
at async runJest (/home/pptruser/app/node_modules/#jest/core/build/runJest.js:369:5)
at async _run10000 (/home/pptruser/app/node_modules/#jest/core/build/cli/index.js:320:7)
at async runCLI (/home/pptruser/app/node_modules/#jest/core/build/cli/index.js:173:3)
at async Object.run (/home/pptruser/app/node_modules/jest-cli/build/cli/index.js:155:37)
jest.config.js:
module.exports = {
preset: "jest-puppeteer"
notifyMode: "always",
maxWorkers: "50%",
maxConcurrency: 150,
maxWorkers: 1,
bail: 1,
collectCoverage: true,
testRunner: "jest-jasmine2",
timers: "fake",
testTimeout: 9000000,
watchman: false,
};
package.json:
"devDependencies": {
"#babel/preset-env": "^7.18.2",
"babel-jest": "^27.0.6",
"dotenv": "^16.0.1",
"jest": "^27.5.1",
"jest-cli": "^27.5.1",
"jest-jasmine2": "^27.2.3",
"jest-puppeteer": "^6.1.0",
"prettier": "2.5.1",
"puppeteer": "^14.1.2"
}
jest-puppeteer.config:
module.exports = {
launch: {
headless: true,
slowMo: 0,
defaultViewport: null,
args: ["--window-size=1920,1080",
"--incognito",
"--start-maximized",
"--disable-extensions",
"--no-sandbox",
"--disable-setuid-sandbox",
"--no-first-run",
"--no-zygote"],
setDefaultNavigationTimeout: 8000000,
setDefaultTimeout: 8000000,
},
browserContext: "default",
};
My docker file:
FROM docker-remote.artifactory.oci.oraclecorp.com/oraclelinux:7-slim
COPY --from=odo-docker-signed-local.artifactory.oci.oraclecorp.com/odo/base-image-support:ol7x-1.6 / /
RUN yum-config-manager --add-repo https://artifactory.oci.oraclecorp.com/io-ol7-nodejs16-yum-local/ \
--add-repo https://artifactory.oci.oraclecorp.com/io-ol7-oracle-instant-client-yum-local/ \
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser \
ORACLE_NPM=https://artifactory.oci.oraclecorp.com/api/npm/npm-remote/
RUN yum -y update \
&& yum -y install nodejs-16.14.2-1.0.1.el7.x86_64 chromium-102.0.5005.115-1.el7.x86_64
RUN groupadd -r pptruser \
&& useradd -r -g pptruser -G audio,video pptruser \
&& mkdir -p /home/pptruser/Downloads \
&& mkdir -p /home/pptruser/app \
&& chown -R pptruser:pptruser /home/pptruser
WORKDIR /home/pptruser/app
COPY --chown=pptruser:pptruser . .
CMD ["npm","run-script smoke"]
The way we solved this was to download Chromium https://download-chromium.appspot.com/ and unzip into a specific folder: .\node_modules\puppeteer.local-chromium\win64-722234
Some other answers include specifying the path to your chromium like so:
const launchOptions = {
// other options (headless, args, etc)
executablePath: '/home/jack/repos/my-repo/node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux/chrome'
}

Could not find expected browser chrome locally

This Meteor code uses "puppeteer 8.0.0", "puppeteer-core 10.0.0", puppeteer-extra 3.1.18" and "puppeteer-extra-plugin-stealth 2.7.8", It gives this error:
Error: Could not find expected browser (chrome) locally. Run npm install to download the correct Chromium revision (884014).
Tried "npm install" for no avail. Reading up online, tried removing "puppeteer-core": "^10.0.0" from package.json dependencies for no avail.
Any help is much appriciated. Thanks
const puppeteer = require('puppeteer-extra');
const nameH = require('./NameH');
const puppeteerOptions = {
headless: true,
ignoreHTTPSErrors: true,
args: ['--no-sandbox', '--single-process', '--no-zygote', '--disable-setuid-sandbox']
}
let browser;
let pageNameH;
const init = async () => {
const StealthPlugin = require('puppeteer-extra-plugin-stealth');
console.log('1') //>>>>>>>>>>>> Prints 1
puppeteer.use(StealthPlugin());
console.log('2') //>>>>>>>>>>>> Prints 2
browser = await puppeteer.launch(puppeteerOptions);
console.log('3') //>>>>>>>>> DID NOT PRINT <<<<<<<<<<<<<<<
pageNameH = await browser.newPage();
console.log('4')
await pageNameH.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36');
await pageNameH.setViewport({ width: 1366, height: 768 });
await pageNameH.setRequestInterception(true);
blockResources(pageNameH);
}
const blockResources = page => {
page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort();
}
else {
req.continue();
}
});
}
export const abc = async (nm, loc) => {
try {
console.log('name try') //>>>>>>>>>>>> Prints "name try"
if (!(browser && pageNameH))
await init();
//use "required" nameh here
} catch (error) { // print the error <<<<<<<<<<<<<<<<<<<<<<<<<
console.log("Could not launch Puppeteer or open a new page.\n" + error);
if (browser && browser.close === 'function') await browser.close();
}
}
// included in package.json
"dependencies": {
"#babel/runtime": "^7.11.2",
"axios": "^0.21.1",
"check": "^1.0.0",
"cheerio": "^1.0.0-rc.6",
"jquery": "^3.5.1",
"meteor-node-stubs": "^1.0.1",
"nightmare": "^3.0.2",
"pending-xhr-puppeteer": "^2.3.3",
"puppeteer": "^8.0.0",
"puppeteer-core": "^10.0.0",
"puppeteer-extra": "^3.1.18",
"puppeteer-extra-plugin-adblocker": "^2.11.11",
"puppeteer-extra-plugin-block-resources": "^2.2.9",
"puppeteer-extra-plugin-stealth": "^2.7.8"
},
may be you can try this, it works for me on linux(centOS), puppeteer(10.2.0).
cd ./node_modules/puppeteer
npm run install
If that fails, you can also try running:
cd ./node_modules/puppeteer
npm install
This will download the chromium to ./node_modules/puppeteer/.local-chromium
I had the same problem. I checked my env variables, and even though PUPPETEER_SKIP_CHROMIUM_DOWNLOAD was set to false, it was still not working. After I removed the variable (unset PUPPETEER_SKIP_CHROMIUM_DOWNLOAD for mac), it worked.
related dependancies:
"dependencies": {
"chrome-aws-lambda": "^10.0.0",
"puppeteer-core": "^10.0.0",
},
"devDependencies": {
"puppeteer": "^10.0.0",
}
Launching Chromium:
import chromium from "chrome-aws-lambda";
const browser = await chromium.puppeteer.launch({
executablePath: await chromium.executablePath,
});
Fixed it by running this command to install chromium manually.
node node_modules/puppeteer/install.js
if you are testing locally, make sure you have puppeteer installed as dev dependencies. specifically
npm install puppeteer --save-dev
https://github.com/alixaxel/chrome-aws-lambda/wiki/HOWTO:-Local-Development#workaround
this approach allows us to rely on puppeteer for local development and puppeteer-core for production deployments.
I had the same issue. What worked for me was to specify as the executablePath Puppeteer launch option the fullpath to the local chromium used by Puppeteer.
Something like this:
const launchOptions = {
// other options (headless, args, etc)
executablePath: '/home/jack/repos/my-repo/node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux/chrome'
}
As noted in another answer, it seems that also referencing a chromium local binary would work, but I think it's a worse solution, since Puppeteer is guaranteed to work only with the local bundled version of Chromium.
Throwing my answer in, in hopes that it helps someone not waste their entire evening like I did.
I was writing a Typescript server that used Puppeteer, and I'm using ESBuild to transpile from TS to JS. In the build step, esbuild was trying to bundle everything into one file, but I had to instruct it to preserve the Puppeteer import from node_modules.
I did this by marking puppeteer as external. See docs here.
Since npm i downloads a compatible version of chromium to the node_modules folder, once I preserved this import, it was able to find Chromium in the node_modules folder.
My build file looks like:
require("esbuild").buildSync({
entryPoints: ["src/index.ts"],
outdir: "build",
bundle: true,
platform: "node",
target: "node16",
external: ["puppeteer"],
});
And I run it with node prod-build.js.
Now in my code, I can just call launch!
const browser = await puppeteer.launch()
I used the installed version on my pc (maybe not what you're looking for)
const browser = await puppeteer.launch({headless:false, executablePath:
'C:/Program Files/.../chrome.exe' });
You may need to install some dependencies depending on your OS. Check Puppeteer's Troubleshooting page for more details.
I was having this error too and I noticed that I started getting such an error after updating my node from my Dockerfile to version ^16 (my puppeteer is in a container). How did I solve it? I downgraded my node from Dockerfile to version 12.
Hope this resolves it...
OBS: I use Ubuntu 21.04 in machine.
EDIT ------
In newest versions of node, you need to go in ./node_modules/puppeteer and run command npm install, then the correct packages will be installed.
I'm using that solution.
If you are using puppeteer in AWS SAM and you don't have puppeteer in dependencies, you can install the same in puppeteer-core using
node node_modules/puppeteer/install.js
For this setup to work you will have to add chrome-aws-lambda in the devDependencies.
"devDependencies": {
"chrome-aws-lambda": "^10.1.0"
}
Also before you take it to production don't forget to add the layer in your template.yaml file:
Layers:
- !Sub 'arn:aws:lambda:ap-south-1:764866452798:layer:chrome-aws-lambda:25'

net::ERR_ADDRESS_UNREACHABLE at {URL}

i am using puppeteer v1.19.0 in nodejs , is error unreachable after build and run in docker,
is js file
await puppeteer.launch({
executablePath: '/usr/bin/chromium-browser',
args: ['--no-sandbox', '--disable-setuid-sandbox', '--headless'],
}).then(async (browser) => {
const url = `${thisUrl}analisa-jabatan/pdf/${_id}`
const page = await browser.newPage()
await page.goto(url, { waitUntil: 'networkidle0' })
// await page.evaluate(() => { window.scrollBy(0, window.innerHeight) })
await page.setViewport({
width: 1123,
height: 794,
})
setTimeout(async () => {
const buffer = await page.pdf({
path: `uploads/analisa-jabatan.pdf`,
displayHeaderFooter: true,
headerTemplate: '',
footerTemplate: '',
printBackground: true,
format: 'A4',
landscape: true,
margin: {
top: 20,
bottom: 20,
left: 20,
right: 20,
},
})
let base64data = buffer.toString('base64')
await res.status(200).send(base64data)
// await res.download(process.cwd() + '/uploads/analisa-jabatan.pdf')
await browser.close()
}, 2000)
})
}
and is dockerfile
FROM aria/alpine-nodejs:3.10
#FROM node:12-alpine
LABEL maintainer="Aria <aryamuktadir22#gmail.com>"
# ENVIRONMENT VARIABLES
# NODE_ENV
ENV NODE_ENV=production
# SERVER Configuration
ENV HOST=0.0.0.0
ENV PORT=3001
ENV SESSION_SECRET=thisissecret
# CORS Configuration
ENV CORS_ORIGIN=http://117.54.250.109:8081
ENV CORS_METHOD=GET,POST,PUT,DELETE,PATCH,OPTIONS,HEAD
ENV CORS_ALLOWED_HEADERS=Authorization,Content-Type,Access-Control-Request-Method,X-Requested-With
ENV CORS_MAX_AGE=600
ENV CORS_CREDENTIALS=false
# DATABASE Configuration
ENV DB_HOST=anjabdb
ENV DB_PORT=27017
ENV DB_NAME=anjab
# Tell Puppeteer to skip installing Chrome. We'll be using the installed package.
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
# SET WORKDIR
WORKDIR /usr/local/app
# INSTALL REQUIRED DEPENDENCIES
RUN apk update && apk upgrade && \
apk add --update --no-cache \
gcc g++ make autoconf automake pngquant \
python2 \
chromium \
udev \
nss \
freetype \
freetype-dev \
harfbuzz \
ca-certificates \
ttf-freefont ca-certificates \
nodejs \
yarn \
libpng libpng-dev lcms2 lcms2-dev
# COPY SOURCE TO CONTAINER
ADD deploy/etc/ /etc
ADD package.json app.js server.js process.yml ./
ADD lib ./lib
ADD middlewares ./middlewares
ADD models ./models
ADD modules ./modules
ADD uploads ./uploads
ADD assets ./assets
ADD views ./views
COPY keycloak.js.prod ./keycloak.js
# INSTALL NODE DEPENDENCIES
RUN npm cache clean --force
RUN npm config set unsafe-perm true
RUN npm -g install pm2 phantomjs html-pdf
RUN yarn && yarn install --production=true && sleep 3 &&\
yarn cache clean
RUN set -ex \
&& apk add --no-cache --virtual .build-deps ca-certificates openssl \
&& wget -qO- "https://github.com/dustinblackman/phantomized/releases/download/2.1.1/dockerized-phantomjs.tar.gz" | tar xz -C / \
&& npm install -g phantomjs \
&& apk del .build-deps
EXPOSE 3001
And is result
Error: net::ERR_ADDRESS_UNREACHABLE at http://117.54.250.109:8089/analisa-jabatan/pdf/5ee9e6a15ff81d00c7c3a614
at navigate (/usr/local/app/node_modules/puppeteer/lib/FrameManager.js:120:37)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame. (/usr/local/app/node_modules/puppeteer/lib/helper.js:111:15)
at Page.goto (/usr/local/app/node_modules/puppeteer/lib/Page.js:674:49)
at Page. (/usr/local/app/node_modules/puppeteer/lib/helper.js:112:23)
at puppeteer.launch.then (/usr/local/app/modules/analisajabatan/methods/pdfpuppeteer.js:60:20)
at process._tickCallback (internal/process/next_tick.js:68:7)
From the puppeteer docs:
page.goto will not throw an error when any valid HTTP status code is returned by the remote server, including 404 "Not Found" and 500 "Internal Server Error"
so providing the url is valid, the server doesn't seem to be sending a response.

How to run NodeJS puppeteer with Chromium v77 in Docker?

Alpine supports Chromium v77 on 8Oct.
Reference: https://pkgs.alpinelinux.org/packages?name=chromium&branch=edge
Tried to copy steps to download Chromium v77 and run Puppeteer v1.20 but with error when running it:
Error for printPdf()
{}
Error: Failed to launch chrome!
Error relocating /usr/lib/chromium/chrome: _ZNSt7__cxx1118basic_stringstreamIcSt11char_traitsIcESaIcEEC1Ev: symbol not found
Error relocating /usr/lib/chromium/chrome: _ZNSt7__cxx1119basic_ostringstreamIcSt11char_traitsIcESaIcEEC1Ev: symbol not found
Error relocating /usr/lib/chromium/chrome: hb_subset_input_set_retain_gids: symbol not found
Error relocating /usr/lib/chromium/chrome: _ZNSt19_Sp_make_shared_tag5_S_eqERKSt9type_info: symbol not found
TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
at onClose (/usr/src/app/node_modules/puppeteer/lib/Launcher.js:348:14)
at Interface.<anonymous> (/usr/src/app/node_modules/puppeteer/lib/Launcher.js:337:50)
at Interface.emit (events.js:214:15)
at Interface.close (readline.js:403:8)
at Socket.onend (readline.js:180:10)
at Socket.emit (events.js:214:15)
at endReadableNT (_stream_readable.js:1178:12)
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Dockerfile:
FROM node:12-alpine
ENV CHROME_BIN="/usr/bin/chromium-browser"\
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true"
RUN set -x \
&& apk update \
&& apk upgrade \
&& echo "127.0.0.1 localhost" >> /etc/hosts \
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" > /etc/apk/repositories \
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/community" >> /etc/apk/repositories \
&& echo "http://dl-cdn.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories \
&& apk add --no-cache g++ chromium \
&& npm install puppeteer#1.20.0 puppeteer-core#1.20.0
...
I think the issue is with your installation. You can try this as a base image.
FROM zenika/alpine-chrome:77-with-node
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true
RUN npm install puppeteer#1.20.0 puppeteer-core#1.20.0
COPY my_script.js /usr/src/app/
CMD ["node","my_script.js"]
my_script.js testing code
const puppeteer = require('puppeteer');
(async () => {
const browser =await puppeteer.launch({
executablePath: '/usr/bin/chromium-browser',
args: ['--no-sandbox', '--headless', '--disable-gpu']
});
const page = await browser.newPage();
await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
await page.pdf({path: 'hn.pdf', format: 'A4'});
await browser.close();
})();
If you want to build from sractch then you can use this Dockerfile and modify as per your need.

Resources