TypeScript: Give better error messages for bare imports with badly configured projects

When targeting es2015 or later, moduleResolution isn’t set to node by default.

Perhaps when

  • moduleResolution is calculated to be classic and
  • paths aren’t specified

we should hint to the user

Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 15
  • Comments: 16 (11 by maintainers)

Most upvoted comments

@saramorsi , I just added this line under compilerOptions in my tsconfig.json file and it solved my problem.

"moduleResolution": "node"

You need to edit the error on src\testRunner\unittests\tsserver\symLinks.ts

Let’s steer away from the word “throw” because that’s just not the way TypeScript reports errors.

You need to report the diagnostic/error that I mentioned in my last comment instead of the current error when the project is misconfigured.

The local folder only changes when the baselines have changed - so if your diffs don’t match the reference folder.

Adding the throw in the user’s project (e.g. creating the error diagnostic) should be the thing which will break a reference test of two hopefully.

Sure - you might want to create one new diagnostic in diagnosticMessages.json

Cannot find module '{0}'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?

Run npx gulp generate-diagnostics

Run the tests

npx gulp runtests-parallel

see what breaks. Diff the folders tests/baselines/local and tests/baselines/reference with a tool like WinMerge or Beyond Compare to see if the new errors are good.

If nothing fails, you need to add a test case in a file named tests/cases/compiler/errorForBareSpecifierWithImplicitModuleResolution1.ts:

// @module: es2015

import { thing } from "non-existent-module";
thing()

Then run the tests to get baselines from that. Accept the baselines:

npx gulp baseline-accept