I am still new to Docker so I am wondering how to execute the simplest playwright JS with node as the dockerfile provided in https://github.com/microsoft/playwright/blob/master/docs/docker/Dockerfile.bionic does not npm install playwright globally.
I ran this script:
const playwright = require('playwright');
(async () => {
for (const browserType of ['chromium', 'firefox', 'webkit']) {
const browser = await playwright[browserType].launch({headless: true, args: ['--no-sandbox']});
const context = await browser.newContext();
const page = await context.newPage();
await page.goto('http://whatsmyuseragent.org/');
await page.screenshot({ path:example-${browserType}.png});
await browser.close();
}
})();
but I get error Error: Cannot find module 'playwright'
Any help is much appreciated fellow coders.
As you pointed out the Docker image does not include Playwright itself, so you'll have to install it separately, e.g. when running the Docker container you can do something like this:
npm init -y
npm install playwright --save
And then run your script from the same directory.
Alternatively you could bake Playwright into your docker image, e.g. by adding the following line to Docker.bionic:
RUN mkdir -p /home/pwuser/example && cd /home/pwuser/example && npm init -y && install playwright --save
thanks Yury. I got it working.
Most helpful comment
As you pointed out the Docker image does not include Playwright itself, so you'll have to install it separately, e.g. when running the Docker container you can do something like this:
And then run your script from the same directory.
Alternatively you could bake Playwright into your docker image, e.g. by adding the following line to Docker.bionic: