繁体   English   中英

TypeScript + moment.js:错误TS2307:找不到模块'时刻'

[英]TypeScript + moment.js: error TS2307: Cannot find module 'moment'

我正在开发一个Web应用程序,使用angular 1.5,typescript 2.4.0,时刻:2.18.1,以及用于项目组装的gulp。

这是我的tsconfig.json

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

在我的date-range-picker.component.ts 我正在导入片段lib,正如主文档中提出的那样:

import * as moment from 'moment'; 

哪个适用于依赖tsify插件的主要gulp项目组装任务:

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):错误TS2307:找不到模块'时刻'。

可以做些什么来解决它?

最后,添加moduleResolution: "node"compilerOptions解决了我的问题。 所以最后使用了以下tsconfig.json配置:

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

以下是可用于了解有关打字稿模块解析方法的更多信息的链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM