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?
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!
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With