我需要在一个简单的打字稿文件中使用一个简单的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! [email protected] start: `node compile && node ./boot/js/Boot.js`
npm ERR! Exit status 8
npm ERR!
npm ERR! Failed at the [email protected] start script.
但是,当我在同一脚本中使用简单的声明时,它会起作用:
var x = 0;
console.log(x); // prints 0 after typescript compilation
在这种情况下我怎么了?
编辑:
这是我的gulp文件:
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
描述了该模块的类型信息。
glob.d.ts
declare module "glob" {
export class Example {
doIt(): string;
}
}
应用程序
import glob = require('glob');
var x = new glob.Example();
某些Node模块已经包含.d.ts
在包中,在其他情况下,您可以从Definitely Typed中获取它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句