Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Here is my tsconfig.json:

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

Inside my date-range-picker.component.ts. I'm importing moment lib, as was proposed in the main documentation:

import * as moment from 'moment'; 

Which works fine for the main gulp project assembling task that relies on 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 for the compiling of the tests I decided to use task employing 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'.

What can be done to fix it?

like image 620
Elias Avatar asked Jul 06 '17 08:07

Elias


People also ask

Can not find module in angular?

To solve the error "Cannot find module '@angular/core'", make sure you have installed all dependencies by running the npm install command, set the baseUrl option to src in your tsconfig. json file and restart your IDE and development server. Copied!

Can not find module or its corresponding type declarations?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.


1 Answers

Finally, adding moduleResolution: "node" to compilerOptions solved my problem. So in the end the following tsconfig.json configuration is employed:

{
  "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.

like image 76
Elias Avatar answered Nov 05 '22 12:11

Elias