Troubleshooting TypeScript when importing external JavaScript: Module not found or no type declaration file available

I recently acquired a VueJS admin template built in JS and am looking to integrate it into my existing TS application. However, when I attempt to transfer the components, views, and other elements, I encounter the following error message. Is there a way to resolve this issue without directly modifying the external code? I need a sustainable solution that will persist through updates.

Below is the error:

ERROR in /Users/username/MyApplicationDirectory/src/main.ts(17,32):
17:32 Cannot find module './components/TroubledComponent' or its corresponding type declarations.
    15 | 
    16 | // Custom components
  > 17 | import TroubledComponent from './components/TroubledComponent'
       |                                ^
    18 | import X from '@/components/X'
    19 | import Y from '@/components/Y'
    20 | import Z from '@/components/Z'

This is the content of my tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    // Other configurations...
  },
  "include": [
    "src/**/*.ts",
    "tests/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

Here is the fragment of the package.json:

{
  "name": "oneui-vue-edition",
  "version": "1.6.0",
  "scripts": {...},
  // Dependency information...
}

The import statement for the component in question within my main.ts:

import TroubledComponent from '@/components/TroubledComponent'

A snippet showcasing the structure of the troubled component:

<template>
  <component>
    ...content
  </component>
</template>

<script>
export default {
  name: 'TroubledComponent',
  props: {
    // properties
  },
  methods: {
    // functions
  }
}
</script>

NodeJS version being used: v14.17.6

Despite attempting various solutions, such as adding certain lines to the tsconfig.json, the error persists.

Answer №1

After much troubleshooting, I managed to resolve the issue by

  1. Executing the command vue add typescript to ensure all necessary typescript files were in place
  2. Verifying that my shims file contained the correct information : VueJS/Typescript - Cannot find module './components/Navigation' or its corresponding type declarations
  3. Adjusting my import statement from
    import TroubledComponent from '@/components/TroubledComponent'
    to
    import TroubledComponent from '@/components/TroubledComponent.vue'

For other JavaScript files, like directives, I simply needed to include "allowJs": true in my tsconfig.json.

I tried adding a declaration in modulename.d.ts for the directives, but kept receiving the error message

File ...../directives/mycrazydirective.d.ts' is not a module
. So, I decided to pause on this until I come across a better solution.

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

After refreshing the page, RouterLinkActive in Angular 6 fails to work

Scenario In my application, there is a menu with various items. The selected item is distinguished by adding the selected class to it, which changes its appearance. Problem While navigating between routes works smoothly, the issue arises when the page ...

Adjust the tally of search results and modify the selection depending on the frequency of the user's searches within an array of objects

Seeking assistance with adding a new function that allows users to navigate to the next searched result. Big thanks to @ggorlen for aiding in the recursive search. https://i.stack.imgur.com/OsZOh.png I have a recursive search method that marks the first ...

What is the best way to customize the active class of a v-btn in Vuetify?

I'm currently in the process of developing a website using Vue.js 2.6.10 with Vuetify 2.1.0 and vue-router 3.1.3. Within my v-app-bar, I have v-btns that link to various pseudo pages on my site. My goal is to apply a custom class to the active button ...

Looking to retrieve the AssetLoadedFunc properties in the LoadAssets function? Wondering if you should use TypeScript or JavaScript

When I invoke this.AssetLoadedFunc within the function LoadAssets(callback, user_data) LoadAssets(callback, user_data) { this.glg.LoadWidgetFromURL("assets/Js/scrollbar_h.g", null, this.AssetLoaded, { name: "scrollb ...

Tips for avoiding errors when determining the length of a child node in Firebase within a vue.js application by utilizing the "Object.keys" function

Let's envision a Vue.js application where the data structure stored in firebase looks something like this: item: { name: itemName, childOne: { subChildA: true, subChildB: true }, childTwo: { subChildA: true, subChildB: true ...

What is the best way to transform a JS const into TSX within a Next.js application?

Exploring the code in a captivating project on GitHub has been quite an adventure. The project, located at https://github.com/MaximeHeckel/linear-vaporwave-react-three-fiber, showcases a 3D next.js application that enables 3D rendering and animation of mes ...

Update the text and background colors of all Vuetify components

Hey everyone, I'm trying to figure out how to change the text and background colors of all Vuetify components. I've tried using a custom theme but it's not working as expected. Any suggestions? :) const myCustomLightTheme = { dark: fals ...

Incorporating an external module into your Angular application for local use

I currently have two projects: my-components, which is an Angular library, and my-showcase, an Angular app. Whenever I make changes or add a new component to my library, I commit it to a private git repository and publish it to a private npm registry. This ...

Creating intricate mazes using canvas drawing techniques

I recently developed a maze generator as a personal project utilizing a graph. While the generation logic works perfectly, I am facing challenges when it comes to rendering the maze. In my approach, each cell is represented by an array of 4 edges where the ...

Transform the date format from Google Forms to TypeScript

I am currently facing an issue with a Google Form connected to a Google Spreadsheet. The date format in the spreadsheet appears as follows when a response is received: 20/02/2023 18:58:59 I am seeking guidance on how to convert this date format using Type ...

Angular 2/4 throws an Error when a Promise is rejected

I implemented an asynchronous validator function as shown below. static shouldBeUnique(control: AbstractControl): Promise<ValidationErrors | null> { return new Promise((resolve, reject) => { setTimeout(() => { if (contr ...

What are the steps for creating a standalone build in nextJS?

Currently, I am undertaking a project in which nextJS was chosen as the client-side tool. However, I am interested in deploying the client as static code on another platform. Upon generating a build, a folder with all the proprietary server elements of ne ...

Ways to halt code execution if the component is not actively in use in Nativescript Vue

I'm facing an issue with my application where it continues to send data about the device every 5 seconds even when I minimize the app and reopen it or navigate away from the page. This leads to a situation where the function of sending data is trigger ...

Using V-bind to assign multiple classes has never been easier

Is there a way to assign one of two classes to an element based on three possible input variables in my Vue.js code? <input type='text' class='inputwordtext' v-bind:class="{(wordupload.firstchoice.selected == 'Zinnenlijst' ...

Assigning the 'name' variable within the Vue.js input

Can I use this syntax in Vue? <input :name="_charset_" /> Alternatively, is it better to write it like this: <input name="_charset_" /> ...

Using dot notation for event handlers in Vue.Js is a handy technique

I am currently developing a Single Page Application (SPA) using Vue.js 3 and Bootstrap 5. On the main page, I have implemented the Bootstrap Offcanvas element with code that closely resembles the one provided in the documentation. The structure of the off ...

Initiate and terminate server using supertest

I've developed a server class that looks like this: import express, { Request, Response } from 'express'; export default class Server { server: any; exp: any; constructor() { this.exp = express(); this.exp.get('/' ...

Tips for adding temporary text in filter input of Kendo UI Grid using Angular

I'm currently working with Kendo UI Grid in conjunction with Angular, and I am struggling to find a solution for adding text or a placeholder in filter inputs using Typescript. Within my code, I am utilizing the kendoGridFilterCellTemplate: <kend ...

Lazy loading with Vue router seems to be having issues within the Vite environment, as an error is being thrown mentioning an unknown

I have successfully implemented the code below in Vue router and it works flawlessly in Vue-CLI. import store from "./../../store/index.js"; function getView(view) { return () => import(`@/views/settings/${vi ...

"The ion-label in Ionic2 is cutting off some of the text and not displaying it

I'm currently facing an issue with ion-label inside ion-item. The description is not properly displaying and instead, it shows dot-dot.. ..I need the entire text to be visible. Is there any solution available? <ion-card *ngFor="let product of prod ...