Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tsconfig-paths crashes when compilerOptions is not present #17

Closed
wh4everest opened this issue Nov 3, 2017 · 11 comments
Closed

tsconfig-paths crashes when compilerOptions is not present #17

wh4everest opened this issue Nov 3, 2017 · 11 comments

Comments

@wh4everest
Copy link

wh4everest commented Nov 3, 2017

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?

@Jontem
Copy link
Collaborator

Jontem commented Nov 3, 2017

You need to set the baseUrl property of compilerOptions because the typescript compiler uses it to resolve paths relative to the baseUrl

From the typescript documentation

Please notice that "paths" are resolved relative to "baseUrl".

It also says

This must be specified if "paths" is.

And that is why tsconfig-paths is throwing an exception when baseUrl is missing

Also if you run tsc without specifying baseUrl you get an error

error TS5060: Option 'paths' cannot be used without specifying '--baseUrl' option.

@wh4everest
Copy link
Author

wh4everest commented 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 :)

@wh4everest
Copy link
Author

I think this is the issue: TypeStrong/tsconfig#21

@Jontem
Copy link
Collaborator

Jontem commented Nov 4, 2017

Sorry for the quick assumption. I'll have a look how we could solve this.

@jonaskello
Copy link
Member

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.

@Jontem
Copy link
Collaborator

Jontem commented Nov 18, 2017

Please give the latest version a try!

@lednhatkhanh
Copy link

Still has the same issue: Missing baseUrl in compilerOptions. tsconfig-paths will be skipped
My tsconfig.json

{
	"compilerOptions": {
		"target": "esnext",
		"module": "commonjs",
		"sourceMap": true,
		"lib": ["esnext"],
		"outDir": "build",
		"rootDir": "src",
		"baseUrl": "",
		"paths": {
			"src": ["./src"]
		},
		"importHelpers": true,
		"noEmitOnError": true,
		"strict": true,
		"noUnusedLocals": true,
		"noUnusedParameters": true,
		"noImplicitReturns": true,
		"noFallthroughCasesInSwitch": true,
		"moduleResolution": "node",
		"experimentalDecorators": true,
		"emitDecoratorMetadata": true
	}
}

Command: ts-node --project . -r tsconfig-paths/register ./src/index.ts
"tsconfig-paths": "^2.6.0"
"ts-node": "3.3.0"

@Jontem
Copy link
Collaborator

Jontem commented Dec 11, 2017

Your baseUrl is empty in the example above? Try setting it to ./

@lednhatkhanh
Copy link

lednhatkhanh commented Dec 11, 2017 via email

@jonaskello
Copy link
Member

jonaskello commented Dec 11, 2017

If tsc accepts a blank string and treats it as "./" then maybe we should too?

@Jontem
Copy link
Collaborator

Jontem commented Dec 11, 2017

Yes it looks like it works. But can't find any documentation that it is supposed to work this way. But should be an easy fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants