question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Changing baseUrl causes compiler not to look at @types subdirectory

See original GitHub issue

When baseUrl is set in tsconfig.json, the compiler will not look into baseUrl/@types to try and find type definitions

TypeScript Version: 2.2.1

Code

Example is available at:

https://github.com/spion/ts-base-url-issue

If the “paths” entry is removed from tsconfig.json, the compiler is unable to find react-dom

Expected behavior:

Compiles fine even without a “paths” entry.

Actual behavior:

Compiler doesn’t find the definition file if the paths config option is removed. --trace-resolution shows that it never tries baseUrl/@types/react-dom

======== Resolving module 'react-dom' from '/Users/spion/Documents/tests/ts-modules-broken/app/index.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
'baseUrl' option is set to '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules', using this value to resolve non-relative module name 'react-dom'
Resolving module name 'react-dom' relative to base url '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules' - '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom'.
Loading module as file / folder, candidate module location '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom', target file type 'TypeScript'.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.ts' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.tsx' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.d.ts' does not exist.
Found 'package.json' at '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/package.json'.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.ts' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.tsx' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.d.ts' does not exist.
Loading module 'react-dom' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/Users/spion/Documents/tests/ts-modules-broken/app/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/Documents/tests/ts-modules-broken/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/Documents/tests/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/Documents/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/spion/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
'baseUrl' option is set to '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules', using this value to resolve non-relative module name 'react-dom'
Resolving module name 'react-dom' relative to base url '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules' - '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom'.
Loading module as file / folder, candidate module location '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom', target file type 'JavaScript'.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.js' does not exist.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom.jsx' does not exist.
Found 'package.json' at '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/package.json'.
'package.json' has 'main' field 'index.js' that references '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js'.
File '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js' exist - use it as a name resolution result.
======== Module name 'react-dom' was successfully resolved to '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js'. ========
app/index.ts(1,22): error TS7016: Could not find a declaration file for module 'react-dom'. '/Users/spion/Documents/tests/ts-modules-broken/vendor/node_modules/react-dom/index.js' implicitly has an 'any' type.

Workaround

Adding a paths entry gets the @types directory into the lookup list again:

    "baseUrl": "./vendor/node_modules",
    "paths": {
      "*": ["@types/*", "*"]
    }

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
ricklovecommented, Oct 10, 2017

@spion I agree. I thought baseUrl would be like defining the root of the directory (like where tsconfig.json is located.)

The 2 big surprises so far were that I had to point it to node_modules and that node_modules/@types was not found.

1reaction
spioncommented, Mar 10, 2017

I believe a “moving node_modules” example would be worthwhile since its pretty common (watch mode in docker with external readonly filesystem, official rails webpacker typescript support using vendor/node_modules etc).

Its certainly not the most intuitive behaviour as people assume that baseUrl/node_modules and baseUrl/node_modules/@types are also looked up

I think it might save people an hour or two 😀

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - can't resolve module inside subdirectory typescript ...
I had a similar issue, and it went away after re-saving the files that caused errors. The problem is very reproducible when moving...
Read more >
TSConfig Reference - Docs on every TSConfig option
This config file will include all packages under ./typings and ./vendor/types , and no packages from ./node_modules/@types . All paths are relative to...
Read more >
Typescript — How to solve the problem with unresolved path ...
The actual cause of the issue is that the tsc compiler does not transpiles correctly the path aliases to absolute (real) paths.
Read more >
Typescript does not resolve modules through tsconfig.json's ...
My tsconfig.json: { "compilerOptions": { "module": "es6", "target": "es6", "lib": ["dom", "es2016"], "moduleResolution": "node", "baseUrl": ...
Read more >
Understanding TypeScript Configuration Options
- error TS2569: Type 'Set<string>' is not an array type or a string type. Use compiler option '--downlevelIteration' to allow iterating of ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found