Error message occurs when using Typescript with es2017 target, indicating a potential need for a loader despite successful project builds in es5

We are working on a typescript react 'main' project. In the package.json file, there is a dependency on another internal library that we utilize. The 'main' project builds successfully when both projects are set to target es5. However, changing to es2017 results in the following error:

You may need an appropriate loader to handle this file type, as currently no loaders are configured to process this file.

This error occurs for all files that declare enums in the dependent project.

Here is the tsconfig.json configuration for the main project:

{
  "compilerOptions": {
    "resolveJsonModule": true,
    "noImplicitAny": false,
    "module": "commonjs",
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es2017",
    "jsx": "react",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "downlevelIteration": true,
    "types": ["node", "jest"],
    "baseUrl": "./src"
  },
  "exclude": ["node_modules"],
  "includes": ["./src"]
}

And here is the webpack.config.js settings:

// webpack configurations here...

This is the babel.config.json setup:

// babel configurations here...

The issue arises when trying to target es2017, and one solution was found by changing the package json to point to the local copy of the dependent repository ('models').

For more information, refer to the IMPORTANT UPDATE section above.

Additionally, be aware of the 2nd UPDATE regarding the preset-react setting in babel.config.json causing issues with enum declarations.

Answer №1

I found the solution through 2 key discoveries. Firstly:

Upon examining the babel.config.json file, I noticed:

"@babel/preset-react"

This line instructed babel to "remove typescript types", as explained here. By removing it, the errors were resolved, but this meant that all types were eliminated (resulting in babel not being able to process JSX).

Realizing that the enum files are ambient, and that babel-loader, particularly preset-react, was unable to handle them (unlike tsc), was a breakthrough.

This realization led me to a helpful Stack Overflow question, where I learned that I needed to explicitly specify the location of the enum files for babel-loader by adding the following code snippet to my package.json:

  {
    test: /\.(ts|tsx)$/,
    include: path.resolve('./node_modules/@tplp/models/src/enumerations'),
    use: [
      {
        loader: 'babel-loader',
        options: {
          cacheDirectory: true,
        },
      },
    ]
  },

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

Can you explain the purpose of the GenericType parameter in a TypeScript promise declaration for me?

I am curious about whether the generic type in Typescript's Promise<GenericType> definition indicates the type of the object passed to the then handler. As an example, consider the following code: const pr:Promise<Array<Number>> ...

Should I use Object.assign or define class properties?

Currently in the process of developing an angular application that interacts with the twitch API. The API returns data in various formats, some of which I need to parse and save into specific classes. My main concern is understanding the potential drawbac ...

Retrieve a specific number from an equation

With my limited knowledge of TypeScript, I am attempting to extract a specific number from an expression. The goal is to locate and retrieve the digit from the following expression. ID:jv.link.weight:234231 In the given string, I aim to extract the numb ...

Triggering ngSubmit function when button is clicked inside an Ionic alert

My ionic app is up and running, utilizing a template driven form in Angular to gather user input. I'm using ngSubmit to pass this data to my ts.file. My challenge lies in triggering the ngSubmit function through a 'No and save data' button w ...

Using Google OAuth2Client with Angular 4

I am encountering an issue while trying to verify the ID token for my client using Google's example. You can find the example code here. const {OAuth2Client} = require('google-auth-library'); // <-- facing issues here const client = new ...

Implementing CSS Pre-loading in an Angular Application with Webpack and Heroku

My current setup involves an Angular application (v4.0.1) with webpack deployed on Heroku. I have a loading spinner in place to show while the app is loading, and it works well locally. However, when deployed on Heroku, the loading spinner (specifically th ...

"Adjusting the position of an Ionic Menu on-the-fly

As I strive to update the Ionic 3 Menu side dynamically when the user changes the language, a challenge arises for RTL languages where the menu needs to be on the right instead of the default left. To tackle this issue, I have subscribed to the TranslateS ...

Where should an Angular application begin when it has distinct files for controllers, services, and other components?

My angular application is structured with separate files for: UI router and initialization Controllers Services Directives Prior to using webpack, I had the router load first followed by the individual component files. However, the other files d ...

Combining nested Observables within an outer array without using inner subscribe (RxJS)

Looking at the TypeScript functions below, which are used for async HTTP-Calls: public retrieveAllMembersIdsFromGroup(groupId: string): Observable<string[]> public retrieveMember(memberId: string): Observable<Member> How can these be combined ...

Improving the structure of a TypeScript switch statement

Looking for a more efficient way to implement a switch statement in TypeScript? Here is the current code snippet from a component: switch (actionType) { case Type.Cancel { this.cancel(); break; } case Type.Discard { thi ...

The output from the map() function does not accurately represent the data returned by my REST call

Struggling with a REST call in my http.service.ts file while working with Angular version 7 and RxJS. The goal is to extract the "cod" value from a JSON response obtained from a REST call to the openweather API. However, when I add map() to extract the des ...

Encountered an Error While Compiling GeoJson File in Vue

I'm a beginner with Vue.js and I am trying to create a GIS application using Webpack+Vue with a GeoJSON file as the basemap. However, when I attempt to compile it, I encounter the following error message: Module parse failed: Unexpected token (2:6) ...

An uncaught exception has occurred: An error was encountered indicating that the specified path is not valid for either posix or windows systems, and it appears that there is no 'join' method defined in the

I am currently working with nextjs version 13.5.6 using the app router and app directory. This issue arises during the compilation of the route app/(home)/page.js. The folder and file structure within the app folder is as follows: app/ -(home)/page.js -ser ...

Typescript compilation fails to include require statements in certain files

Currently, I am in the process of converting a Node.js project to TypeScript. The first two main files of the Node project transpiled correctly, but the third file ran into issues with the requires statement at the top for pulling in dependencies. Despite ...

Tips on how to effectively unit test error scenarios when creating a DOM element using Angular

I designed a feature to insert a canonical tag. Here is the code for the feature: createLinkForCanonicalURL(tagData) { try { if (!tagData) { return; } const link: HTMLLinkElement = this.dom.createElement('link'); ...

I am having trouble establishing a connection between two containers on Heroku

My web application is built using Node.js and MongoDB. After containerizing it with Docker, everything worked fine locally. However, when I tried to deploy it to production, I encountered an issue where the backend could not establish a connection with the ...

Use the ngFor directive to iterate over the most recently created array from the parent ng

I am looking to link material tabs with ngFor to generate arrays for child ngFor. Let's start from the beginning: <mat-tab-group> <mat-tab *ngFor="let tab of asyncTabs "> <ng-template mat-tab-label>{{tab.label}}</ng-template ...

Angular - Is there a specific type for the @HostListener event that listens for scrolling on the window?

Encountering certain errors here: 'e.target' is possibly 'null'. Property 'scrollingElement' does not exist on type 'EventTarget'. What should be the designated type for the event parameter in the function onWindow ...

Here's how to retrieve a property from a union type object in Typescript without the need for type casting

I am facing a scenario with various types: export type a = { todo: string; }; export type b = { id: number; }; export type TodosAction = Action<string> & (a | b); In addition, I have a function defined as follows: function doSmth(action:To ...

Encountering a module resolve error when a tsx file is added to the project item group

After setting up an asp.net core project with a react template and configuring Typescript, I created a simple file named Test.tsx with the following code: import React from 'react'; class Test extends React.Component { render() { r ...