microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.02k stars 12.49k forks source link

TSC not find imported scoped packages. #27126

Closed rbiggs closed 6 years ago

rbiggs commented 6 years ago

TypeScript Version: TS6029: Version 1.5.3

Search Terms: import scoped packages

Code

In my package.json I have

"checkjs": "tsc --allowJs --checkJs --noEmit --target ES6 src/*.js"

Expected behavior: I'm using NPM scoped packages: https://docs.npmjs.com/misc/scope. For normal ES6 imports TSC correctly resolves imported packages and parses the results. Scoped Scoped packages look like this:

import {getType} from '@composi/get-type'

The scope is basically an NPM namespace to prevent collision of package names.

Actual behavior: TSC fails to find the packages. Here's the error message I'm getting:

 npm run checkjs
> tsc --allowJs --checkJs --noEmit --target ES6 src/*.js

src/vdom.js:2:23 - error TS2307: Cannot find module '@composi/merge'.

2 import { merge } from '@composi/merge'
                        ~~~~~~~~~~~~~~~~
src/vdom.js:3:25 - error TS2307: Cannot find module '@composi/get-type'.

3 import { getType } from '@composi/get-type'
screen shot 2018-09-15 at 8 10 02 pm

Rollup and Babel 7.0.0 have no problem with import of scoped packages.

rbiggs commented 6 years ago

Here's a simple repository on Github that illustrates the project. Download, run npm install. Then try to run TypeScript type checking on the JavaScript using npm run checkjs.

https://github.com/rbiggs/scope-package-test

rbiggs commented 6 years ago

Interestingly, VSCode seems to have no problem resolving the path for scoped packages:

screen shot 2018-09-15 at 8 52 25 pm
kitsonk commented 6 years ago

Version 1.5.3 is from May 2015. VSCode uses a much newer version than that. Is the version correct?

rbiggs commented 6 years ago

I updated the project to TypeScript 3.0.3 and the same issue occurs.

screen shot 2018-09-16 at 5 57 22 am
mattmccutchen commented 6 years ago

You need to set --moduleResolution node. --moduleResolution defaults to classic when your target is es6 and node when your target is the default es3. See the Compiler Options handbook page for more information. It's highly recommendable to put your TypeScript options in a tsconfig.json file so you can easily share them between your IDE and your batch build to avoid inconsistencies like this.

rbiggs commented 6 years ago

Thanks @mattmccutchen. That seems to be what was the problem. With the --moduleResolution node flag, the tsc check is running fine. Closing this now.