Module or its corresponding type declarations not found in the specified location.ts(2307)

After creating my own npm package at https://www.npmjs.com/package/leon-theme?activeTab=code, I proceeded to set up a basic create-react-app project at https://github.com/leongaban/test-project.

In the src/index.tsx file of my react app, I attempted to import my package using this line of code:

import { LeonTheme } from "leon-theme";

However, upon hovering over the import statement, an error message popped up:

Cannot find module 'leon-theme' or its corresponding type declarations.ts(2307)

tsconfig contents:

{
        "compilerOptions": {
            "module": "ES6",
            "target": "es2016",
            "lib": ["ES2020", "DOM"],
            "jsx": "react",
            "allowJs": true,
            "declaration": true,
            "declarationDir": "types",
            "sourceMap": true,
            "allowSyntheticDefaultImports": true,
            "noEmit": false,
            "outDir": "./dist",
            "moduleResolution": "node",
            "noImplicitAny": false
        },
        "include": ["src/**/*.ts"]
    }
    

package.json details:

{
        "name": "leon-theme",
        "version": "0.0.15",
        "module": "dist/lib/es6/index.js",
        // additional package details...
    }
    

Directory structure of node_modules/leon-theme within the new React app:

https://i.stack.imgur.com/OT5Np.png

Answer №1

After struggling with building and bundling issues, I discovered that using rollup resolved all my problems in creating the necessary files.

Below is the configuration for rollup:

import babel from 'rollup-plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import postcss from 'rollup-plugin-postcss';
import typescript from '@rollup/plugin-typescript';

export default {
    input: 'src/index.ts',
    output: [
        {
            file: 'dist/index.js',
            format: 'cjs',
            sourcemap: true
        },
        {
            file: 'dist/index.esm.js',
            format: 'esm',
            sourcemap: true
        }
    ],
    plugins: [
        peerDepsExternal(),
        postcss({
            inject: true
        }),
        typescript(),
        babel({
            extensions: ['.js', '.jsx', '.ts', '.tsx'],
            exclude: 'node_modules/**'
        }),
        resolve(),
        commonjs()
    ],
};

The tsconfig has also been updated as follows:

{
  "compilerOptions": {
    "outDir": "./dist",
    "module": "es6",
    "target": "es5",
    "lib": [
      "es6",
      "dom"
    ],
    "sourceMap": true,
    "allowJs": false,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDirs": [
      "src"
    ],
    "baseUrl": ".",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "declaration": true
  },
  "include": [
    "src/**/*"
  ]
}

In addition to the above changes, a new bablerc file was required:

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react",
    "@babel/preset-typescript"
  ]
}

Lastly, here are the package details for the library:

{
  "name": "leon-theme",
  "version": "0.0.17",
  "module": "dist/index.js",
  "description": "A simple React style & component library",
  "homepage": "https://github.com/leongaban/leon-theme",
  "bugs": {
    "url": "https://github.com/leongaban/leon-theme/issues"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/leongaban/leon-theme.git"
  },
  "keywords": [
    "javascript",
    "theme",
    "buttons",
    "leon-theme"
  ],
  "scripts": {
    "build": "rm -rf dist/lib && rollup -c"
  },
  "author": "Leon Gaban",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.22.20",
    .... (Truncated for brevity)
  },
  "files": [
    "dist",
    "types"
  ],
  "types": "dist/index.d.ts",
  "peerDependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "typescript": "^5.2.2"
  }
}

https://i.stack.imgur.com/QVci7.png

The previously encountered missing module error has now been resolved: https://i.stack.imgur.com/zbQEK.png

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

Setting up a development environment for .NET Core 2.0 with an Angular template and website template that can be easily downloaded using NPM

Currently, I am embarking on a new project utilizing .NET Core 2.0 with the Angular template. My intention is to integrate an existing HTML template into my project. After some research, I opted for a free HTML template called Gentelella which I have to in ...

Can you demonstrate how to repeatedly increase a running total using a for loop until the loop ends?

Looking at the for loop. The goal is to have the interest calculated based on the latest value variable. Currently, the script generates the same interest rate and value for each year. What I desire is for it to use the previous value variable to determine ...

Personalized parallax design

I am in the process of developing my own custom parallax plugin to allow me to control the direction in which items transition off the screen. However, I am currently facing a challenge in ensuring that regardless of how a user scrolls or the size of the w ...

/usr/bin/env: node: Access denied for nodejs when attempting AWS Code Deploy

After successfully deploying my code to an EC2 instance using AWS Code Deploy and having all the necessary hooks run without issue, I encountered a problem with the following command: /usr/bin/node/bin/forever start /home/ubuntu/codebase/app/bin/www This ...

How to upload a file with JavaScript without using Ajax technology

Is it possible to upload a file using the input tag with type='file' and save it in my project folder without using ajax? Can JavaScript help me achieve this task? Below is the code snippet I am currently working on: function func3() { var ...

Tips for smoothly animating and showing content as the user scrolls to a specific element on the page

Here is a sample template: <template> <div id="Test"> <transition name="fade"> <div class="row" id="RowOne"> <p>Lorem ipsum dolor odit qui sit?</p> </div> ...

How can I position two divs side by side within an Appbar?

I would like the entire Container to be in a single row, with the Typography centered as it already is, and the toggle-container to float to the right <AppBar className={styles.AppBar}> <Toolbar> <Container> ...

Utilize Twilio to forward messages to a different phone number

I am seeking a solution to automatically forward incoming messages from one Twilio number to another, based on the original sender's number. For example: If I receive a message on my Twilio number "+14444444444" from '+15555555555', I want ...

Integration of Django Tastypie with Angular for secure user authentication

I'm currently facing an issue where I cannot establish a connection to the Django server. Upon checking the browser console, I noticed the following error message: [![console error][1]][1] My setup involves using Tastypie along with a UserResource c ...

How to transfer data using props through the ":to" attribute of a router link in Vue.js

I am facing an issue with creating a router-link in Vue and passing a route name as a prop. What I am trying to achieve is the following: <template> <div> <router-link :to="myProps">Login</router-link> </div> </tem ...

Ensuring Proper Sequence of Function Execution in Angular Directive

I'm a bit confused after reading the following two blogs: Blog I: Eric W Green - Toptal Order of execution Compile -> Controller -> PreLink -> PostLink Blog II: Jason More Order of execution Controller -> Compile -> PreLink ...

What is the best way to fill a "multiselect" element with information from a JSON object?

I'm struggling to populate the multiselect field with data from a JSON object. No matter which multiselect I use, the data shows in inspect mode but not on the frontend. It was supposed to look like this. https://i.sstatic.net/FVz2H.png but it comes ...

Exploring the process of dynamically setting up Passportjs strategies

Currently experimenting with passport and setting up my Twitter login like this: passport.use(new TwitterStrategy({ consumerKey: '*****', consumerSecret: '*****', callbackURL: "http://blabla/callback" }, functi ...

Utilizing an Observable in an Angular 6 service to ensure proper synchronization with Okta token present in local storage

Within my application, I have implemented third-party authentication to facilitate user login and store a token in their local storage. To enhance the user experience, I am developing a service to cache profile information. This service utilizes the user&a ...

Implementing a design aesthetic to installed React components

After downloading a react multiselect component created by an individual, I installed it using npm install .... and incorporated the component in my react code with <MultiSelect .../>. The dropdown checkbox 'menu' is designed to allow users ...

Find the string "s" within a div element aligned vertically, using Popper

Currently utilizing Popper from Material-UI <Popper id={"simplePopper"} open={true} style={{backgroundColor: 'red',opacity:'0.5',width:'100%',height:'100%'}}> <div style={{height:"100%", ...

stay at the top of the screen with anchor #link

Is there a way to link to a page with a specific bootstrap nav-tabs open without the page automatically scrolling down to that tab? I've tried using #link with the tab id, like www.mysite.com/apagewithtabs#tab2, but I want the user to be at the top of ...

Collaborate on global functions across the Quasar ecosystem and beyond, including Vue3

Is it plausible to utilize this method for sharing functionality (not data, which is handled by stores) in a Quasar and Vue3 application? // boot/generic_stuff.js import {boot} from 'quasar/wrappers' const function_list = { /* content goes here ...

Validation in Ajax Response

When receiving an object from my API call, I want to perform error checking before displaying the JSON data in my view. function response(oResponse) { if (oResponse && oResponse != null && typeof oResponse === "object" && oResponse.response ...

Prevent the use of the + or - symbols within the body of a regular expression when

function validateNumberInput(){ userInput = document.getElementById('txtNumber').value; var numberPlusMinusRegex = /^[\+?\-?\d]+$/g; if (userInput.match(numberPlusMinusRegex)) { alert('Vali ...