TypeScript + moment.js: error TS2307: cannot find module 'moment'

I am developing a web application using angular 1.5, typescript 2.4.0, moment: 2.18.1 and gulp to build the project.

Here is mine tsconfig.json:

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

Inside of mine date-range-picker.component.ts. I import the lib moment as suggested in the main documentation :

import * as moment from 'moment'; 

Which is great for the main build task of the gulp project, which depends on the tsify plugin .:

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 to compile the tests, I decided to use the task using 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`));
});

This results in the following error:

date-range-picker / date-range-picker.component.ts (2,25): error TS2307: Cannot find the moment module.

What can be done to fix this?

+4
1

, moduleResolution: "node" compilerOptions . , tsconfig.json:

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

, typescript.

+4

Source: https://habr.com/ru/post/1680862/


All Articles