【发布时间】:2015-05-15 11:56:02
【问题描述】:
我需要在一个简单的打字稿文件中使用一个简单的 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
在这种情况下我做错了什么?
编辑:
这是我的 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或import 'glob' -
导入全局;在 IDE 中给我一个错误,在使用 import 'glob' 时我应该如何引用我的库?感谢您的回答
-
应该是 var glob = require('glob');你也使用 import 吗?
-
这不是带有 import 的标准 typescript 方法吗?
-
您是否为打字稿编译器指定了正确的
--module选项?对于节点,它应该是--module commonjs
标签: javascript node.js typescript