javascript - Typescript module, require external node_modules -
i need use simple node_module inside simple typescript file, seems compiler doesn't want it.
here's simple ts file :
import glob = require('glob'); console.log(glob);
and i've got error :
[13:51:11] compiling typescript files using tsc version 1.5.0 [13:51:12] [tsc] > f:/skeletonproject/boot/ts/boot.ts(4,23): error ts2307: cannot find external module 'glob'. [13:51:12] failed compile typescript: error: tsc command has exited code:2 events.js:72 throw er; // unhandled 'error' event ^ error: failed compile: tsc command has exited code:2 npm err! skeleton-typescript-name@0.0.1 start: `node compile && node ./boot/js/boot.js` npm err! exit status 8 npm err! npm err! failed @ skeleton-typescript-name@0.0.1 start script.
however, when use simple declaration in same script, works :
var x = 0; console.log(x); // prints 0 after typescript compilation
what doing wrong in case ?
edit:
here's gulp file :
var gulp = require('gulp'); var typescript = require('gulp-tsc'); gulp.task('compileapp', ['compileboot'], function () { return gulp.src(['app/src/**/*.ts']) .pipe(typescript()) .pipe(gulp.dest('app/dist/')) }); gulp.task('compileboot', function () { return gulp.src(['boot/ts/*.ts']) .pipe(typescript({ module:'commonjs' })) .pipe(gulp.dest('boot/js/')) }); gulp.start('compileapp');
thanks advance
thanks advance
you using correct syntax:
import glob = require('glob');
but error: cannot find external module 'glob'
pointing out using special case.
by default, compiler looking glob.ts
, in case using node module, not module have written. reason, glob
module need special treatment...
if glob
plain javascript module, can add file named glob.d.ts
type information described module.
glob.d.ts
declare module "glob" { export class example { doit(): string; } }
app.ts
import glob = require('glob'); var x = new glob.example();
some node modules include .d.ts
in package, in other cases can grab definitely typed.
Comments
Post a Comment