`How can create-react-app with typescript be set up to automatically utilize ts-loader for node modules written in typescript?`

Currently, I am working on a project named client which relies on components and services from the core project. The core project, written in TypeScript, has not been compiled or built yet. To speed up the development of the client application, I need to work on both the core and client simultaneously. To achieve this, I created an npm link from the client to the core. However, when I attempt to utilize the core code, I encounter an error message.

Module parse failed: Unexpected token (7:34)
You may need an appropriate loader to handle this file type, as currently no loaders are configured to process this file. Check out https://webpack.js.org/concepts#loaders for more information.

I suspect that webpack is not using the correct loader to build the TypeScript node module. Is there a solution to this issue without having to eject the configuration files?

Answer №1

In the typescript version of create-react-app, babel-loader is used to convert typescript into javascript. The issue you are facing lies here:

test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),

It only compiles ts|tsx files in the paths.appSrc folder, the main src folder, while your linked module is located outside of this directory. To resolve this, you can adjust the include rule or remove it entirely, but this may require ejecting.

If you prefer not to eject, you can add a script to your build process that modifies the webpack.config.js file in

node_modules\react-scripts\config
after running npm install, although this is considered bad practice.

Here is an example of the script I am using:

import * as fs from 'fs';

export function deleteLineFromFile(props: { path: string; lineToRemove: { index: number; value: string } }) {
  const data = fs.readFileSync(props.path, 'utf-8');
  const array = data.split('\n');
  const value = array[props.lineToRemove.index - 1].trim();

  if (value === props.lineToRemove.value) {
    array.splice(props.lineToRemove.index - 1, 1);
    const newData = array.join('\n');
    fs.writeFileSync(props.path, newData, 'utf-8');
  }
}

deleteLineFromFile({
  path: 'node_modules/react-scripts/config/webpack.config.js',
  lineToRemove: { index: 384, value: 'include: paths.appSrc,' },
});

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

Angular template error: Potential is present but not defined

Encountering an issue with my Angular application's template file (dashboard.component.html). The error message reads "Object is possibly 'undefined'." Here's the portion of the template that seems to be causing the problem: <div> ...

What is the best way to retrieve the instance variable of a component from a handler method in Angular, specifically using the Razorpay API

I am working on integrating Razorpay into my Angular application. I am trying to retrieve the value of the days variable from the handler function, but I am getting an error stating that the property days does not exist. This is a reference to the option ...

What is the best way to handle typing arguments with different object types in TypeScript?

Currently, I have a function that generates input fields dynamically based on data received from the backend. To ensure proper typing, I've defined interfaces for each type of input field: interface TPField { // CRM id as a hash. id: string nam ...

The ReactJS Material Table Tree mode experiences delays when new row data is introduced

My Reactjs app utilizes the material-table widget within a component: render() { return ( <div> <Row> <MaterialTable title="Mon équipement" style={{ width: "100%", margin: "0%" }} ...

Error occurred while trying to find binding file following electron version update

I am currently in the process of updating my project from electron version 1.2.5 to the latest electron version, 1.7.7 (at least that was the version I downloaded). My node version is 6.9.1. Upon starting my project with the new electron version, I encoun ...

Incorporating HTML code within a .ts file: a basic guide

I'm relatively new to Angular, but I've been given a project that's built with Angular. As I examine the .ts file containing the list of property types, I need to wrap a span around the label text. Is this doable? Here is the current list ...

Dynamic data manipulation with Angular ReactiveForms

One of the challenges I am facing involves using formArray for my list of products. Specifically, I am trying to access the value of product_code in my .ts file similar to [ngModel] so that I can manipulate the data accordingly. Can anyone provide guidance ...

Tips for creating a window closing event handler in Angular 2

Can someone guide me on how to create a window closing event handler in Angular 2, specifically for closing and not refreshing the page? I am unable to use window.onBeforeunLoad(); method. ...

ConcatMap in RxJS processes only the last item in the queue

Currently, I am implementing NGRX with RXJS. Within the effects layer, I am utilizing a concatMap to organize my requests in a queue fashion. However, once the latest request is finished, I aim to execute the most recent item added to the queue instead of ...

"What is the most effective way to utilize and integrate the `setValue` function from react-hook-form within a custom react hook

Struggling to pass setValue to a react hook? In my custom react hook, I need to set values in a form using react-hook-form's setValue. Yet, after migrating from v6 to v7, I'm having trouble figuring out the proper typing for this. This is how t ...

Need a specialized package for your project?

Attempting to add an npm package from a custom registry via npm during a project's build step in TeamCity. Created a build step with the following configurations: Runner type: Command Line Step name: Install Run: Executable with parameters Comman ...

What steps are involved in setting up an npm package so that a global installation will place it in the path?

I am currently working on turning my node module into a global NPM package. By using the -g flag during installation, I want to ensure that it automatically adds a couple of commands to the path. The module in question is ncrypt-stream and it contains two ...

Eliminate the loading screen in Ionic 2

Within my app, there is a button that triggers the opening of WhatsApp and sends a sound. Attached to this button is a method that creates an Ionic loading component when clicked. The issue I am facing lies with the "loading.dismiss()" function. I want the ...

Having trouble locating async function after installation

Today, I encountered a strange issue after installing async globally in Node.js - it reported that the module could not be found. Here is the workflow: Install async npm install -g async Verify async installation npm list -g async Output received: / ...

Tips for incorporating momentjs into TypeScript within AngularJS 1.5

I am looking to integrate the momentJs library into my TypeScript code for Date object operations. However, I need some guidance on how to inject TypeScript in AngularJS, as it differs slightly from JavaScript. angular.module("app") .config(functio ...

Using Typescript in React to render font colors with specific styling

Attempting to utilize a variable to set the font color within a react component, encountering an error with my <span>: Type '{ style: "color:yellow"; }' is not assignable to type 'HTMLProps<HTMLSpanElement>' The use of yel ...

What is the process for connecting a date/time form control?

My code seems to only be working for the 'title' element, while the 'docdatetime' control remains blank. Can anyone spot what I'm doing wrong? //template =================================================== <div class="form-grou ...

Which symbol or character corresponds to the public "get symbol" method?

While going through some Typescript code, I came across a line that is giving me trouble to understand. const symbol = Symbol.for('symbolname'); class Something { public get [symbol]() { return true; } ... } I am confused abou ...

Issue encountered: Syntax error in bootstrap import statement at line 1, column 38 during Rails precompilation process

Upon executing the command: rake assets:precompile RUBY_ENV=production or when it automatically precompiles for deployment, an error message is encountered: /usr/share/rvm/rubies/ruby-2.7.4/lib/ruby/2.7.0/net/protocol.rb:66: warning: already initialized ...

Guide on importing videojs-offset library

I am working on a component that utilizes video.js and HLS streaming in Angular. The component code is as follows: import { Component, ElementRef, AfterViewInit, ViewChild, Input, EventEmitter, Output } from '@angular/core'; import ...