Typescript模块,需要外部node_modules

我需要在一个简单的打字稿文件中使用一个简单的node_module,但似乎编译器不想获取它。

这是我简单的ts文件:

import glob = require('glob'); console.log(glob); 

我有这个错误:

 [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 to compile TypeScript: Error: tsc command has exited with code:2 events.js:72 throw er; // Unhandled 'error' event ^ Error: Failed to compile: tsc command has exited with 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 at the skeleton-typescript-name@0.0.1 start script. 

但是,当我在同一个脚本中使用简单的声明时,它可以工作:

 var x = 0; console.log(x); // prints 0 after typescript compilation 

我在这种情况下做错了什么?

编辑:

这是我的大文件:

 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'); 

感谢提前

感谢提前

您正在使用正确的语法:

 import glob = require('glob'); 

但错误: Cannot find external module 'glob'指出你正在使用一个特殊的情况。

默认情况下,编译器正在寻找glob.ts ,但在你的情况下,你正在使用一个节点模块,而不是你写的模块。 为此, glob模块将需要特殊处理…

如果glob是一个普通的JavaScript模块,则可以添加一个名为glob.d.ts的文件和描述该模块的types信息。

glob.d.ts

 declare module "glob" { export class Example { doIt(): string; } } 

app.ts

 import glob = require('glob'); var x = new glob.Example(); 

一些Node模块已经在包中包含.d.ts ,在其他情况下,您可以从Definitely Typed中获取它。

这是你的代码的错误

  import glob = require('glob'); 

因为在node.js中import不是保留关键字。 如果您的应用程序中需要任何模块,只需使用该语句即可

  var glob = require('glob'); 

一旦完成,你可以使用

  console.log(glob); 

要打印glob的值。replace导入将有希望为你做的工作。