puppeteer/assets/js/dd3755ab.b846408d.js
release-please[bot] 581a4083f3 deploy: fd08e6ad22
2023-04-17 11:24:22 +00:00

1 line
7.9 KiB
JavaScript

"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([[24558],{93394:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>m,contentTitle:()=>u,default:()=>f,frontMatter:()=>l,metadata:()=>s,toc:()=>c});t(67294);var i=t(3905),a=t(93070),r=t(50551);function o(){return o=Object.assign||function(e){for(var n=1;n<arguments.length;n++){var t=arguments[n];for(var i in t)Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i])}return e},o.apply(this,arguments)}function p(e,n){if(null==e)return{};var t,i,a=function(e,n){if(null==e)return{};var t,i,a={},r=Object.keys(e);for(i=0;i<r.length;i++)t=r[i],n.indexOf(t)>=0||(a[t]=e[t]);return a}(e,n);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(i=0;i<r.length;i++)t=r[i],n.indexOf(t)>=0||Object.prototype.propertyIsEnumerable.call(e,t)&&(a[t]=e[t])}return a}const l={},u="Configuration",s={unversionedId:"guides/configuration",id:"version-19.9.1/guides/configuration",title:"Configuration",description:"All defaults in Puppeteer can be customized in two ways:",source:"@site/versioned_docs/version-19.9.1/guides/configuration.mdx",sourceDirName:"guides",slug:"/guides/configuration",permalink:"/guides/configuration",draft:!1,tags:[],version:"19.9.1",frontMatter:{},sidebar:"docs",previous:{title:"Guides",permalink:"/category/guides"},next:{title:"Query Selectors",permalink:"/guides/query-selectors"}},m={},c=[{value:"Configuration files",id:"configuration-files",level:2},{value:"Examples",id:"examples",level:3},{value:"Changing the default cache directory",id:"changing-the-default-cache-directory",level:4},{value:"Enabling experiments",id:"enabling-experiments",level:4},{value:"Environment variables",id:"environment-variables",level:2}],d={toc:c};function f(e){var{components:n}=e,t=p(e,["components"]);return(0,i.kt)("wrapper",o({},d,t,{components:n,mdxType:"MDXLayout"}),(0,i.kt)("h1",o({},{id:"configuration"}),"Configuration"),(0,i.kt)("p",null,"All defaults in Puppeteer can be customized in two ways:"),(0,i.kt)("ol",null,(0,i.kt)("li",{parentName:"ol"},(0,i.kt)("a",o({parentName:"li"},{href:"#configuration-files"}),"Configuration files")," (",(0,i.kt)("strong",{parentName:"li"},"recommended"),")"),(0,i.kt)("li",{parentName:"ol"},(0,i.kt)("a",o({parentName:"li"},{href:"#environment-variables"}),"Environment variables"))),(0,i.kt)("admonition",o({},{type:"caution"}),(0,i.kt)("p",{parentName:"admonition"},"Note that some options are only customizable through environment variables (such\nas ",(0,i.kt)("inlineCode",{parentName:"p"},"HTTPS_PROXY"),").")),(0,i.kt)("admonition",o({},{type:"caution"}),(0,i.kt)("p",{parentName:"admonition"},"Puppeteer's configuration files and environment variables are ignored by ",(0,i.kt)("inlineCode",{parentName:"p"},"puppeteer-core"),".")),(0,i.kt)("h2",o({},{id:"configuration-files"}),"Configuration files"),(0,i.kt)("p",null,"Configuration files are the ",(0,i.kt)("strong",{parentName:"p"},"recommended")," choice for configuring Puppeteer.\nPuppeteer will look up the file tree for any of the following formats:"),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},".puppeteerrc.cjs"),","),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},".puppeteerrc.js"),","),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},".puppeteerrc")," (YAML/JSON),"),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},".puppeteerrc.json"),","),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},".puppeteerrc.yaml"),","),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"puppeteer.config.js"),", and"),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"puppeteer.config.cjs"))),(0,i.kt)("p",null,"Puppeteer will also read a ",(0,i.kt)("inlineCode",{parentName:"p"},"puppeteer")," key from your application's\n",(0,i.kt)("inlineCode",{parentName:"p"},"package.json"),"."),(0,i.kt)("p",null,"See the ",(0,i.kt)("a",o({parentName:"p"},{href:"../api/puppeteer.configuration"}),(0,i.kt)("inlineCode",{parentName:"a"},"Configuration"))," interface for possible\noptions."),(0,i.kt)("admonition",o({},{type:"caution"}),(0,i.kt)("p",{parentName:"admonition"},"After adding a configuration file, you may need to remove and reinstall\n",(0,i.kt)("inlineCode",{parentName:"p"},"puppeteer")," for it to take effect if the changes affect installation.")),(0,i.kt)("h3",o({},{id:"examples"}),"Examples"),(0,i.kt)("h4",o({},{id:"changing-the-default-cache-directory"}),"Changing the default cache directory"),(0,i.kt)("p",null,"Starting in v19.0.0, Puppeteer stores browsers in ",(0,i.kt)("inlineCode",{parentName:"p"},"~/.cache/puppeteer")," to\nglobally cache browsers between installation. This can cause problems if\n",(0,i.kt)("inlineCode",{parentName:"p"},"puppeteer")," is packed during some build step and moved to a fresh location. The\nfollowing configuration can solve this issue (reinstall ",(0,i.kt)("inlineCode",{parentName:"p"},"puppeteer")," to take\neffect):"),(0,i.kt)("pre",null,(0,i.kt)("code",o({parentName:"pre"},{className:"language-js",metastring:'title="project-directory/.puppeteerrc.cjs"',title:'"project-directory/.puppeteerrc.cjs"'}),"const {join} = require('path');\n\n/**\n * @type {import(\"puppeteer\").Configuration}\n */\nmodule.exports = {\n // Changes the cache location for Puppeteer.\n cacheDirectory: join(__dirname, '.cache', 'puppeteer'),\n};\n")),(0,i.kt)("admonition",o({},{type:"note"}),(0,i.kt)("p",{parentName:"admonition"},"Notice this is only possible with CommonJS configuration files as information\nabout the ambient environment is needed (in this case, ",(0,i.kt)("inlineCode",{parentName:"p"},"__dirname"),").")),(0,i.kt)("h4",o({},{id:"enabling-experiments"}),"Enabling experiments"),(0,i.kt)("p",null,"By default, experiments are turned off, but they can be individually turned on\nusing the ",(0,i.kt)("a",o({parentName:"p"},{href:"../api/puppeteer.configuration"}),(0,i.kt)("inlineCode",{parentName:"a"},"experiments"))," key."),(0,i.kt)("p",null,"For example, if you want to enable ARM-native macOS chromium, you can use"),(0,i.kt)(a.Z,{mdxType:"Tabs"},(0,i.kt)(r.Z,{value:"CommonJS",mdxType:"TabItem"},(0,i.kt)("pre",null,(0,i.kt)("code",o({parentName:"pre"},{className:"language-js",metastring:'title=".puppeteerrc.cjs"',title:'".puppeteerrc.cjs"'}),'/**\n * @type {import("puppeteer").Configuration}\n */\nmodule.exports = {\n experiments: {\n macArmChromiumEnabled: true,\n },\n};\n'))),(0,i.kt)(r.Z,{value:"JSON",mdxType:"TabItem"},(0,i.kt)("pre",null,(0,i.kt)("code",o({parentName:"pre"},{className:"language-json",metastring:'title=".puppeteerrc.json"',title:'".puppeteerrc.json"'}),'{\n "experiments": {\n "macArmChromiumEnabled": true\n }\n}\n'))),(0,i.kt)(r.Z,{value:"YAML",mdxType:"TabItem"},(0,i.kt)("pre",null,(0,i.kt)("code",o({parentName:"pre"},{className:"language-yaml",metastring:'title=".puppeteerrc.yaml"',title:'".puppeteerrc.yaml"'}),"experiments:\n macArmChromiumEnabled: true\n")))),(0,i.kt)("h2",o({},{id:"environment-variables"}),"Environment variables"),(0,i.kt)("p",null,"Along with configuration files, Puppeteer looks for certain\n",(0,i.kt)("a",o({parentName:"p"},{href:"https://en.wikipedia.org/wiki/Environment_variable"}),"environment variables")," for\ncustomizing behavior. Environment variables will always override configuration\nfile options when applicable."),(0,i.kt)("p",null,"The following options are ",(0,i.kt)("em",{parentName:"p"},"environment-only")," options"),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"HTTP_PROXY"),", ",(0,i.kt)("inlineCode",{parentName:"li"},"HTTPS_PROXY"),", ",(0,i.kt)("inlineCode",{parentName:"li"},"NO_PROXY")," - defines HTTP proxy settings that are\nused to download and run the browser.")),(0,i.kt)("p",null,"All other options can be found in the documentation for the\n",(0,i.kt)("a",o({parentName:"p"},{href:"../api/puppeteer.configuration"}),(0,i.kt)("inlineCode",{parentName:"a"},"Configuration"))," interface."))}f.isMDXComponent=!0}}]);