What is the process for importing a TypeScript module from the local directory?

I am currently working on a TypeScript module with plans to eventually release it on NPM. However, before publishing, I want to import the module into another project hosted locally for testing purposes. Both projects are written in TypeScript.

The TypeScript module successfully compiles using tsc and generates the appropriate files in the dist directory. Additionally, the module includes a storybook that is able to import and compile the module without any issues using relative paths.

In my local host project, I have attempted to import the module using both npm link and by running npm install ../localmodule.

Here is the tsconfig configuration for the module:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "jsx": "preserve",
    "declaration": true,
    "declarationMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "noImplicitAny": true,
    "typeRoots": ["./src/types"],
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

The module's package.json file contains:

  "main": "dist/index.js",
  "types": "dist/index.d.ts",

However, when attempting to compile the host project with the imported local module, I encounter numerous errors that were not present during the development or while utilizing the storybook.

  1. An important error points out that the host project cannot locate one of the module's exports.
<e> ERROR in ../react-regl/dist/index.js 9:18-51
<e> Module not found: Error: Can't resolve './components/ReglFrame' in '/Users/kevzettler/code/react-regl/dist'
<e>  @ ./src/Ground.ts 6:0-30 11:11-21 12:15-27 20:26-30 31:13-17
<e>  @ ./src/RenderStore.ts 24:0-48 271:12-24 292:12-18 488:28-40 497:28-34
<e>  @ ./src/ClientStore.ts 18:0-40 74:32-43
<e>  @ ./src/browser.worker.ts 1:0-40 10:20-31
<e>  @ ./src/index.ts 83:21-77

I can confirm that this export exists in the dist directory of the module:

kevs-mbp:react-regl kevzettler$ ls dist/components/
ReglFrame.d.ts      ReglFrame.d.ts.map  ReglFrame.jsx
  1. In addition, there are type errors indicating differences in type inference between the host project and the storybook.
<e> ERROR in /Users/kevzettler/code/hypeworks/src/Ground.ts
<e> ./src/Ground.ts
<e> [tsl] ERROR in /Users/kevzettler/code/hypeworks/src/Ground.ts(17,17)
<e>       TS2339: Property 'clear' does not exist on type 'ReactRegl'.
<e>  @ ./src/RenderStore.ts 24:0-48 271:12-24 292:12-18 488:28-40 497:28-34
<e>  @ ./src/ClientStore.ts 18:0-40 74:32-43
<e>  @ ./src/browser.worker.ts 1:0-40 10:20-31
<e>  @ ./src/index.ts 83:21-77
<e>
<e> ERROR in /Users/kevzettler/code/hypeworks/src/Ground.ts
<e> ./src/Ground.ts
<e> [tsl] ERROR in /Users/kevzettler/code/hypeworks/src/Ground.ts(19,21)
<e>       TS2339: Property 'texture' does not exist on type 'ReactRegl'.
<e>  @ ./src/RenderStore.ts 24:0-48 271:12-24 292:12-18 488:28-40 497:28-34
<e>  @ ./src/ClientStore.ts 18:0-40 74:32-43
<e>  @ ./src/browser.worker.ts 1:0-40 10:20-31
<e>  @ ./src/index.ts 83:21-77

I have verified that these methods, such as clear and texture, are defined in the .d.ts files from the module.

The tsconfig settings for the host project are as follows:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "esnext",
    "target": "es5",
    "jsx": "react",
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "useDefineForClassFields": true,
    "lib": [
      "es2019",
      "dom"
    ],
    "exclude": [
      "./src/server.worker.ts",
      "./src/server.ts",
      "./src/network/ServerNetwork.ts",
      "./src/Worker/implementation.worker_threads.ts"
    ]
  }
}

Answer №1

Assuming that webpack is being used for compilation in your main project, it is important to include the extension .jsx in the resolve.extensions section of your webpack configuration file. This allows your project to recognize modules with the specified extensions. Below is a sample webpack.config.js file demonstrating how to use resolve.extensions with .js, .jsx, and .json.

module.exports = {
  // other configurations here...
  resolve: {
    extensions: ['.js', '.jsx', '.json']
  }
};

Answer №2

Create a new file and save it with the extension {{name}}.d.ts

global.d.ts

declare module "your-module-name";

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

The error message "The specified property 'bin' does not exist in the package.json file" is displayed when using the node

Currently, I am utilizing PKG to construct a program by using package.json. Within the package.json file, I have included the "pkg" property. However, whenever I attempt to run PKG, it consistently returns the following error message: Error! Property &apos ...

Simultaneous asynchronous access to a single object

I have been working with JS/TS for quite some time now, but the concept of race conditions still perplexes me. I am currently attempting to execute the following logic: class Deployer { protected txDataList: Array<TXData>; constructor() { this ...

What steps should I take to enable TypeScript IntelliSense to recommend correct suggestions within discriminated unions?

I am working on creating a checkbox UI component based on a design in Figma. The outline variant is specified to only be compatible with the large size, while the solid variant can be used with all sizes. As a result, I am trying to build an interface whe ...

Encountered an issue with a module not being found while trying to install a published React component library that is built using Rollup. The error message states:

In my latest project, I have developed a React component library called '@b/b-core' and used Rollup for building and publishing to the repository. When trying to install the library in a React app, an issue arises where it shows Module not found: ...

Jetstream Livewire App does not contain the Tailwind framework

Recently, I set up a Laravel Jetstream Livewire application and successfully hosted it on a VPS managed by Plesk (not locally). Here are the steps I followed to achieve this: Navigate into an empty subdomain folder and execute the command: composer crea ...

The value specified for configuration.output.path, "./", is not a valid absolute path

Encountered an error: Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema. - configuration.output.path: The provided value "./" is not an absolute path! Error occurred while installi ...

Is it possible to utilize types as constants in a switch statement?

In my file called checkoutTypes.ts, I have defined some checkout types like this: export type CheckoutInvoiceAddressSection = "InvoiceAddress"; export type CheckoutDeliveryAddressSection = "DeliveryAddress"; export type CheckoutDelivery ...

Tips on preventing image previews from consuming too much text data while updating a database in Angular 12 using Material UI for image uploads within a FormGroup object

Currently working with Angular 12 and Angular Material for image uploads with preview. I have a formgroup object below, but I'm running into issues with the 197kb image preview text being inserted into the database. Despite trying setValue/patchValue/ ...

Is it feasible to mock a defined function in Typescript for a unit test scenario?

Currently, I am working on typescript code that compiles into javascript, gets bundled with rollup, and is utilized by a framework. This framework exposes a library to me in the global scope, taking the form of a function: fun({ prop1: number, ...

The package.json file is being overlooked by NPM when it comes to the jQuery

Something seems to be off when I try to run 'npm install' in my Angular 4 project. It's not recognizing the jQuery version specified in my packages.json file and instead installing version 1.7.4. I specifically have 2.2.4 mentioned in packa ...

Prisma causing a compiler error

Currently, I am in the process of developing a project that integrates a GraphQL server and utilizes Prisma to establish a connection with the database. Additionally, I have chosen to implement this project using TypeScript. Unfortunately, as I compile my ...

Sometimes the downloaded xlsx file may become corrupted

Currently, I am working on developing a project using Angular4 with Typescript. One of the tasks involved creating a blob utilizing the XLSX-populate library. Below is an example showing the code snippet for generating a valid xlsx object: var url = wind ...

Angular Material 2: Sidenav does not come with a backdrop

I'm encountering an issue with the SideNav component while developing a website using Angular 2. The SideNav has 3 modes, none of which seem to affect what happens when I open it. I am trying to make the backdrop display after opening. Even though t ...

Executing npm commands within a complex folder hierarchy

I am facing an issue where I can only execute my npm scripts like npm run dev or npm run build when my folder is placed on the Desktop. However, when I try to move the folder into my directory structure, it throws the following error: npm run dev > &l ...

When attempting to import css-animator in Angular2/Typescript, a 404 error is displayed, indicating that the

Recently, I delved into the world of Angular2 and decided to experiment with some animations using css-animator package.json "dependencies": { "@angular/common": "2.0.0-rc.3", "@angular/compiler": "2.0.0-rc.3", "@angular/core": "2.0.0-rc.3", ...

Utilizing Angular's global interceptor functionality can streamline the process

Having trouble making 2 interceptors (httpInterceptorProviders, jwtInterceptorProviders) work globally in my lazy modules. I have a CoreModule and X number of lazy-loaded modules. Interestingly, autogenerated code by the Swagger generator (HTTP services) g ...

NodeJS Google Cloud Trace for packaged applications

My server-side rendered web application had no issues sending tracing information to Google using express and @google-cloud/trace-agent. However, after bundling our application, all trace information disappeared. While we can still see the requests in the ...

Developing a custom React component library using Webpack and Typescript, however encountering issues with Webpack consistently bundling React

I'm currently in the process of developing an external component library using Webpack and TypeScript. Although it compiles without any issues, I encounter an error when attempting to use the library: Invalid hook call. Hooks can only be called ins ...

"Having access to the source code of third-party JavaScript libraries can greatly aid in the debugging

Is it possible to access the source code of third-party JavaScript libraries for debugging purposes? I work with npm/nodejs and the angular CLI, which uses Webpack. The libraries I am interested in having access to during debugging are: Angular 2 (Type ...

Sam's attempt to build ended in failure as a result of the following error: Build Failed Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR!

Currently, I am in the process of learning how to build lambdas on AWS. For guidance, I am utilizing this official tutorial. Here are the commands that I have executed: sam init (using default values for everything and selecting template 8 - webapp backe ...