[{"data":1,"prerenderedAt":409},["ShallowReactive",2],{"navigation_docs":3,"rule-content-\u002Fpackage\u002Frules\u002Fdependencies\u002Foptional-peer-import-in-try":48},[4,8,12,16,20,24,28,32,36,40,44],{"title":5,"path":6,"stem":7},"CLI","\u002Fcli","1.cli",{"title":9,"path":10,"stem":11},"Nuxt","\u002Fnuxt","2.nuxt",{"title":13,"path":14,"stem":15},"Vue","\u002Fvue","3.vue",{"title":17,"path":18,"stem":19},"Vite","\u002Fvite","4.vite",{"title":21,"path":22,"stem":23},"Nitro","\u002Fnitro","5.nitro",{"title":25,"path":26,"stem":27},"TypeScript","\u002Ftypescript","6.typescript",{"title":29,"path":30,"stem":31},"shadcn","\u002Fshadcn","7.shadcn",{"title":33,"path":34,"stem":35},"Package","\u002Fpackage","8.package",{"title":37,"path":38,"stem":39},"Extending Doctor","\u002Fextending","9.extending",{"title":41,"path":42,"stem":43},"MCP","\u002Fmcp","9.mcp",{"title":45,"path":46,"stem":47},"Pinia","\u002Fpinia","9.pinia",{"id":49,"title":50,"body":51,"category":57,"description":396,"diagnosticCodes":397,"docsUrl":59,"extension":398,"fix":60,"framework":399,"meta":400,"navigation":401,"pack":61,"path":403,"recommendedReplacement":404,"ruleId":62,"seo":405,"severity":63,"source":64,"sourceUrl":65,"stem":406,"why":407,"__hash__":408},"rules\u002Fpackage\u002Frules\u002Fdependencies\u002Foptional-peer-import-in-try.md","Guard import() of optional packages with try or .catch()",{"type":52,"value":53,"toc":387},"minimark",[54,66,71,109,113,132,136,148,152,164,168,173,176,250,253,383],[55,56],"rule-metadata",{"category":57,"diagnosticCodes":58,"docsUrl":59,"fix":60,"pack":61,"ruleId":62,"severity":63,"source":64,"sourceUrl":65},"dependencies","PKG0004","https:\u002F\u002Fesbuild.github.io\u002Fapi\u002F#non-analyzable-imports","suggestion","vite-doctor\u002Fpackage","package\u002Foptional-peer-import-in-try","warn","src\u002Frule-packs\u002Fpackage\u002Frules\u002Foptional-peer-import-in-try.ts","https:\u002F\u002Fgithub.com\u002Fonmax\u002Fvite-doctor\u002Fblob\u002Fmain\u002Fsrc\u002Frule-packs\u002Fpackage\u002Frules\u002Foptional-peer-import-in-try.ts",[67,68,70],"h2",{"id":69},"run-this-rule","Run this rule",[72,73,78],"pre",{"className":74,"code":75,"language":76,"meta":77,"style":77},"language-bash shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","pnpm vite-doctor . --extends package\u002Frecommended --rules package\u002Foptional-peer-import-in-try\n","bash","",[79,80,81],"code",{"__ignoreMap":77},[82,83,86,90,94,97,100,103,106],"span",{"class":84,"line":85},"line",1,[82,87,89],{"class":88},"sBMFI","pnpm",[82,91,93],{"class":92},"sfazB"," vite-doctor",[82,95,96],{"class":92}," .",[82,98,99],{"class":92}," --extends",[82,101,102],{"class":92}," package\u002Frecommended",[82,104,105],{"class":92}," --rules",[82,107,108],{"class":92}," package\u002Foptional-peer-import-in-try\n",[67,110,112],{"id":111},"why-it-matters","Why it matters",[114,115,116,117,120,121,123,124,127,128,131],"p",{},"esbuild, and the Wrangler, Netlify, and Vite builds that use it, fails the consumer's build when an ",[79,118,119],{},"import()"," names a package that is not installed. It keeps the import for run time only when the ",[79,122,119],{}," is directly awaited in a try body of the same function or has a .",[79,125,126],{},"catch()"," or two-argument .",[79,129,130],{},"then()",". A try in a caller, or a loader callback passed to a helper that catches, does not count.",[67,133,135],{"id":134},"recommended-fix","Recommended fix",[114,137,138,139,141,142,144,145,147],{},"Directly await the ",[79,140,119],{}," of the optional package inside a try in the function that calls it, and turn a missing package into an error that names the install command. A .",[79,143,126],{}," on the ",[79,146,119],{}," also works.",[67,149,151],{"id":150},"useful-links","Useful links",[153,154,155],"ul",{},[156,157,158],"li",{},[159,160,163],"a",{"href":59,"rel":161},[162],"nofollow","Rule reference",[67,165,167],{"id":166},"example","Example",[169,170,172],"h3",{"id":171},"keep-the-import-of-an-optional-peer-inside-the-try","Keep the import() of an optional peer inside the try",[114,174,175],{},"Before",[72,177,181],{"className":178,"code":179,"language":180,"meta":77,"style":77},"language-js shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","export const loadForge = () => importOptional(() => import(\"forge\"), \"forge\");\n","js",[79,182,183],{"__ignoreMap":77},[82,184,185,189,193,197,201,204,207,211,214,217,219,222,224,227,230,232,235,238,241,243,245,247],{"class":84,"line":85},[82,186,188],{"class":187},"s7zQu","export",[82,190,192],{"class":191},"spNyl"," const",[82,194,196],{"class":195},"sTEyZ"," loadForge ",[82,198,200],{"class":199},"sMK4o","=",[82,202,203],{"class":199}," ()",[82,205,206],{"class":191}," =>",[82,208,210],{"class":209},"s2Zo4"," importOptional",[82,212,213],{"class":195},"(",[82,215,216],{"class":199},"()",[82,218,206],{"class":191},[82,220,221],{"class":199}," import",[82,223,213],{"class":195},[82,225,226],{"class":199},"\"",[82,228,229],{"class":92},"forge",[82,231,226],{"class":199},[82,233,234],{"class":195},")",[82,236,237],{"class":199},",",[82,239,240],{"class":199}," \"",[82,242,229],{"class":92},[82,244,226],{"class":199},[82,246,234],{"class":195},[82,248,249],{"class":199},";\n",[114,251,252],{},"After",[72,254,256],{"className":178,"code":255,"language":180,"meta":77,"style":77},"export async function loadForge() {\n  try {\n    return await import(\"forge\");\n  } catch (error) {\n    throw new Error(\"Install forge to use this integration\", { cause: error });\n  }\n}\n",[79,257,258,276,284,308,329,371,377],{"__ignoreMap":77},[82,259,260,262,265,268,271,273],{"class":84,"line":85},[82,261,188],{"class":187},[82,263,264],{"class":191}," async",[82,266,267],{"class":191}," function",[82,269,270],{"class":209}," loadForge",[82,272,216],{"class":199},[82,274,275],{"class":199}," {\n",[82,277,279,282],{"class":84,"line":278},2,[82,280,281],{"class":187},"  try",[82,283,275],{"class":199},[82,285,287,290,293,295,298,300,302,304,306],{"class":84,"line":286},3,[82,288,289],{"class":187},"    return",[82,291,292],{"class":187}," await",[82,294,221],{"class":199},[82,296,213],{"class":297},"swJcz",[82,299,226],{"class":199},[82,301,229],{"class":92},[82,303,226],{"class":199},[82,305,234],{"class":297},[82,307,249],{"class":199},[82,309,311,314,317,320,323,326],{"class":84,"line":310},4,[82,312,313],{"class":199},"  }",[82,315,316],{"class":187}," catch",[82,318,319],{"class":297}," (",[82,321,322],{"class":195},"error",[82,324,325],{"class":297},") ",[82,327,328],{"class":199},"{\n",[82,330,332,335,338,341,343,345,348,350,352,355,358,361,364,367,369],{"class":84,"line":331},5,[82,333,334],{"class":187},"    throw",[82,336,337],{"class":199}," new",[82,339,340],{"class":209}," Error",[82,342,213],{"class":297},[82,344,226],{"class":199},[82,346,347],{"class":92},"Install forge to use this integration",[82,349,226],{"class":199},[82,351,237],{"class":199},[82,353,354],{"class":199}," {",[82,356,357],{"class":297}," cause",[82,359,360],{"class":199},":",[82,362,363],{"class":195}," error",[82,365,366],{"class":199}," }",[82,368,234],{"class":297},[82,370,249],{"class":199},[82,372,374],{"class":84,"line":373},6,[82,375,376],{"class":199},"  }\n",[82,378,380],{"class":84,"line":379},7,[82,381,382],{"class":199},"}\n",[384,385,386],"style",{},"html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}",{"title":77,"searchDepth":278,"depth":278,"links":388},[389,390,391,392,393],{"id":69,"depth":278,"text":70},{"id":111,"depth":278,"text":112},{"id":134,"depth":278,"text":135},{"id":150,"depth":278,"text":151},{"id":166,"depth":278,"text":167,"children":394},[395],{"id":171,"depth":286,"text":172},"Find import() calls of optional peers and optional dependencies that no try block in the same function and no .catch() handles, in runtime JavaScript reachable from a package export.",[58],"md","package",{},{"title":402},"Optional import()","\u002Fpackage\u002Frules\u002Fdependencies\u002Foptional-peer-import-in-try","Directly await the import() of the optional package inside a try in the function that calls it, and turn a missing package into an error that names the install command. A .catch() on the import() also works.",{"title":50,"description":396},"package\u002Frules\u002Fdependencies\u002Foptional-peer-import-in-try","esbuild, and the Wrangler, Netlify, and Vite builds that use it, fails the consumer's build when an import() names a package that is not installed. It keeps the import for run time only when the import() is directly awaited in a try body of the same function or has a .catch() or two-argument .then(). A try in a caller, or a loader callback passed to a helper that catches, does not count.","2re4vF--RJwRbVvLAufTw8Vr6CpoDXWLtHDBAhoj1uM",1795268786202]