简体   繁体   English

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

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

I'm developing a web app, using angular 1.5, typescript 2.4.0, moment: 2.18.1, and gulp for project assembling. 我正在开发一个Web应用程序,使用angular 1.5,typescript 2.4.0,时刻:2.18.1,以及用于项目组装的gulp。

Here is my tsconfig.json : 这是我的tsconfig.json

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

Inside my date-range-picker.component.ts . 在我的date-range-picker.component.ts I'm importing moment lib, as was proposed in the main documentation : 我正在导入片段lib,正如主文档中提出的那样:

import * as moment from 'moment'; 

Which works fine for the main gulp project assembling task that relies on tsify plugin.: 哪个适用于依赖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));
});

But for the compiling of the tests I decided to use task employing tsProject(): 但是为了编译测试,我决定使用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`));
});

Which leads to the following error: 这导致以下错误:

date-range-picker/date-range-picker.component.ts(2,25): error TS2307: Cannot find module 'moment'. date-range-picker / date-range-picker.component.ts(2,25):错误TS2307:找不到模块'时刻'。

What can be done to fix it? 可以做些什么来解决它?

Finally, adding moduleResolution: "node" to compilerOptions solved my problem. 最后,添加moduleResolution: "node"compilerOptions解决了我的问题。 So in the end the following tsconfig.json configuration is employed: 所以最后使用了以下tsconfig.json配置:

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

Here is the link one can use to learn more about typescript module resolution approaches. 以下是可用于了解有关打字稿模块解析方法的更多信息的链接

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

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