The local types package cannot be built by react-scripts even though tsc has successfully completed the process

I have developed an application with a TypeScript frontend and backend. To streamline the process, I decided to create a shared types module that contains all the necessary types for both components in one centralized location. Rather than going through the hassle of packaging it and using a registry, my aim is to keep it local and leverage npm link.

The basic structure of my project looks like this:

project/
project/backend
project/frontend
project/my-types

All these components are node modules, with the frontend being an un-ejected create-react-app, involving babel and webpack configurations that interact with TypeScript's tsc.

In the package/my-types/package.json file:

{
  "name": "@types/my-types",
  "version": "1.0.0",
  "type": "module",
  "description": "shared types library for all types among all applications",
  "types": "index.d.ts",
  "scripts": {
    "test": "echo \"no tests for this package\"",
    "build": "npx tsc"
  },
  "devDependencies": {
    "@types/ws": "^8.5.3",
    "pg-promise": "^10.12.1",
    "typescript": "^4.8.4"
  }
}

The project/my-types/tsconfig.json configuration:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": "src",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    ...
  ...
}

The project/backend/tsconfig.json configuration:

{
  "compilerOptions": {
    "types": ["@types/my-types"],
    "allowJs": true,
    ...
  ...
}

The project/frontend/tsconfig.json configuration:

{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    ...
  ...
}

To link the modules, here's what I did:

$ cd project/my-types
$ npm link
$ cd ../backend
$ npm link @types/my-types
$ cd ../frontend
$ npm link @types/my-types

After verification, the folders are structured as follows:

package/frontend/node_modules/@types/my-types
package/backend/node_modules/@types/my-types
my-types/package.json
...

I attempted importing types successfully in the backend:

import {EventPayload} from "my-types/event"
import {Subscriber} from "my-types/db" 

The Main Question

My concerns are:

  1. How can I ensure successful builds in react-scripts for hot-reload and other functionalities?
  2. Is it possible to set outDir: "dist" in my-types/tsconfig.json while maintaining imports like:
import {Thing} from "my-types/scores"
  1. Should the types module be named @types/my-types or is it more practical to name it simply my-types considering it won't be placed in a registry?
...

Answer №1

I've encountered a similar situation where I have a server running on ts-node and a React application that requires access to a shared library. Previously, I would manually copy the common directory into my React /src directory so that the server could utilize it.

  1. I recently found a solution using craco and react-app-alias. With Craco, it even updates my development build automatically when saving changes to the shared TypeScript files.

Below are the configurations I used:

// client/craco.config.js
const { CracoAliasPlugin } = require('react-app-alias-ex');

module.exports = {
    plugins: [
        {
            plugin: CracoAliasPlugin,
            options: {
                alias: { 'common': '../common' },
            },
        },
    ],
}
// client/package.json
  "scripts": {
    "start": "craco start"
  },
  "dependencies": {
    "common": "file:../common",
  1. By following this setup, I can import the uncompiled ts files as if they were packaged. For example:
// client/src/components/Pawn.tsx
import { PawnClass } from 'common/pawn'; // Actually ts file
  1. If you only need to import the ts files like in the previous example, there is no need for special directory structures. I now use the same import type syntax for both client-side and server-side imports.

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

I encountered an issue while generating a crypto address on the Waves blockchain using the @waves/waves-crypto library in TypeScript

Encountering an issue with finding "crypto-js" in "@waves/waves-crypto". Despite attempts to uninstall and reinstall the module via npm and importing it using "*wavesCrypto", the error persists within the module's index.d.ts file. I am attempting to ...

The contents table remains fixed in the top right corner as you scroll

I have developed an Angular app with a table-of-contents component that only displays two items. The code for the script is as follows: ts import { Component, OnInit } from '@angular/core'; import { pdfDefaultOptions } from 'ngx-extended-p ...

Unable to load connected modules from npm/yarn link within the WSL environment

Trying to import a local dependency into my project is proving to be quite challenging. The situation at hand involves a project named 'test_project' and another one called 'test_module'. Linking test module to the global node_modules ...

The error message "TypeError: undefined is not an object (evaluating '_reactNative.Stylesheet.create')" occurred in a React Native environment

I've been working on a project in React Native and have successfully installed all the necessary dependencies. However, upon running the code, I encounter the following error message: TypeError: undefined is not an object (evaluating '_reactNativ ...

How to start Angular2 prototype with an object literal

export abstract class GridColumn { public field?: string; public sortField?: string; public header?: string; public footer?: string; public sortable?: any = true; public editable?: boolean = false; public filter?: boolean = true ...

Is there a way to extract a specific item from a ListView by tapping on it in Nativescript?

Attempting to retrieve data from a tap event using angular2 + typescript: This is the html code for the component: <RadListView row="1" [items]="groceryList" [class.visible]="listLoaded" (tap)="seeItem($event)" swipeActions="true" (itemSwipeProgr ...

Type Vue does not contain the specified property

I am encountering an issue where I am using ref to retrieve a value, but I keep receiving the error message "Property 'value' does not exist on type 'Vue'". Below is the code snippet causing the problem: confirmPasswordRules: [ ...

What is the best way to pass a variable from a class and function to another component in an Angular application?

One of the components in my project is called flow.component.ts and here is a snippet of the code: var rsi_result: number[]; @Component({ selector: 'flow-home', templateUrl: './flow.component.html', styleUrls: ['./flow.comp ...

Avoid automatic restart of NestJS server upon modifying specific directories

When running my application in watch mode using the nest start --watch command, the server automatically restarts whenever a source file is changed. While this behavior is expected, I am looking for a way to exclude certain directories or files from trigg ...

Ionic 2 encountering issue with `ctorParameters.map` not being a function error

Recently, I wanted to incorporate the Network-information plugin into my project but encountered compatibility issues with an older version of Ionic-native. To resolve this, I took the following steps: npm rm --save ionic native npm install --save ionic-n ...

Challenges encountered when assigning values in a form with Material UI, Formik, and Typescript

When attempting to set the 'role' and 'active' values on a form, I encountered a couple of issues. The first problem arises from the fact that the selectors' original values are not being properly set. These values are fetched in ...

Is it necessary for the package.json file to have a distinct name for each website when utilized in local development?

As I embark on my journey with npm, I have successfully set up a test site on my local development server. My next step involves installing new projects in a different local directory that have the same dependencies as the test site. This can be achieved b ...

Tips for injecting a service into a class (not a component)

Can you inject a service into a class that is not a component? Consider the following scenario: Myservice import {Injectable} from '@angular/core'; @Injectable() export class myService { dosomething() { // implementation } } MyClass im ...

Can anyone suggest a substitution for npm audit that involves yarn?

Looking for a way to pin resolutions using yarn while also being able to conduct an audit with npm audit? Are there any alternatives in yarn to npm audit? Alternatively, will pinning resolutions of dependencies of dependencies work in npm? ...

After upgrading to version 8 of the Firebase JS SDK, the "firestore" module (imported as "firebase") could not be located within the "firebase" package

After upgrading the firebase JS SDK from v7 to v8.0.0, I changed my import of firebase as shown below. import * as firebase from 'firebase'; However, attempting to access certain properties resulted in an error message: firebase.firestore.FieldV ...

Data is not displaying correctly in the Angular Material Table

I'm currently trying to build a mat table based on an online tutorial, but I'm facing a problem where the table appears empty even though I have hard coded data. As someone new to Angular and HTML/CSS, I'm struggling to understand why the ma ...

Guide to automatically installing @types for all node modules

As a newcomer to Typescript and NodeJs, I have been experiencing errors when mentioning node modules in my package.json file and trying to import them. The error messages I always encounter are as follows: Could not find a declaration file for module &apos ...

I am encountering an issue with my code where the function this.ProductDataService.getAllProducts is not recognized when

Encountering an issue while running unit test cases with jasmine-karma in Angular 7. The error received is: ProjectManagementComponent should use the ProjectList from the service TypeError: this.ProjectManagementService.getProject is not a function If I ...

Unfulfilled Peer Dependency in react

Encountering javascript issues with react. Chrome error message when rendering page: Uncaught TypeError: Super expression must either be null or a function, not undefined at _inherits (application.js:16301) at application.js:16310 at Object.232.prop-types ...

Is it possible for me to make the default export anonymous?

Why bother naming the export if you already have a file with the default export name? This seems redundant and goes against the DRY principle. While there is a rule that discourages anonymous default exports, how can we enforce an error when someone does ...