{"componentChunkName":"component---src-templates-handbook-tsx","path":"/docs/handbook/module-resolution.html","result":{"data":{"allSitePage":{"nodes":[{"path":"/dev-404-page/"},{"path":"/docs/handbook/advanced-types.html"},{"path":"/docs/handbook/basic-types.html"},{"path":"/docs/handbook/classes.html"},{"path":"/docs/handbook/compiler-options-in-msbuild.html"},{"path":"/docs/handbook/compiler-options.html"},{"path":"/docs/handbook/configuring-watch.html"},{"path":"/docs/handbook/declaration-merging.html"},{"path":"/docs/handbook/decorators.html"},{"path":"/docs/handbook/enums.html"},{"path":"/docs/handbook/functions.html"},{"path":"/docs/handbook/generics.html"},{"path":"/docs/handbook/integrating-with-build-tools.html"},{"path":"/docs/handbook/interfaces.html"},{"path":"/docs/handbook/iterators-and-generators.html"},{"path":"/docs/handbook/jsdoc-supported-types.html"},{"path":"/docs/handbook/jsx.html"},{"path":"/docs/handbook/literal-types.html"},{"path":"/docs/handbook/mixins.html"},{"path":"/docs/handbook/module-resolution.html"},{"path":"/docs/handbook/modules.html"},{"path":"/docs/handbook/namespaces-and-modules.html"},{"path":"/docs/handbook/namespaces.html"},{"path":"/docs/handbook/nightly-builds.html"},{"path":"/docs/handbook/project-references.html"},{"path":"/docs/handbook/symbols.html"},{"path":"/docs/handbook/"},{"path":"/docs/handbook/triple-slash-directives.html"},{"path":"/docs/handbook/type-checking-javascript-files.html"},{"path":"/docs/handbook/type-compatibility.html"},{"path":"/docs/handbook/type-inference.html"},{"path":"/docs/handbook/unions-and-intersections.html"},{"path":"/docs/handbook/utility-types.html"},{"path":"/docs/handbook/variable-declarations.html"},{"path":"/docs/handbook/writing-declaration-files.html"},{"path":"/docs/handbook/declaration-files/by-example.html"},{"path":"/docs/handbook/declaration-files/consumption.html"},{"path":"/docs/handbook/declaration-files/deep-dive.html"},{"path":"/docs/handbook/declaration-files/do-s-and-don-ts.html"},{"path":"/docs/handbook/declaration-files/introduction.html"},{"path":"/docs/handbook/declaration-files/library-structures.html"},{"path":"/docs/handbook/declaration-files/publishing.html"},{"path":"/docs/handbook/declaration-files/templates.html"},{"path":"/docs/handbook/release-notes/typescript-1-1.html"},{"path":"/docs/handbook/release-notes/typescript-1-3.html"},{"path":"/docs/handbook/release-notes/typescript-1-4.html"},{"path":"/docs/handbook/release-notes/typescript-1-5.html"},{"path":"/docs/handbook/release-notes/typescript-1-6.html"},{"path":"/docs/handbook/release-notes/typescript-1-7.html"},{"path":"/docs/handbook/release-notes/typescript-1-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-0.html"},{"path":"/docs/handbook/release-notes/typescript-2-2.html"},{"path":"/docs/handbook/release-notes/typescript-2-1.html"},{"path":"/docs/handbook/release-notes/typescript-2-3.html"},{"path":"/docs/handbook/release-notes/typescript-2-4.html"},{"path":"/docs/handbook/release-notes/typescript-2-5.html"},{"path":"/docs/handbook/release-notes/typescript-2-6.html"},{"path":"/docs/handbook/release-notes/typescript-2-7.html"},{"path":"/docs/handbook/release-notes/typescript-2-8.html"},{"path":"/docs/handbook/release-notes/typescript-2-9.html"},{"path":"/docs/handbook/release-notes/typescript-3-0.html"},{"path":"/docs/handbook/release-notes/typescript-3-1.html"},{"path":"/docs/handbook/release-notes/typescript-3-2.html"},{"path":"/docs/handbook/release-notes/typescript-3-3.html"},{"path":"/docs/handbook/release-notes/typescript-3-4.html"},{"path":"/docs/handbook/release-notes/typescript-3-5.html"},{"path":"/docs/handbook/release-notes/typescript-3-6.html"},{"path":"/docs/handbook/release-notes/typescript-3-7.html"},{"path":"/docs/handbook/release-notes/typescript-3-8.html"},{"path":"/docs/handbook/asp-net-core.html"},{"path":"/docs/handbook/angular.html"},{"path":"/docs/handbook/dom-manipulation.html"},{"path":"/docs/handbook/gulp.html"},{"path":"/docs/handbook/migrating-from-javascript.html"},{"path":"/docs/handbook/react-&-webpack.html"},{"path":"/docs/handbook/react.html"},{"path":"/docs/handbook/typescript-in-5-minutes-func.html"},{"path":"/docs/handbook/typescript-in-5-minutes.html"},{"path":"/docs/handbook/typescript-in-5-minutes-oop.html"},{"path":"/docs/handbook/typescript-from-scratch.html"},{"path":"/docs/handbook/typescript-tooling-in-5-minutes.html"},{"path":"/docs/handbook/tsconfig-json.html"},{"path":"/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/global-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-class-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-function-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-plugin-d-ts.html"},{"path":"/docs/handbook/declaration-files/templates/module-d-ts.html"},{"path":"/docs/handbook/release-notes/overview.html"},{"path":"/tsconfig"},{"path":"/en/tsconfig"},{"path":"/ja/tsconfig"},{"path":"/vo/tsconfig"},{"path":"/play"},{"path":"/en/play"},{"path":"/es/play"},{"path":"/vo/play"},{"path":"/zh/play"},{"path":"/ja/play"},{"path":"/play/3-7/fixits/big-number-literals.ts"},{"path":"/play/3-7/fixits/const-to-let.ts"},{"path":"/play/3-7/fixits/infer-from-usage-changes.ts"},{"path":"/play/3-7/syntax-and-messaging/flattened-error-reporting.ts"},{"path":"/play/3-7/syntax-and-messaging/nullish-coalescing.ts"},{"path":"/play/3-7/syntax-and-messaging/optional-chaining.ts"},{"path":"/play/3-7/types-and-code-flow/assertion-functions.ts"},{"path":"/play/3-7/types-and-code-flow/recursive-type-references.ts"},{"path":"/play/3-7/types-and-code-flow/uncalled-function-checks.ts"},{"path":"/play/3-8/breaking-changes/checking-unions-with-index-signatures.ts"},{"path":"/play/3-8/jsdoc-improvements/accessibility-modifiers.js"},{"path":"/play/3-8/syntax-and-messaging/export-modules-from.ts"},{"path":"/play/3-8/syntax-and-messaging/private-class-fields.ts"},{"path":"/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/play/javascript/external-apis/typescript-with-node.js"},{"path":"/play/javascript/external-apis/typescript-with-web.js"},{"path":"/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/play/javascript/helping-with-javascript/errors.ts"},{"path":"/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/play/javascript/javascript-essentials/functions.ts"},{"path":"/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/play/javascript/modern-javascript/async-await.ts"},{"path":"/play/javascript/modern-javascript/immutability.ts"},{"path":"/play/javascript/modern-javascript/import-export.ts"},{"path":"/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/play/javascript/working-with-classes/classes-101.ts"},{"path":"/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/play/javascript/working-with-classes/mixins.ts"},{"path":"/play/javascript/working-with-classes/this.ts"},{"path":"/play/playground/config/javascript-playgrounds.js"},{"path":"/play/playground/config/new-compiler-defaults.ts"},{"path":"/play/playground/language/automatic-type-acquisition.ts"},{"path":"/play/playground/language/fixits.ts"},{"path":"/play/playground/tooling/mobile-support.ts"},{"path":"/play/playground/tooling/sharable-urls.ts"},{"path":"/play/playground/tooling/typescript-versions.ts"},{"path":"/play/typescript/language/soundness.ts"},{"path":"/play/typescript/language/structural-typing.ts"},{"path":"/play/typescript/language/type-guards.ts"},{"path":"/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/play/typescript/language-extensions/enums.ts"},{"path":"/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/play/typescript/meta-types/conditional-types.ts"},{"path":"/play/typescript/meta-types/discriminate-types.ts"},{"path":"/play/typescript/meta-types/indexed-types.ts"},{"path":"/play/typescript/meta-types/mapped-types.ts"},{"path":"/play/typescript/primitives/any.ts"},{"path":"/play/typescript/primitives/literals.ts"},{"path":"/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/play/typescript/primitives/unknown-and-never.ts"},{"path":"/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/play/typescript/type-primitives/nullable-types.ts"},{"path":"/play/typescript/type-primitives/tuples.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/es/play/javascript/external-apis/typescript-with-node.js"},{"path":"/es/play/javascript/external-apis/typescript-with-web.js"},{"path":"/es/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/es/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/es/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/es/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/es/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/es/play/typescript/type-primitives/nullable-types.ts"},{"path":"/es/play/typescript/type-primitives/tuples.ts"},{"path":"/ja/play/typescript/language-extensions/enums.ts"},{"path":"/ja/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/ja/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/ja/play/typescript/primitives/any.ts"},{"path":"/ja/play/typescript/primitives/literals.ts"},{"path":"/ja/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/ja/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/vo/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/vo/play/javascript/javascript-essentials/functions.ts"},{"path":"/vo/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/vo/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-deno.ts"},{"path":"/zh/play/javascript/external-apis/typescript-with-node.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-web.js"},{"path":"/zh/play/javascript/external-apis/typescript-with-webgl.js"},{"path":"/zh/play/javascript/functions-with-javascript/function-chaining.ts"},{"path":"/zh/play/javascript/functions-with-javascript/generic-functions.ts"},{"path":"/zh/play/javascript/functions-with-javascript/typing-functions.ts"},{"path":"/zh/play/javascript/helping-with-javascript/errors.ts"},{"path":"/zh/play/javascript/helping-with-javascript/quick-fixes.ts"},{"path":"/zh/play/javascript/javascript-essentials/code-flow.ts"},{"path":"/zh/play/javascript/javascript-essentials/functions.ts"},{"path":"/zh/play/javascript/javascript-essentials/hello-world.ts"},{"path":"/zh/play/javascript/modern-javascript/import-export.ts"},{"path":"/zh/play/javascript/modern-javascript/jsdoc-support.js"},{"path":"/zh/play/javascript/working-with-classes/classes-101.ts"},{"path":"/zh/play/javascript/working-with-classes/generic-classes.ts"},{"path":"/zh/play/javascript/working-with-classes/mixins.ts"},{"path":"/zh/play/javascript/working-with-classes/this.ts"},{"path":"/zh/play/playground/config/javascript-playgrounds.js"},{"path":"/zh/play/playground/config/new-compiler-defaults.ts"},{"path":"/zh/play/playground/language/automatic-type-acquisition.ts"},{"path":"/zh/play/playground/language/fixits.ts"},{"path":"/zh/play/playground/tooling/mobile-support.ts"},{"path":"/zh/play/playground/tooling/sharable-urls.ts"},{"path":"/zh/play/playground/tooling/typescript-versions.ts"},{"path":"/zh/play/typescript/language/soundness.ts"},{"path":"/zh/play/typescript/language/structural-typing.ts"},{"path":"/zh/play/typescript/language/type-guards.ts"},{"path":"/zh/play/typescript/language/type-widening-and-narrowing.ts"},{"path":"/zh/play/typescript/language-extensions/enums.ts"},{"path":"/zh/play/typescript/language-extensions/nominal-typing.ts"},{"path":"/zh/play/typescript/language-extensions/types-vs-interfaces.ts"},{"path":"/zh/play/typescript/meta-types/conditional-types.ts"},{"path":"/zh/play/typescript/meta-types/discriminate-types.ts"},{"path":"/zh/play/typescript/meta-types/indexed-types.ts"},{"path":"/zh/play/typescript/meta-types/mapped-types.ts"},{"path":"/zh/play/typescript/primitives/any.ts"},{"path":"/zh/play/typescript/primitives/literals.ts"},{"path":"/zh/play/typescript/primitives/union-and-intersection-types.ts"},{"path":"/zh/play/typescript/primitives/unknown-and-never.ts"},{"path":"/zh/play/typescript/type-primitives/built-in-utility-types.ts"},{"path":"/zh/play/typescript/type-primitives/nullable-types.ts"},{"path":"/zh/play/typescript/type-primitives/tuples.ts"},{"path":"/zh/play/javascript/modern-javascript/immutability.ts"},{"path":"/zh/play/javascript/modern-javascript/async-await.ts"},{"path":"/zh/play/javascript/javascript-essentials/objects-and-arrays.ts"},{"path":"/community"},{"path":"/es/community"},{"path":"/ja/community"},{"path":"/vo/community"},{"path":"/zh/community"},{"path":"/download"},{"path":"/es/download"},{"path":"/ja/download"},{"path":"/vo/download"},{"path":"/zh/download"},{"path":"/empty"},{"path":"/es/empty"},{"path":"/ja/empty"},{"path":"/vo/empty"},{"path":"/zh/empty"},{"path":"/"},{"path":"/es/"},{"path":"/ja/"},{"path":"/vo/"},{"path":"/zh/"},{"path":"/tools"},{"path":"/es/tools"},{"path":"/ja/tools"},{"path":"/vo/tools"},{"path":"/zh/tools"},{"path":"/why-create-typescript"},{"path":"/es/why-create-typescript"},{"path":"/ja/why-create-typescript"},{"path":"/vo/why-create-typescript"},{"path":"/zh/why-create-typescript"},{"path":"/docs/home"},{"path":"/es/docs/home"},{"path":"/ja/docs/home"},{"path":"/vo/docs/home"},{"path":"/zh/docs/home"},{"path":"/dev/playground-plugins/"},{"path":"/dev/sandbox/"},{"path":"/dev/twoslash/"},{"path":"/dev/typescript-vfs/"}]},"markdownRemark":{"id":"4e34d93f-d6ba-5646-a5d8-ab67cde52484","excerpt":"This section assumes some basic knowledge about modules.\nPlease see the Modules documentation for more information. Module resolution is the process the…","html":"<blockquote>\n<p>This section assumes some basic knowledge about modules.\nPlease see the <a href=\"/b65a5c87767a578f1cf8ffe424260555/Modules.md\">Modules</a> documentation for more information.</p>\n</blockquote>\n<p><em>Module resolution</em> is the process the compiler uses to figure out what an import refers to.\nConsider an import statement like <code>import { a } from \"moduleA\"</code>;\nin order to check any use of <code>a</code>, the compiler needs to know exactly what it represents, and will need to check its definition <code>moduleA</code>.</p>\n<p>At this point, the compiler will ask “what’s the shape of <code>moduleA</code>?”\nWhile this sounds straightforward, <code>moduleA</code> could be defined in one of your own <code>.ts</code>/<code>.tsx</code> files, or in a <code>.d.ts</code> that your code depends on.</p>\n<p>First, the compiler will try to locate a file that represents the imported module.\nTo do so the compiler follows one of two different strategies: <a href=\"#classic\">Classic</a> or <a href=\"#node\">Node</a>.\nThese strategies tell the compiler <em>where</em> to look for <code>moduleA</code>.</p>\n<p>If that didn’t work and if the module name is non-relative (and in the case of <code>\"moduleA\"</code>, it is), then the compiler will attempt to locate an <a href=\"./Modules.md#ambient-modules\">ambient module declaration</a>.\nWe’ll cover non-relative imports next.</p>\n<p>Finally, if the compiler could not resolve the module, it will log an error.\nIn this case, the error would be something like <code>error TS2307: Cannot find module 'moduleA'.</code></p>\n<h2 id=\"relative-vs-non-relative-module-imports\" style=\"position:relative;\"><a href=\"#relative-vs-non-relative-module-imports\" aria-label=\"relative vs non relative module imports permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Relative vs. Non-relative module imports</h2>\n<p>Module imports are resolved differently based on whether the module reference is relative or non-relative.</p>\n<p>A <em>relative import</em> is one that starts with <code>/</code>, <code>./</code> or <code>../</code>.\nSome examples include:</p>\n<ul>\n<li><code>import Entry from \"./components/Entry\";</code></li>\n<li><code>import { DefaultHeaders } from \"../constants/http\";</code></li>\n<li><code>import \"/mod\";</code></li>\n</ul>\n<p>Any other import is considered <strong>non-relative</strong>.\nSome examples include:</p>\n<ul>\n<li><code>import * as $ from \"jquery\";</code></li>\n<li><code>import { Component } from \"@angular/core\";</code></li>\n</ul>\n<p>A relative import is resolved relative to the importing file and <em>cannot</em> resolve to an ambient module declaration.\nYou should use relative imports for your own modules that are guaranteed to maintain their relative location at runtime.</p>\n<p>A non-relative import can be resolved relative to <code>baseUrl</code>, or through path mapping, which we’ll cover below.\nThey can also resolve to <a href=\"./Modules.md#ambient-modules\">ambient module declarations</a>.\nUse non-relative paths when importing any of your external dependencies.</p>\n<h2 id=\"module-resolution-strategies\" style=\"position:relative;\"><a href=\"#module-resolution-strategies\" aria-label=\"module resolution strategies permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Module Resolution Strategies</h2>\n<p>There are two possible module resolution strategies: <a href=\"#node\">Node</a> and <a href=\"#classic\">Classic</a>.\nYou can use the <code>--moduleResolution</code> flag to specify the module resolution strategy.\nIf not specified, the default is <a href=\"#classic\">Classic</a> for <code>--module AMD | System | ES2015</code> or <a href=\"#node\">Node</a> otherwise.</p>\n<h3 id=\"classic\" style=\"position:relative;\"><a href=\"#classic\" aria-label=\"classic permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Classic</h3>\n<p>This used to be TypeScript’s default resolution strategy.\nNowadays, this strategy is mainly present for backward compatibility.</p>\n<p>A relative import will be resolved relative to the importing file.\nSo <code>import { b } from \"./moduleB\"</code> in source file <code>/root/src/folder/A.ts</code> would result in the following lookups:</p>\n<ol>\n<li><code>/root/src/folder/moduleB.ts</code></li>\n<li><code>/root/src/folder/moduleB.d.ts</code></li>\n</ol>\n<p>For non-relative module imports, however, the compiler walks up the directory tree starting with the directory containing the importing file, trying to locate a matching definition file.</p>\n<p>For example:</p>\n<p>A non-relative import to <code>moduleB</code> such as <code>import { b } from \"moduleB\"</code>, in a source file <code>/root/src/folder/A.ts</code>, would result in attempting the following locations for locating <code>\"moduleB\"</code>:</p>\n<ol>\n<li><code>/root/src/folder/moduleB.ts</code></li>\n<li><code>/root/src/folder/moduleB.d.ts</code></li>\n<li><code>/root/src/moduleB.ts</code></li>\n<li><code>/root/src/moduleB.d.ts</code></li>\n<li><code>/root/moduleB.ts</code></li>\n<li><code>/root/moduleB.d.ts</code></li>\n<li><code>/moduleB.ts</code></li>\n<li><code>/moduleB.d.ts</code></li>\n</ol>\n<h3 id=\"node\" style=\"position:relative;\"><a href=\"#node\" aria-label=\"node permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Node</h3>\n<p>This resolution strategy attempts to mimic the <a href=\"https://nodejs.org/\">Node.js</a> module resolution mechanism at runtime.\nThe full Node.js resolution algorithm is outlined in <a href=\"https://nodejs.org/api/modules.html#modules_all_together\">Node.js module documentation</a>.</p>\n<h4 id=\"how-nodejs-resolves-modules\" style=\"position:relative;\"><a href=\"#how-nodejs-resolves-modules\" aria-label=\"how nodejs resolves modules permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How Node.js resolves modules</h4>\n<p>To understand what steps the TS compiler will follow, it is important to shed some light on Node.js modules.\nTraditionally, imports in Node.js are performed by calling a function named <code>require</code>.\nThe behavior Node.js takes will differ depending on if <code>require</code> is given a relative path or a non-relative path.</p>\n<p>Relative paths are fairly straightforward.\nAs an example, let’s consider a file located at <code>/root/src/moduleA.js</code>, which contains the import <code>var x = require(\"./moduleB\");</code>\nNode.js resolves that import in the following order:</p>\n<ol>\n<li>Ask the file named <code>/root/src/moduleB.js</code>, if it exists.</li>\n<li>Ask the folder <code>/root/src/moduleB</code> if it contains a file named <code>package.json</code> that specifies a <code>\"main\"</code> module.\nIn our example, if Node.js found the file <code>/root/src/moduleB/package.json</code> containing <code>{ \"main\": \"lib/mainModule.js\" }</code>, then Node.js will refer to <code>/root/src/moduleB/lib/mainModule.js</code>.</li>\n<li>Ask the folder <code>/root/src/moduleB</code> if it contains a file named <code>index.js</code>.\nThat file is implicitly considered that folder’s “main” module.</li>\n</ol>\n<p>You can read more about this in Node.js documentation on <a href=\"https://nodejs.org/api/modules.html#modules_file_modules\">file modules</a> and <a href=\"https://nodejs.org/api/modules.html#modules_folders_as_modules\">folder modules</a>.</p>\n<p>However, resolution for a <a href=\"#relative-vs-non-relative-module-imports\">non-relative module name</a> is performed differently.\nNode will look for your modules in special folders named <code>node_modules</code>.\nA <code>node_modules</code> folder can be on the same level as the current file, or higher up in the directory chain.\nNode will walk up the directory chain, looking through each <code>node_modules</code> until it finds the module you tried to load.</p>\n<p>Following up our example above, consider if <code>/root/src/moduleA.js</code> instead used a non-relative path and had the import <code>var x = require(\"moduleB\");</code>.\nNode would then try to resolve <code>moduleB</code> to each of the locations until one worked.</p>\n<ol>\n<li><code>/root/src/node_modules/moduleB.js</code></li>\n<li><code>/root/src/node_modules/moduleB/package.json</code> (if it specifies a <code>\"main\"</code> property)</li>\n<li><code>/root/src/node_modules/moduleB/index.js</code>\n<br /><br /></li>\n<li><code>/root/node_modules/moduleB.js</code></li>\n<li><code>/root/node_modules/moduleB/package.json</code> (if it specifies a <code>\"main\"</code> property)</li>\n<li><code>/root/node_modules/moduleB/index.js</code>\n<br /><br /></li>\n<li><code>/node_modules/moduleB.js</code></li>\n<li><code>/node_modules/moduleB/package.json</code> (if it specifies a <code>\"main\"</code> property)</li>\n<li><code>/node_modules/moduleB/index.js</code></li>\n</ol>\n<p>Notice that Node.js jumped up a directory in steps (4) and (7).</p>\n<p>You can read more about the process in Node.js documentation on <a href=\"https://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders\">loading modules from <code>node_modules</code></a>.</p>\n<h4 id=\"how-typescript-resolves-modules\" style=\"position:relative;\"><a href=\"#how-typescript-resolves-modules\" aria-label=\"how typescript resolves modules permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How TypeScript resolves modules</h4>\n<p>TypeScript will mimic the Node.js run-time resolution strategy in order to locate definition files for modules at compile-time.\nTo accomplish this, TypeScript overlays the TypeScript source file extensions (<code>.ts</code>, <code>.tsx</code>, and <code>.d.ts</code>) over Node’s resolution logic.\nTypeScript will also use a field in <code>package.json</code> named <code>\"types\"</code> to mirror the purpose of <code>\"main\"</code> - the compiler will use it to find the “main” definition file to consult.</p>\n<p>For example, an import statement like <code>import { b } from \"./moduleB\"</code> in  <code>/root/src/moduleA.ts</code> would result in attempting the following locations for locating <code>\"./moduleB\"</code>:</p>\n<ol>\n<li><code>/root/src/moduleB.ts</code></li>\n<li><code>/root/src/moduleB.tsx</code></li>\n<li><code>/root/src/moduleB.d.ts</code></li>\n<li><code>/root/src/moduleB/package.json</code> (if it specifies a <code>\"types\"</code> property)</li>\n<li><code>/root/src/moduleB/index.ts</code></li>\n<li><code>/root/src/moduleB/index.tsx</code></li>\n<li><code>/root/src/moduleB/index.d.ts</code></li>\n</ol>\n<p>Recall that Node.js looked for a file named <code>moduleB.js</code>, then an applicable <code>package.json</code>, and then for an <code>index.js</code>.</p>\n<p>Similarly, a non-relative import will follow the Node.js resolution logic, first looking up a file, then looking up an applicable folder.\nSo <code>import { b } from \"moduleB\"</code> in source file <code>/root/src/moduleA.ts</code> would result in the following lookups:</p>\n<ol>\n<li><code>/root/src/node_modules/moduleB.ts</code></li>\n<li><code>/root/src/node_modules/moduleB.tsx</code></li>\n<li><code>/root/src/node_modules/moduleB.d.ts</code></li>\n<li><code>/root/src/node_modules/moduleB/package.json</code> (if it specifies a <code>\"types\"</code> property)</li>\n<li><code>/root/src/node_modules/@types/moduleB.d.ts</code></li>\n<li><code>/root/src/node_modules/moduleB/index.ts</code></li>\n<li><code>/root/src/node_modules/moduleB/index.tsx</code></li>\n<li><code>/root/src/node_modules/moduleB/index.d.ts</code>\n<br /><br /></li>\n<li><code>/root/node_modules/moduleB.ts</code></li>\n<li><code>/root/node_modules/moduleB.tsx</code></li>\n<li><code>/root/node_modules/moduleB.d.ts</code></li>\n<li><code>/root/node_modules/moduleB/package.json</code> (if it specifies a <code>\"types\"</code> property)</li>\n<li><code>/root/node_modules/@types/moduleB.d.ts</code></li>\n<li><code>/root/node_modules/moduleB/index.ts</code></li>\n<li><code>/root/node_modules/moduleB/index.tsx</code></li>\n<li><code>/root/node_modules/moduleB/index.d.ts</code>\n<br /><br /></li>\n<li><code>/node_modules/moduleB.ts</code></li>\n<li><code>/node_modules/moduleB.tsx</code></li>\n<li><code>/node_modules/moduleB.d.ts</code></li>\n<li><code>/node_modules/moduleB/package.json</code> (if it specifies a <code>\"types\"</code> property)</li>\n<li><code>/node_modules/@types/moduleB.d.ts</code></li>\n<li><code>/node_modules/moduleB/index.ts</code></li>\n<li><code>/node_modules/moduleB/index.tsx</code></li>\n<li><code>/node_modules/moduleB/index.d.ts</code></li>\n</ol>\n<p>Don’t be intimidated by the number of steps here - TypeScript is still only jumping up directories twice at steps (9) and (17).\nThis is really no more complex than what Node.js itself is doing.</p>\n<h2 id=\"additional-module-resolution-flags\" style=\"position:relative;\"><a href=\"#additional-module-resolution-flags\" aria-label=\"additional module resolution flags permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Additional module resolution flags</h2>\n<p>A project source layout sometimes does not match that of the output.\nUsually a set of build steps result in generating the final output.\nThese include compiling <code>.ts</code> files into <code>.js</code>, and copying dependencies from different source locations to a single output location.\nThe net result is that modules at runtime may have different names than the source files containing their definitions.\nOr module paths in the final output may not match their corresponding source file paths at compile time.</p>\n<p>The TypeScript compiler has a set of additional flags to <em>inform</em> the compiler of transformations that are expected to happen to the sources to generate the final output.</p>\n<p>It is important to note that the compiler will <em>not</em> perform any of these transformations;\nit just uses these pieces of information to guide the process of resolving a module import to its definition file.</p>\n<h3 id=\"base-url\" style=\"position:relative;\"><a href=\"#base-url\" aria-label=\"base url permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Base URL</h3>\n<p>Using a <code>baseUrl</code> is a common practice in applications using AMD module loaders where modules are “deployed” to a single folder at run-time.\nThe sources of these modules can live in different directories, but a build script will put them all together.</p>\n<p>Setting <code>baseUrl</code> informs the compiler where to find modules.\nAll module imports with non-relative names are assumed to be relative to the <code>baseUrl</code>.</p>\n<p>Value of <em>baseUrl</em> is determined as either:</p>\n<ul>\n<li>value of <em>baseUrl</em> command line argument (if given path is relative, it is computed based on current directory)</li>\n<li>value of <em>baseUrl</em> property in ‘tsconfig.json’ (if given path is relative, it is computed based on the location of ‘tsconfig.json’)</li>\n</ul>\n<p>Note that relative module imports are not impacted by setting the baseUrl, as they are always resolved relative to their importing files.</p>\n<p>You can find more documentation on baseUrl in <a href=\"http://requirejs.org/docs/api.html#config-baseUrl\">RequireJS</a> and <a href=\"https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#baseurl\">SystemJS</a> documentation.</p>\n<h3 id=\"path-mapping\" style=\"position:relative;\"><a href=\"#path-mapping\" aria-label=\"path mapping permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Path mapping</h3>\n<p>Sometimes modules are not directly located under <em>baseUrl</em>.\nFor instance, an import to a module <code>\"jquery\"</code> would be translated at runtime to <code>\"node_modules/jquery/dist/jquery.slim.min.js\"</code>.\nLoaders use a mapping configuration to map module names to files at run-time, see <a href=\"http://requirejs.org/docs/api.html#config-paths\">RequireJs documentation</a> and <a href=\"https://github.com/systemjs/systemjs/blob/master/docs/config-api.md#paths\">SystemJS documentation</a>.</p>\n<p>The TypeScript compiler supports the declaration of such mappings using <code>\"paths\"</code> property in <code>tsconfig.json</code> files.\nHere is an example for how to specify the <code>\"paths\"</code> property for <code>jquery</code>.</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"baseUrl\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\".\"</span><span style=\"color: #000000\">, </span><span style=\"color: #008000\">// This must be specified if \"paths\" is.</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"paths\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0451A5\">\"jquery\"</span><span style=\"color: #000000\">: [</span><span style=\"color: #A31515\">\"node_modules/jquery/dist/jquery\"</span><span style=\"color: #000000\">] </span><span style=\"color: #008000\">// This mapping is relative to \"baseUrl\"</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Please notice that <code>\"paths\"</code> are resolved relative to <code>\"baseUrl\"</code>.\nWhen setting <code>\"baseUrl\"</code> to another value than <code>\".\"</code>, i.e. the directory of <code>tsconfig.json</code>, the mappings must be changed accordingly.\nSay, you set <code>\"baseUrl\": \"./src\"</code> in the above example, then jquery should be mapped to <code>\"../node_modules/jquery/dist/jquery\"</code>.</p>\n<p>Using <code>\"paths\"</code> also allows for more sophisticated mappings including multiple fall back locations.\nConsider a project configuration where only some modules are available in one location, and the rest are in another.\nA build step would put them all together in one place.\nThe project layout may look like:</p>\n<pre><code class=\"language-tree\">projectRoot\n├── folder1\n│   ├── file1.ts (imports 'folder1/file2' and 'folder2/file3')\n│   └── file2.ts\n├── generated\n│   ├── folder1\n│   └── folder2\n│       └── file3.ts\n└── tsconfig.json\n</code></pre>\n<p>The corresponding <code>tsconfig.json</code> would look like:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"baseUrl\"</span><span style=\"color: #000000\">: </span><span style=\"color: #A31515\">\".\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"paths\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #0451A5\">\"*\"</span><span style=\"color: #000000\">: [</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #A31515\">\"*\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">        </span><span style=\"color: #A31515\">\"generated/*\"</span>\n<span style=\"color: #000000\">      ]</span>\n<span style=\"color: #000000\">    }</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>This tells the compiler for any module import that matches the pattern <code>\"*\"</code> (i.e. all values), to look in two locations:</p>\n<ol>\n<li><code>\"*\"</code>: meaning the same name unchanged, so map <code>&#x3C;moduleName></code> => <code>&#x3C;baseUrl>/&#x3C;moduleName></code></li>\n<li><code>\"generated/*\"</code> meaning the module name with an appended prefix “generated”, so map <code>&#x3C;moduleName></code> => <code>&#x3C;baseUrl>/generated/&#x3C;moduleName></code></li>\n</ol>\n<p>Following this logic, the compiler will attempt to resolve the two imports as such:</p>\n<p>import ‘folder1/file2’:</p>\n<ol>\n<li>pattern ’*’ is matched and wildcard captures the whole module name</li>\n<li>try first substitution in the list: ’*’ -> <code>folder1/file2</code></li>\n<li>result of substitution is non-relative name - combine it with <em>baseUrl</em> -> <code>projectRoot/folder1/file2.ts</code>.</li>\n<li>File exists. Done.</li>\n</ol>\n<p>import ‘folder2/file3’:</p>\n<ol>\n<li>pattern ’*’ is matched and wildcard captures the whole module name</li>\n<li>try first substitution in the list: ’*’ -> <code>folder2/file3</code></li>\n<li>result of substitution is non-relative name - combine it with <em>baseUrl</em> -> <code>projectRoot/folder2/file3.ts</code>.</li>\n<li>File does not exist, move to the second substitution</li>\n<li>second substitution ‘generated/*’ -> <code>generated/folder2/file3</code></li>\n<li>result of substitution is non-relative name - combine it with <em>baseUrl</em> -> <code>projectRoot/generated/folder2/file3.ts</code>.</li>\n<li>File exists. Done.</li>\n</ol>\n<h3 id=\"virtual-directories-with-rootdirs\" style=\"position:relative;\"><a href=\"#virtual-directories-with-rootdirs\" aria-label=\"virtual directories with rootdirs permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Virtual Directories with <code>rootDirs</code></h3>\n<p>Sometimes the project sources from multiple directories at compile time are all combined to generate a single output directory.\nThis can be viewed as a set of source directories create a “virtual” directory.</p>\n<p>Using ‘rootDirs’, you can inform the compiler of the <em>roots</em> making up this “virtual” directory;\nand thus the compiler can resolve relative modules imports within these “virtual” directories <em>as if</em> were merged together in one directory.</p>\n<p>For example consider this project structure:</p>\n<pre><code class=\"language-tree\"> src\n └── views\n     └── view1.ts (imports './template1')\n     └── view2.ts\n\n generated\n └── templates\n         └── views\n             └── template1.ts (imports './view2')\n</code></pre>\n<p>Files in <code>src/views</code> are user code for some UI controls.\nFiles in <code>generated/templates</code> are UI template binding code auto-generated by a template generator as part of the build.\nA build step will copy the files in <code>/src/views</code> and <code>/generated/templates/views</code> to the same directory in the output.\nAt run-time, a view can expect its template to exist next to it, and thus should import it using a relative name as <code>\"./template\"</code>.</p>\n<p>To specify this relationship to the compiler, use<code>\"rootDirs\"</code>.\n<code>\"rootDirs\"</code> specify a list of <em>roots</em> whose contents are expected to merge at run-time.\nSo following our example, the <code>tsconfig.json</code> file should look like:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"rootDirs\"</span><span style=\"color: #000000\">: [</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #A31515\">\"src/views\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #A31515\">\"generated/templates/views\"</span>\n<span style=\"color: #000000\">    ]</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>Every time the compiler sees a relative module import in a subfolder of one of the <code>rootDirs</code>, it will attempt to look for this import in each of the entries of <code>rootDirs</code>.</p>\n<p>The flexibility of <code>rootDirs</code> is not limited to specifying a list of physical source directories that are logically merged. The supplied array may include any number of ad hoc, arbitrary directory names, regardless of whether they exist or not. This allows the compiler to capture sophisticated bundling and runtime features such as conditional inclusion and project specific loader plugins in a type safe way.</p>\n<p>Consider an internationalization scenario where a build tool automatically generates locale specific bundles by interpolating a special path token, say <code>#{locale}</code>, as part of a relative module path such as <code>./#{locale}/messages</code>. In this hypothetical setup the tool enumerates supported locales, mapping the abstracted path into <code>./zh/messages</code>, <code>./de/messages</code>, and so forth.</p>\n<p>Assume that each of these modules exports an array of strings. For example <code>./zh/messages</code> might contain:</p>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">export</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">default</span><span style=\"color: #000000\"> [</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"您好吗\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #A31515\">\"很高兴认识你\"</span>\n<span style=\"color: #000000\">];</span></code></div></pre>\n<p>By leveraging <code>rootDirs</code> we can inform the compiler of this mapping and thereby allow it to safely resolve <code>./#{locale}/messages</code>, even though the directory will never exist. For example, with the following <code>tsconfig.json</code>:</p>\n<pre class=\"shiki\"><div class=\"language-id\">json</div><div class='code-container'><code><span style=\"color: #000000\">{</span>\n<span style=\"color: #000000\">  </span><span style=\"color: #0451A5\">\"compilerOptions\"</span><span style=\"color: #000000\">: {</span>\n<span style=\"color: #000000\">    </span><span style=\"color: #0451A5\">\"rootDirs\"</span><span style=\"color: #000000\">: [</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #A31515\">\"src/zh\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #A31515\">\"src/de\"</span><span style=\"color: #000000\">,</span>\n<span style=\"color: #000000\">      </span><span style=\"color: #A31515\">\"src/#{locale}\"</span>\n<span style=\"color: #000000\">    ]</span>\n<span style=\"color: #000000\">  }</span>\n<span style=\"color: #000000\">}</span></code></div></pre>\n<p>The compiler will now resolve <code>import messages from './#{locale}/messages'</code> to <code>import messages from './zh/messages'</code> for tooling purposes, allowing development in a locale agnostic manner without compromising design time support.</p>\n<h2 id=\"tracing-module-resolution\" style=\"position:relative;\"><a href=\"#tracing-module-resolution\" aria-label=\"tracing module resolution permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Tracing module resolution</h2>\n<p>As discussed earlier, the compiler can visit files outside the current folder when resolving a module.\nThis can be hard when diagnosing why a module is not resolved, or is resolved to an incorrect definition.\nEnabling the compiler module resolution tracing using <code>--traceResolution</code> provides insight in what happened during the module resolution process.</p>\n<p>Let’s say we have a sample application that uses the <code>typescript</code> module.\n<code>app.ts</code> has an import like <code>import * as ts from \"typescript\"</code>.</p>\n<pre><code class=\"language-tree\">│   tsconfig.json\n├───node_modules\n│   └───typescript\n│       └───lib\n│               typescript.d.ts\n└───src\n        app.ts\n</code></pre>\n<p>Invoking the compiler with <code>--traceResolution</code></p>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">tsc --traceResolution</span></code></div></pre>\n<p>Results in an output such as:</p>\n<pre><code class=\"language-txt\">======== Resolving module 'typescript' from 'src/app.ts'. ========\nModule resolution kind is not specified, using 'NodeJs'.\nLoading module 'typescript' from 'node_modules' folder.\nFile 'src/node_modules/typescript.ts' does not exist.\nFile 'src/node_modules/typescript.tsx' does not exist.\nFile 'src/node_modules/typescript.d.ts' does not exist.\nFile 'src/node_modules/typescript/package.json' does not exist.\nFile 'node_modules/typescript.ts' does not exist.\nFile 'node_modules/typescript.tsx' does not exist.\nFile 'node_modules/typescript.d.ts' does not exist.\nFound 'package.json' at 'node_modules/typescript/package.json'.\n'package.json' has 'types' field './lib/typescript.d.ts' that references 'node_modules/typescript/lib/typescript.d.ts'.\nFile 'node_modules/typescript/lib/typescript.d.ts' exist - use it as a module resolution result.\n======== Module name 'typescript' was successfully resolved to 'node_modules/typescript/lib/typescript.d.ts'. ========\n</code></pre>\n<h4 id=\"things-to-look-out-for\" style=\"position:relative;\"><a href=\"#things-to-look-out-for\" aria-label=\"things to look out for permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Things to look out for</h4>\n<ul>\n<li>Name and location of the import</li>\n</ul>\n<blockquote>\n<p>======== Resolving module <strong>‘typescript’</strong> from <strong>‘src/app.ts’</strong>. ========</p>\n</blockquote>\n<ul>\n<li>The strategy the compiler is following</li>\n</ul>\n<blockquote>\n<p>Module resolution kind is not specified, using <strong>‘NodeJs’</strong>.</p>\n</blockquote>\n<ul>\n<li>Loading of types from npm packages</li>\n</ul>\n<blockquote>\n<p>‘package.json’ has <strong>‘types’</strong> field ‘./lib/typescript.d.ts’ that references ‘node_modules/typescript/lib/typescript.d.ts’.</p>\n</blockquote>\n<ul>\n<li>Final result</li>\n</ul>\n<blockquote>\n<p>======== Module name ‘typescript’ was <strong>successfully resolved</strong> to ‘node_modules/typescript/lib/typescript.d.ts’. ========</p>\n</blockquote>\n<h2 id=\"using---noresolve\" style=\"position:relative;\"><a href=\"#using---noresolve\" aria-label=\"using   noresolve permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Using <code>--noResolve</code></h2>\n<p>Normally the compiler will attempt to resolve all module imports before it starts the compilation process.\nEvery time it successfully resolves an <code>import</code> to a file, the file is added to the set of files the compiler will process later on.</p>\n<p>The <code>--noResolve</code> compiler options instructs the compiler not to “add” any files to the compilation that were not passed on the command line.\nIt will still try to resolve the module to files, but if the file is not specified, it will not be included.</p>\n<p>For instance:</p>\n<h4 id=\"appts\" style=\"position:relative;\"><a href=\"#appts\" aria-label=\"appts permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>app.ts</h4>\n<pre class=\"shiki\"><div class=\"language-id\">ts</div><div class='code-container'><code><span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> A </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"moduleA\"</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">// OK, 'moduleA' passed on the command-line</span>\n<span style=\"color: #0000FF\">import</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">*</span><span style=\"color: #000000\"> </span><span style=\"color: #0000FF\">as</span><span style=\"color: #000000\"> B </span><span style=\"color: #0000FF\">from</span><span style=\"color: #000000\"> </span><span style=\"color: #A31515\">\"moduleB\"</span><span style=\"color: #000000\"> </span><span style=\"color: #008000\">// Error TS2307: Cannot find module 'moduleB'.</span></code></div></pre>\n<pre class=\"shiki\"><div class=\"language-id\">shell</div><div class='code-container'><code><span style=\"color: #000000\">tsc app.ts moduleA.ts --noResolve</span></code></div></pre>\n<p>Compiling <code>app.ts</code> using <code>--noResolve</code> should result in:</p>\n<ul>\n<li>Correctly finding <code>moduleA</code> as it was passed on the command-line.</li>\n<li>Error for not finding <code>moduleB</code> as it was not passed.</li>\n</ul>\n<h2 id=\"common-questions\" style=\"position:relative;\"><a href=\"#common-questions\" aria-label=\"common questions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Common Questions</h2>\n<h3 id=\"why-does-a-module-in-the-exclude-list-still-get-picked-up-by-the-compiler\" style=\"position:relative;\"><a href=\"#why-does-a-module-in-the-exclude-list-still-get-picked-up-by-the-compiler\" aria-label=\"why does a module in the exclude list still get picked up by the compiler permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Why does a module in the exclude list still get picked up by the compiler?</h3>\n<p><code>tsconfig.json</code> turns a folder into a “project”.\nWithout specifying any <code>“exclude”</code> or <code>“files”</code> entries, all files in the folder containing the <code>tsconfig.json</code> and all its sub-directories are included in your compilation.\nIf you want to exclude some of the files use <code>“exclude”</code>, if you would rather specify all the files instead of letting the compiler look them up, use <code>“files”</code>.</p>\n<p>That was <code>tsconfig.json</code> automatic inclusion.\nThat does not embed module resolution as discussed above.\nIf the compiler identified a file as a target of a module import, it will be included in the compilation regardless if it was excluded in the previous steps.</p>\n<p>So to exclude a file from the compilation, you need to exclude it and <strong>all</strong> files that have an <code>import</code> or <code>/// &#x3C;reference path=\"...\" /></code> directive to it.</p>","headings":[{"value":"Relative vs. Non-relative module imports","depth":2},{"value":"Module Resolution Strategies","depth":2},{"value":"Classic","depth":3},{"value":"Node","depth":3},{"value":"How Node.js resolves modules","depth":4},{"value":"How TypeScript resolves modules","depth":4},{"value":"Additional module resolution flags","depth":2},{"value":"Base URL","depth":3},{"value":"Path mapping","depth":3},{"value":"Virtual Directories with rootDirs","depth":3},{"value":"Tracing module resolution","depth":2},{"value":"Things to look out for","depth":4},{"value":"Using --noResolve","depth":2},{"value":"app.ts","depth":4},{"value":"Common Questions","depth":2},{"value":"Why does a module in the exclude list still get picked up by the compiler?","depth":3}],"frontmatter":{"permalink":"/docs/handbook/module-resolution.html","title":"Module Resolution"}}},"pageContext":{"slug":"/docs/handbook/module-resolution.html","isOldHandbook":true}}}