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.

tsconfig-paths crashes when `compilerOptions` is not present

See original GitHub issue

To reproduce:

tsconfig.json:

{
  "extends": "../../tsconfig.json",
}

Now I run: "test": "mocha --no-timeouts --compilers ts:ts-node/register -r tsconfig-paths/register 'tests/index.ts'"

And get:

/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/tsconfig-paths/lib/tsconfig-loader.js:17
        baseUrl: loadResult.config.compilerOptions.baseUrl,
                                                  ^
TypeError: Cannot read property 'baseUrl' of undefined
    at loadSyncDefault (/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/tsconfig-paths/lib/tsconfig-loader.js:17:51)
    at tsConfigLoader (/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/tsconfig-paths/lib/tsconfig-loader.js:8:22)
    at Object.configLoader (/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/tsconfig-paths/lib/config-loader.js:18:22)
    at Object.register (/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/tsconfig-paths/lib/register.js:9:46)
    at Object.<anonymous> (/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/tsconfig-paths/register.js:1:15)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)
    at /home/whoeverest/freelance/99abcproj/my-work-project/node_modules/mocha/bin/_mocha:345:3
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (/home/whoeverest/freelance/99abcproj/my-work-project/node_modules/mocha/bin/_mocha:344:10)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3

If I add compilerOptions to tsconfig.json it works. This doesn’t crash:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "./build"
  }
}

Then I get a warning: Missing baseUrl in compilerOptions. tsconfig-paths will be skipped

I’m not very familiar with this tool, but it’s obvious that it tries to read from compilerOptions which in this case is undefined. Hope it makes sense.


EDIT: BTW, there is a baseUrl defined in the top-level tsconfig.json which the local one extends. Is that a bug, too?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jonaskellocommented, Nov 4, 2017

Yes, we are using the tsconfig package to read the tsconfig.json file. The tsconfig.json file is not 100% json. For example it allows for comments which the json standard does not. I think submitting a PR for the issue mentioned above would be the best solution.

1reaction
wh4everestcommented, Nov 3, 2017

I have set paths and baseUrl in the main tsconfig.json from which I extend:

{
  "compilerOptions": {
    "sourceMap": true,
    "sourceRoot": ".",
    "...": "...",
    "baseUrl": "./",
    "paths": {
      "*": ["packages/*/src"]
    },
    "plugins": ["..."]
}

I think some part of the library code parses the derived tsconfig.json, assumes that compilerOptions is present on the object (when trying to check if baseUrl is present) and throws the exception “Cannot read property ‘baseUrl’ of undefined”.

A totally blind guess: some check somewhere needs to look like this: obj.compilerOptions && obj.compilerOptions.baseUrl 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test tsconfig, losing autocompletion and typing when ...
Short answer: Add "baseUrl": "." to your tests/tsconfig.json . Explanation: The tests/tsconfig.json inherits the baseUrl from the main ...
Read more >
TSConfig Reference - Docs on every TSConfig option
All relative paths found in the configuration file will be resolved relative to the configuration file they originated in.
Read more >
tsconfig-paths
The typescript compiler can resolve these paths from tsconfig so it will compile OK. But if you then try to execute the compiled...
Read more >
Typescript — How to solve the problem with unresolved ...
js file containing the line with the path alias. The actual cause of the issue is that the tsc compiler does not transpiles...
Read more >
Typescript: "paths" tsconfig - YouTube
# typescript # tsc Here's a thing that might help you with staying focused on features by setting a perman … Show more....
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