1"use strict";(self.webpackChunkwebsite=self.webpackChunkwebsite||[]).push([["273"],{28844(e,r,t){t.r(r),t.d(r,{metadata:()=>n,default:()=>h,frontMatter:()=>a,contentTitle:()=>c,toc:()=>d,assets:()=>u});var n=JSON.parse('{"id":"no-var-requires","title":"no-var-requires","description":"Disallow `require` statements except in import statements.","source":"@site/../eslint-plugin/docs/rules/no-var-requires.mdx","sourceDirName":".","slug":"/no-var-requires","permalink":"/rules/no-var-requires","draft":false,"unlisted":false,"editUrl":"https://github.com/typescript-eslint/typescript-eslint/edit/main/packages/website/../eslint-plugin/docs/rules/no-var-requires.mdx","tags":[],"version":"current","frontMatter":{"description":"Disallow `require` statements except in import statements.","image":"/img/og/rules/no-var-requires.png"},"sidebar":"rulesSidebar","previous":{"title":"no-useless-empty-export","permalink":"/rules/no-useless-empty-export"},"next":{"title":"no-wrapper-object-types","permalink":"/rules/no-wrapper-object-types"}}'),s=t(65723),o=t(7143),i=t(70503),l=t(46742);let a={description:"Disallow `require` statements except in import statements.",image:"/img/og/rules/no-var-requires.png"},c,u={},d=[{value:"Examples",id:"examples",level:2},{value:"Options",id:"options",level:2},{value:"<code>allow</code>",id:"allow",level:3},{value:"When Not To Use It",id:"when-not-to-use-it",level:2},{value:"Related To",id:"related-to",level:2},{value:"Resources",id:"resources",level:2}];function p(e){let r={a:"a",admonition:"admonition",blockquote:"blockquote",code:"code",h2:"h2",h3:"h3",li:"li",meta:"meta",p:"p",pre:"pre",ul:"ul",...(0,o.R)(),...e.components},{Head:t,RuleAttributes:n,TryInPlayground:a}=r;return t||m("Head",!0),n||m("RuleAttributes",!0),a||m("TryInPlayground",!0),(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(s.Fragment,{children:(0,s.jsxs)(r.blockquote,{children:["\n",(0,s.jsxs)(r.p,{children:["Disallow ",(0,s.jsx)(r.code,{children:"require"})," statements except in import statements."]}),"\n"]})}),"\n",(0,s.jsx)(n,{name:"no-var-requires"}),"\n","\n",(0,s.jsx)(r.admonition,{title:"Deprecated",type:"danger",children:(0,s.jsxs)(r.p,{children:["This rule has been deprecated in favour of the ",(0,s.jsx)(r.a,{href:"/rules/no-require-imports",children:(0,s.jsx)(r.code,{children:"@typescript-eslint/no-require-imports"})})," rule."]})}),"\n",(0,s.jsxs)(r.p,{children:["In other words, the use of forms such as ",(0,s.jsx)(r.code,{children:'var foo = require("foo")'})," are banned. Instead use ES6 style imports or ",(0,s.jsx)(r.code,{children:'import foo = require("foo")'})," imports."]}),"\n",(0,s.jsxs)(i.A,{children:[(0,s.jsx)(l.A,{value:"Flat Config",children:(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-js",metastring:'title="eslint.config.mjs"',children:'export default defineConfig({\n rules: {\n "@typescript-eslint/no-var-requires": "error"\n }\n});\n'})})}),(0,s.jsx)(l.A,{value:"Legacy Config",children:(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-js",metastring:'title=".eslintrc.cjs"',children:'module.exports = {\n "rules": {\n "@typescript-eslint/no-var-requires": "error"\n }\n};\n'})})})]}),"\n",(0,s.jsx)(a,{eslintrcHash:"N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oDcBDaWtEQBHWJSGoMkRNGgdokcGAC+IZUA",children:(0,s.jsx)(r.p,{children:"Try this rule in the playground \u2197"})}),"\n",(0,s.jsx)(r.h2,{id:"examples",children:"Examples"}),"\n",(0,s.jsxs)(i.A,{children:[(0,s.jsx)(l.A,{value:"\u274C Incorrect",children:(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-ts",metastring:'eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oDcBDaWtEQBHWJSGoMkRNGgdokcGAC+IZUA"',children:"var foo = require('foo');\nconst foo = require('foo');\nlet foo = require('foo');\n"})})}),(0,s.jsx)(l.A,{value:"\u2705 Correct",children:(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-ts",metastring:'eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oDcBDaWtEQBHWJSGoMkRNGgdokcGAC+IZUA"',children:"import foo = require('foo');\nrequire('foo');\nimport foo from 'foo';\n"})})})]}),"\n",(0,s.jsx)(r.h2,{id:"options",children:"Options"}),"\n",(0,s.jsx)(s.Fragment,{children:(0,s.jsx)(r.p,{children:"This rule accepts the following options:"})}),"\n",(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-ts",children:"type Options = [\n {\n /** Patterns of import paths to allow requiring from. */\n allow?: string[];\n },\n];\n\nconst defaultOptions: Options = [{ allow: [] }];\n"})}),"\n",(0,s.jsx)(r.h3,{id:"allow",children:(0,s.jsx)(r.code,{children:"allow"})}),"\n",(0,s.jsx)(s.Fragment,{children:(0,s.jsxs)(r.p,{children:["Patterns of import paths to allow requiring from. Default: ",(0,s.jsx)(r.code,{children:"[]"}),"."]})}
1),"\n","\n",(0,s.jsxs)(r.p,{children:["A array of strings. These strings will be compiled into regular expressions with the ",(0,s.jsx)(r.code,{children:"u"})," flag and be used to test against the imported path. A common use case is to allow importing ",(0,s.jsx)(r.code,{children:"package.json"}),". This is because ",(0,s.jsx)(r.code,{children:"package.json"})," commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with ",(0,s.jsx)(r.code,{children:"resolveJsonModule"})," enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where ",(0,s.jsx)(r.code,{children:"import"})," statements cannot work."]}),"\n",(0,s.jsxs)(r.p,{children:["With ",(0,s.jsx)(r.code,{children:"{allow: ['/package\\\\.json$']}"}),":"]}),"\n",(0,s.jsxs)(i.A,{children:[(0,s.jsx)(l.A,{value:"\u274C Incorrect",children:(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-ts",metastring:'option=\'{ "allow": ["/package.json$"] }\' eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oDcBDaWtEQBHWJSGoMAbXDYciaNA7RIAGllysc7JF7x4HAO7owM7dsitivMgGteAc0QA6AFbIOTACSQN2gLp+EAC+foEhIMFAA"',children:"const foo = require('../data.json');\n"})})}),(0,s.jsx)(l.A,{value:"\u2705 Correct",children:(0,s.jsx)(r.pre,{children:(0,s.jsx)(r.code,{className:"language-ts",metastring:'option=\'{ "allow": ["/package.json$"] }\' eslintrcHash="N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1oDcBDaWtEQBHWJSGoMAbXDYciaNA7RIAGllysc7JF7x4HAO7owM7dsitivMgGteAc0QA6AFbIOTACSQN2gLp+EAC+foEhIMFAA"',children:"const foo = require('../package.json');\n"})})})]}),"\n",(0,s.jsx)(r.h2,{id:"when-not-to-use-it",children:"When Not To Use It"}),"\n",(0,s.jsxs)(r.p,{children:["If your project frequently uses older CommonJS ",(0,s.jsx)(r.code,{children:"require"}),"s, then this rule might not be applicable to you.\nIf only a subset of your project uses ",(0,s.jsx)(r.code,{children:"require"}),"s then you might consider using ",(0,s.jsx)(r.a,{href:"https://eslint.org/docs/latest/use/configure/rules#use-configuration-comments",children:"ESLint disable comments"})," for those specific situations instead of completely disabling this rule."]}),"\n",(0,s.jsx)(r.h2,{id:"related-to",children:"Related To"}),"\n",(0,s.jsxs)(r.ul,{children:["\n",(0,s.jsx)(r.li,{children:(0,s.jsx)(r.a,{href:"/rules/no-require-imports",children:(0,s.jsx)(r.code,{children:"no-require-imports"})})}),"\n"]}),"\n",(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(r.h2,{id:"resources",children:"Resources"}),"\n",(0,s.jsxs)(r.ul,{children:["\n",(0,s.jsx)(r.li,{children:(0,s.jsx)(r.a,{href:"https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/src/rules/no-var-requires.ts",children:"Rule source"})}),"\n",(0,s.jsx)(r.li,{children:(0,s.jsx)(r.a,{href:"https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/tests/rules/no-var-requires.test.ts",children:"Test source"})}),"\n"]})]}),"\n",(0,s.jsx)(t,{children:(0,s.jsx)(r.meta,{content:"no-var-requires: Disallow require statements except in import statements",name:"twitter:image:alt"})})]})}function h(e={}){let{wrapper:r}={...(0,o.R)(),...e.components};return r?(0,s.jsx)(r,{...e,children:(0,s.jsx)(p,{...e})}):p(e)}function m(e,r){throw Error("Expected "+(r?"component":"object")+" `"+e+"` to be defined: you likely forgot to import, pass, or provide it.")}},46742(e,r,t){t.d(r,{A:()=>l});var n=t(65723);t(22155);var s=t(70851),o=t(28226);function i({children:e,className:r,hidden:t}){return(0,n.jsx)("div",{role:"tabpanel",className:(0,s.A)("tabItem_eQ9s",r),hidden:t,children:e})}function l({children:e,className:r,value:t}){let{selectedValue:s,lazy:a}=(0,o.uc)(),c=t===s;return!c&&a?null:(0,n.jsx)(i,{className:r,hidden:!c,children:e})}},70503(e,r,t){t.d(r,{A:()=>p});var n=t(65723);t(22155);var s=t(70851),o=t(98838),i=t(28226),l=t(26309),a=t(29021);function c({className:e}){let{selectedValue:r,selectValue:t,tabValues:o,block:a}=(0,i.uc)(),u=[],{blockElementScrollPositionUntilNextRender:d}=(0,l.a_)(),p=e=>{let n=e.currentTarget,s=o[u.indexOf(n)].value;s!==r&&(d(n),t(s))},h=e=>{let r=null;switch(e.key){case"Enter":p(e);break;case"ArrowRight":{let t=u.indexOf(e.currentTarget)+1;r=u[t]??u[0];break}case"ArrowLeft":{let t=u.indexOf(e.currentTarget)-1;r=u[t]??u[u.length-1]}}r?.focus()};return(0,n.jsx)("ul",{role:"tablist","aria-orientation":"horizontal",className:(0,s.A)("tabs",{"tabs--block":a},e),children:o.map(({value:e,label:t,attributes:o})=>(0,n.jsx)("li",{role:"tab",tabIndex:r===e?0:-1,"aria-selected":r===e,ref:e=>{u.push(e)},onKeyDown:h,onClick:p,...o,className:(0,s.A)("tabs__item","tabItem_e1w9",o?.className,{"tabs__item--active":r===e}),children:t??e},e))})}function u({children:e}){return(0,n.jsx)("div",{className:"margin-top--md",children:e})}function d({className:e,children:r}){return(0,n.jsxs)("div",{className:(0,s.A)(o.G.tabs.container,"tabs-container","tabList_mVW_"),children:[(0,n.jsx)(c,{className:e}),(0,n.jsx)(u,{children:r})]})}function p(e){let r=(0,a.A)(),t=(0,i.OC)(e);return(0,n.jsx)(i.O_,{value:t,children:(0,n.jsx)(d,{className:e.className,children:(0,i.vT)(e.children)})},String(r))}},28226(e,r,t){t.d(r,{OC:()=>p,O_:()=>f,uc:()=>m,vT:()=>u});var n=t(65723),s=t(22155),o=t(62934),i=t(24127),l=t(12910),a=t(22399),c=t(6660);function u(e){return s.Children.toArray(e).filter(e=>"\n"!==e)}function d({value:e,tabValues:r}){return r.some(r=>r.value===e)}function p(e){let r,{defaultValue:t,queryString:n=!1,groupId:u}=e,p=function(e){let{values:r,children:t}=e;return(0,s.useMemo)(()=>{let e=r??s.Children.toArray(t).flatMap(e=>{if(!e)return[];if((0,s.isValidElement)(e)&&function(e){let{props:r}=e;return!!r&&"object"==typeof r&&"value"in r}(e))return[e];
1let r="string"==typeof e.type?e.type:e.type.name;throw Error(`Docusaurus error: Bad <Tabs> child <${r}>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop. 2If you do not want to pass on a "value" prop to the direct children of <Tabs>, you can also pass an explicit <Tabs values={...}> prop.`)}).map(({props:{value:e,label:r,attributes:t,default:n}})=>({value:e,label:r,attributes:t,default:n})),n=(0,a.XI)(e,(e,r)=>e.value===r.value);if(n.length>0)throw Error(`Docusaurus error: Duplicate values "${n.map(e=>`'${e.value}'`).join(", ")}" found in <Tabs>. Every value needs to be unique.`);return e},[r,t])}(e),[h,m]=(0,s.useState)(()=>(function({defaultValue:e,tabValues:r}){if(0===r.length)throw Error("Docusaurus error: the <Tabs> component requires at least one <TabItem> children component");if(e){if(!d({value:e,tabValues:r}))throw Error(`Docusaurus error: The <Tabs> has a defaultValue "${e}" but none of its children has the corresponding value. Available values are: ${r.map(e=>e.value).join(", ")}. If you intend to show no default tab, use defaultValue={null} instead.`);return e}let t=r.find(e=>e.default)??r[0];if(!t)throw Error("Unexpected error: 0 tabValues");return t.value})({defaultValue:t,tabValues:p})),[f,x]=function({queryString:e=!1,groupId:r}){let t=(0,o.W6)(),n=function({queryString:e=!1,groupId:r}){if("string"==typeof e)return e;if(!1===e)return null;if(!0===e&&!r)throw Error('Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".');return r??null}({queryString:e,groupId:r});return[(0,l.aZ)(n),(0,s.useCallback)(e=>{if(!n)return;let r=new URLSearchParams(t.location.search);r.set(n,e),t.replace({...t.location,search:r.toString()})},[n,t])]}({queryString:n,groupId:u}),[g,j]=function({groupId:e}){let r=e?`docusaurus.tab.${e}`:null,[t,n]=(0,c.Dv)(r);return[t,(0,s.useCallback)(e=>{r&&n.set(e)},[r,n])]}({groupId:u}),v=d({value:r=f??g,tabValues:p})?r:null;return(0,i.A)(()=>{v&&m(v)},[v]),{selectedValue:h,selectValue:(0,s.useCallback)(e=>{if(!d({value:e,tabValues:p}))throw Error(`Can't select invalid tab value=${e}`);m(e),x(e),j(e)},[x,j,p]),tabValues:p,lazy:e.lazy??!1,block:e.block??!1}}let h=(0,s.createContext)(null);function m(){let e=s.useContext(h);if(!e)throw Error("useTabsContext() must be used within a Tabs component");return e}function f(e){return(0,n.jsx)(h.Provider,{value:e.value,children:e.children})}},7143(e,r,t){t.d(r,{R:()=>i,x:()=>l});var n=t(22155);let s={},o=n.createContext(s);function i(e){let r=n.useContext(o);return n.useMemo(function(){return"function"==typeof e?e(r):{...r,...e}},[r,e])}function l(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:i(e.components),n.createElement(o.Provider,{value:r},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.