2017-07-06 37 views
4

私は、1.5、typescript 2.4.0、moment:2.18.1、およびgulpを使用してプロジェクトを作成しています。TypeScript + moment.js:エラーTS2307: 'moment'モジュールを見つけることができません

は、ここに私のtsconfig.jsonです:

{ 
    "files": [ 
    "src/app/main.ts", 
    "types/**/*.ts" 
    ], 
    "compilerOptions": { 
    "noImplicitAny": false, 
    "target": "es2015", 
    "allowSyntheticDefaultImports": true 
    } 
} 

date-range-picker.component.tsを内部。私はmain documentationで提案されたように、モーメントのlibをインポートしています:tsifyプラグインに依存しているタスクを組み立てるメイン一気プロジェクトのために正常に動作します

import * as moment from 'moment'; 

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" 
    } 
} 

はtypescriptですモジュール解決のアプローチについての詳細を学ぶために使用することができますlink一つです。

+0

ありがとうございます。 moduleResolutionをノードに設定すると、古いコードまたは既存のコードを大幅に改変できます。 tsconfigの '' paths'''と '' '' baseUrl'''のプロパティを使って、次のようにリストアします:-) – sumitkm

関連する問題