我正在開發一個web應用程序,使用angular 1.5,typescript 2.4.0,moment:2.18.1和gulp進行項目組裝。TypeScript + moment.js:錯誤TS2307:無法找到模塊'時刻'
這裏是我的tsconfig.json
:
{
"files": [
"src/app/main.ts",
"types/**/*.ts"
],
"compilerOptions": {
"noImplicitAny": false,
"target": "es2015",
"allowSyntheticDefaultImports": true
}
}
裏面我date-range-picker.component.ts
。我進口時刻lib中,在被提出的main documentation:
import * as moment from 'moment';
這對於主要項目,一飲而盡組裝任務,它依賴於tsify插件:
var browserifyIt = browserify({
basedir: '.',
debug: true,
entries: paths.browserifyEntries,
cache: {},
packageCache: {}
}).plugin(tsify);
gulp.task('bundle', ['views'], function() {
return browserifyIt
.transform('babelify', {
presets: ['es2015'],
extensions: ['.ts']
})
.bundle()
.on('error', interceptErrors)
.pipe(source(fileNames.buildJs))
.pipe(ngAnnotate())
.pipe(buffer())
.pipe(sourcemaps.init({loadMaps: true}))
.pipe(sourcemaps.write(paths.sourcemapPath))
.pipe(gulp.dest(paths.build));
});
但對於編譯工作正常測試我決定使用任務採用tsProject():
const testSrcPaths = {
....,
componentTests: 'src/app/components/**/*.test.ts',
....
};
gulp.task('component-tests:compile', function() {
return gulp.src(testSrcPaths.componentTests).pipe(tsProject())
.on('error', interceptErrors)
.js.pipe(gulp.dest(`${paths.testsDist}/components`));
});
這導致了以下錯誤:
date-range-picker/date-range-picker.component.ts(2,25): error TS2307: Cannot find module 'moment'.
可以做些什麼來解決它?
你可以試試var tsProject = ts.createProject('tsconfig.json',{}); ,也許這有助於 –
結果是一樣的 – Elias