I can't seem to convince Visual Studio Code to resolve absolute TypeScript module paths. Relative paths work, but absolute don't. I would like Visual Studio Code to resolve module paths from ./src
folder on.
// This works when source file is in /src/here/there/file.ts
// and importing an interface in /src/api/interfaces.ts
import { Interface } from '../../api/interfaces';
// This doesn't work
import { Interface } from 'api/interfaces';
import { Interface } from '/api/interfaces';
import { Interface } from 'src/api/interfaces';
import { Interface } from '/src/api/interfaces';
// This works, but it is of course not supposed to be used
import { Interface } from 'c:/..../src/api/interfaces';
The last one of course doesn't count as each developer's project path is highly likely different. But even if we'd all set a system variable %ProjectXRoot%
we can't use this variable in the code. Visual Studio Code will not resolve such module path. I've tried.
// Won't work
import { Interface } from '%ProjectXRoot%/api/interfaces';
Currently installed versions
• TypeScript: 1.8.10
• VSCode: 1.1.1
Question
I've tried to somehow configure Visual Studio Code to resolve absolute module paths, but I can't seem to do so. I've tried configuring tsconfig.json (in the project root) by adding baseUrl
in two different places.
{
...
"compilerOptions": {
"baseUrl": "./src", // Doesn't work
...
},
"baseUrl": "./src", // Doesn't work either
...
}
I've tried values like src
, ./src
and ./src/
, but none of them work in any of the upper configuration places.
So how does one configure Visual Studio Code to resolve absolute module paths from a certain folder?
If that's not possible, it would be at least better to resolve absolute paths from project root. How does Visual Studio Code determine that? Is it where you Open Folder or is it where the .vscode folder is?
But it would still be a viable solution for absolute paths. I've tried using ~
similar to Visual Studio, but to no avail either.
(Unresolved)
--
As steinso points out in his answer, all modules starting with /
, ./
or ../
are considered relative. Especially the first one surprised me completely as I usually consider that a project root-relative path.
But this fact basically means that the main question now becomes: How can I provide module imports as absolute paths (from some project root folder path) at all? Starting paths with slashes usually meant absolute, but in this case it doesn't.
Even when I set compiler option moduleResolution
to classic
(so module resolution wont be looking into node_modules
folder) the second set of imports above should actually all work as per Microsoft's linked document. But for some reason I still get red squiggly lines in Visual Studio Code and errors during compilation.
So how can I import a specific project module without providing an exact relative path to it, but rather just its own project-relative path?
typescript@next
and some minor configurations. – Lekhnath"include": ["js/*"]
) typescriptlang.org/docs/handbook/tsconfig-json.html – Christopher Davies