Juicy Juicy - 2 months ago 28
TypeScript Question

TypeScript compiler in Gulp says it cannot find @angular modules but compiles fine

I'm getting a lot of "false" errors when my

gulp
task compiles my typescript. Here's the relevant
gulp task
:

var tsProject = typescript.createProject('tsconfig.json', {noResolve: true});

gulp.task('build-ts', function() {
gulp.src(appDev + '**/*.ts')
.pipe(sourcemaps.init())
.pipe(typescript(tsProject))
.pipe(sourcemaps.write())
//.pipe(jsuglify())
.pipe(gulp.dest(appProd));
gulp.src(appDev + '**/*.+(html|css)')
.pipe(gulp.dest(appProd));
});


And these are the errors I get:

[09:38:52] Starting 'default'...
[09:38:52] Finished 'default' after 8.88 ms
ng/app.component.ts(1,27): error TS2307: Cannot find module '@angular/core'.
ng/app.component.ts(5,12): error TS2304: Cannot find name 'module'.
ng/app.module.ts(1,26): error TS2307: Cannot find module '@angular/core'.
ng/app.module.ts(2,31): error TS2307: Cannot find module '@angular/platform-browser'.
ng/app.module.ts(3,31): error TS2307: Cannot find module '@angular/common'.
[...]
[09:38:53] TypeScript: 27 semantic errors
[09:38:53] TypeScript: emit succeeded (with errors)
[BS] Proxying: http://0.0.0.0:5000


My tree structure looks like this (simplified):

.
├── gulpfile.js
├── ng
├── node_modules
├── package.json
├── tsconfig.json
├── typings.json
└── web


All the modules that the Typescript compiler fails to find are actually in
node_modules
and my final application works fine despite all the errors.

This is my
tsconfig.js
:

{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false,
"outDir": "./app"
},
"filesGlob": [
"./app/**/*.ts",
"!./node_modules/**/*.ts"
],
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
],
"atom": {
"rewriteTsconfig": true
}
}


How can I get rid of these "false positive" errors.

Answer

In the gulp.js file the line

var tsProject = typescript.createProject('tsconfig.json', {noExternalResolve: true, noResolve: true});

should be

var tsProject = typescript.createProject('tsconfig.json');

As we want to load external typings.

And also you need to load the typings in the main.ts file on the top :

/// <reference path="../typings/index.d.ts" />

I did clone your project and it compiles without warnings after this.