2017-07-06 104 views
4

我正在開發一個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'.

可以做些什麼來解決它?

+0

你可以試試var tsProject = ts.createProject('tsconfig.json',{}); ,也許這有助於 –

+0

結果是一樣的 – Elias

回答

4

最後,加入moduleResolution: "node"compilerOptions解決了我的問題。那麼,到底下面tsconfig.json配置採用:

{ 
    "files": [ 
    "src/app/main.ts" 
    ], 
    "compilerOptions": { 
    "noImplicitAny": false, 
    "target": "es2015", 
    "moduleResolution": "node" 
    } 
} 

這裏是link一個可以用它來了解更多關於打字稿模塊解決方案。

+0

感謝您的鏈接。將moduleResolution設置爲節點可以大量破壞舊的/現有的代碼。使用tsconfig中的''''''''''''''''''''''''來恢復命令:-) – sumitkm

相關問題