The Typescript Compiler struggles to locate certain npm packages, such as gulp-typescript

When using gulp-typescript to compile my TypeScript files, the issue arises where external modules such as "@angular/core" are not found. How can I configure TSC to search for these modules in the node_modules folder?

Additionally, I would like to bundle the TypeScript files with systemJS. Is there a way to include the systemjs-config so that TSC knows where to look?

CODE:

gulp.task('tsc:release_dist', function() {
    return gulp.src(releaseBuildDest+'/**/*.ts')
        .pipe(gulpIgnore('node_modules/**'))
        .pipe(debug())
        .pipe(tsc({
            noImplicitAny: true,
            out: 'bundle.js',
            module: 'system'
        }))
        .pipe(gulp.dest(releaseBuildDest));
});

My project structure:

.dist/
..../release/
......./app/ (Angular2 components, ...)
......./assets/ (CSS, img, ...)
......./node_modules/ (Angular2 and other dependencies)
.......index.html
.......main.ts
.......systemjs.config.js

Console output:

[12:25:59] Using gulpfile C:\Develop\frontend\gulpfile.js
[12:25:59] Starting 'tsc:release_dist'...
[12:26:00] gulp-debug: dist\release\main.ts
[12:26:03] gulp-debug: dist\release\app\app.component.spec.ts
[12:26:05] gulp-debug: dist\release\app\app.component.ts
[12:26:05] gulp-debug: dist\release\app\app.module.ts
[12:26:05] gulp-debug: dist\release\app\components\afafc\authafafc\authafafc.component.ts
[12:26:05] gulp-debug: dist\release\app\components\afafc\langafafc\langafafc.component.ts
[12:26:06] gulp-debug: 6 items
dist\release\app\app.component.spec.ts(3,50): error TS2307: Cannot find module '@angular/core/testing'.
dist\release\app\app.component.spec.ts(4,20): error TS2307: Cannot find module '@angular/platform-browser'.
dist\release\app\app.component.spec.ts(5,30): error TS2307: Cannot find module '@angular/core'.
dist\release\app\app.component.ts(1,27): error TS2307: Cannot find module '@angular/core'.
dist\release\app\app.module.ts(2,31): error TS2307: Cannot find module '@angular/platform-browser'.
dist\release\app\components\afafc\authafafc\authafafc.component.ts(1,34): error TS2307: Cannot find module '@angular/core'.
dist\release\app\components\afafc\langafafc\langafafc.component.ts(1,34): error TS2307: Cannot find module '@angular/core'.
dist\release\main.ts(1,40): error TS2307: Cannot find module '@angular/platform-browser-dynamic'.

How my components are requiring these packages:

import { NgModule } from '@angular/core';

tsconfig.js

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

Additional information: Everything works fine when I compile the regular dev build using "npm start", which is a shortcut to tsc -p src/. However, I'm encountering errors when trying to build a "release build" gulp-task with bundled files. When I copy everything to my release build directory and try to compile with tsc, the errors mentioned above occur.

package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "my-project",
  ... (remaining content unchanged)
}

Answer №1

Consider configuring your gulp typescript setup to utilize the tsconfig details specific to your project. This could potentially enhance the performance. Here's a sample configuration that has been effective for me. Feel free to eliminate any sourcemaps-related sections if they are unnecessary.

var ts = require('gulp-typescript');
var gulp = require('gulp');
var sourcemaps = require('gulp-sourcemaps');

gulp.task('build.js.dev', () => 
{
    var tsProject = ts.createProject('tsconfig.json');

    var tsResult = tsProject.src()
        .pipe(sourcemaps.init())   
        .pipe(tsProject()); 

        //Write definitions 
        //tsResult.dts.pipe(gulp.dest(TEMP_TARGET_FOLDER)),
        //Write compiled js
    return tsResult.js.pipe(sourcemaps.write(
            ".", 
            { 
                includeContent: true, 
                sourceRoot: __dirname + "/dist"
            })).pipe(gulp.dest(TEMP_TARGET_FOLDER));
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

updating firebase data using Angular

Currently, I am attempting to insert a new object into my Firebase database export class AppComponent { courses$: AngularFireList<any[]>; course$;ang author$ constructor(db: AngularFireDatabase) { this.courses$ = db.list('/courses ...

What is the best way to arrange a list in Angular2 using AngularFire2's FirebaseListObservable?

Looking to retrieve all users from a Firebase realtime database and organize them based on a score property. I managed to achieve this by using the variable users: FirebaseListObservable<any[]>; however, encountered the following errors: Type & ...

Postman issue: Your username and password combination is incorrect within the MEAN stack environment

I am new to mean stack development and facing some issues. When I try to run "api/users/login" in Postman, it shows an error saying "Username or password is invalid!". Additionally, when attempting to register using "register/users/register", it gives a me ...

When using firebase serve, typescript code is not being compiled prior to initiating the server

My typescript firebase function project is simple yet the code works fine. However, there seems to be an issue in the project configuration that causes firebase serve NOT to recompile the code before starting the server. On the contrary, firebase deploy wo ...

IssueTS1128Compilation: Syntax error, declaration or statement is missing in Angular 2

After updating my ASP.NET Core 1.0 dependency to ASP.NET Core 1.1 using the NuGet manager, I have encountered an error: Error TS1128 Build:Declaration or statement expected. Upon researching, I discovered that the issue lies in me using an outdated vers ...

The text editor within the NG nickname terminal

After purchasing a new Windows computer, I encountered an issue while attempting to install Angular. The ng commands were not functioning as expected and instead opened a file in the editor. Upon researching a solution at this source, it was suggested tha ...

Error: Trying to access the `push` property of an undefined value is causing an issue

Issues with using the push method in TypeScript have arisen. Here are the details: Below is the code snippet for the reservation modal component: guestArray=[1, 2, 3, 4, 5, 6]; guests: number; isDateTime: boolean = false; constructor(private params: Mo ...

Typescript issue: "The property 'webPreferences' is causing an expected type error, as declared in type 'BrowserWindowConstructorOptions'"

Struggling to get unredacter by bishop fox up and running. Despite my best efforts, I can't seem to compile the code. I tried debugging it in VS Code, but since I only have a basic knowledge of HTML and no experience with TypeScript or JavaScript, I&a ...

Using boolean value as default input value in React

When trying to set the default value of a controlled checkbox input from my request, I encountered an error stating "Type 'boolean' is not assignable to type 'string | number | readonly string[] | undefined'". Do you have any suggestion ...

Utilizing real-time communication in a microservices environment through websocket integration

I'm facing a challenging exercise that I've been handling quite well until now. The only remaining task is to integrate WebSockets into the mix. The project involves a simple voting app focused on two topics, with specific technologies designate ...

Testing React components within a controlled environment using cypress-react-unit-test

I'm currently facing a challenge in comprehending how to modify the props of a react component while utilizing cypress-react-unit-test. Below is a straightforward controlled input component: interface MyInputProps { inputVal: string onInputCh ...

Linking a button to a (click) event within HTML content fetched from the backend

Hey there, I'm facing a scenario where I have an angular service that sends a HTTP request to the backend for some HTML code. Once the HTML is received, I'm inserting it into the component's HTML using <div [innerHTML]="..."/>. The iss ...

Storing data from a collection of interface objects in a string array

Take a look at the following code snippet: import React, {FC} from 'react'; import {useFetchErrors} from "../Api/Api"; import {useLocation} from "react-router-dom"; interface ExecutionTableProps { project_id: number } const ...

Having trouble updating state with useEffect in a React functional component

Currently, I am dealing with a React functional component that is calling an API to fetch data. The response from the API call is confirmed to be received successfully. My aim is to store this data in an array within the component's state so that it c ...

What circumstances warrant the utilization of an Angular service?

My understanding is that services are utilized for inter and intra component communication to abstract complex data structures. Is it accurate to say that services are exclusively used for persistent data structures? In what scenarios should we avoid usi ...

What is the process of registering custom pipes in Angular 17?

Recently, I developed a custom Pipe and usually I would register it in app.module.ts. However, with Angular 17, we no longer have that file. Can someone please guide me on how to register it now? Thanks in advance. I attempted to import it into the import ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

Manage the recently implemented features with TypeScript in IntelliJ

When using IntelliJ with TypeScript and referencing a new function in an .html file, IntelliJ has the option to automatically add this function to the corresponding .component.ts file. For example: <div *ngIf="ifConditionIsTrue()"> Intell ...

Creating Typescript type definitions for any object properties

Struggling to find the correct TS syntax with Typescript 3.7.3. I have a random object, for example: var obj = { one: ..., two: ... three: ... }; I want to create a type that includes all keys from that object, like this: type ObjKeys = &ap ...

Can the Rxjs library's Observables of() function be used to output multiple values?

I am inquiring about this because I came across in the Angular documentation that of(HEROES) returns an Observable<Hero[]> which emits a single value - an array of mock heroes. If I cannot use of(), do you have any alternative suggestions for me? I ...