Having trouble with Nuxt rejecting my own custom Vue component library, showing the error message "Declaration file for module not found"

I recently created a Vue component library using a tutorial I found at:

Creating the Component Library

The VueCLI project I set up included Typescript, which resulted in the creation of some *.d.ts files:

// shims-tsx.d.ts
import Vue, { VNode } from 'vue';

declare global {
  namespace JSX {
    interface Element extends VNode {}
    interface ElementClass extends Vue {}
    interface IntrinsicElements {
      [elem: string]: any
    }
  }
}

// shims-vue.d.ts
declare module '*.vue' {
  import Vue from 'vue';

  export default Vue;
}

In my index.ts file, I have exported all my components:


import ATag from './components/ATag.vue';
import AnotherThing from './components/AnotherThing.vue';
...

export {
  ATag,
  AnotherThing,
  ...
};

Here is the content of my package.json file:

{
  "name": "my-ui-components",
  "scripts": {
    "build": "vue-cli-service build --target lib --name my-ui-components ./src/index.ts",
  },
  "main": "./dist/my-ui-components.common.js",
  "files": [
    "dist/*"
  ]
}

When running the build script, it generates several JS files, a CSS file, and includes a folder for packaged images.

In my Nuxt project, I simply imported the component library from our Bitbucket account via SSH:

"dependencies": {
  "my-ui-components": "git+ssh://<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cfbf5e8dcfef5e8fee9fff7f9e8b2f3eefb">[email protected]</a>:my-account/my-ui-components.git",
}

However, when attempting to import my components downstream in my Nuxt app like this:

pages/Index.vue

<script>
import { ATag, AnotherThing } from my-ui-components;
export default {
...
components: {
ATag,
}
...
}
</script>

I encountered an error stating: "Could not find a declaration file for module 'my-ui-components' implicitly has an 'any' type". So it seems that my "ATag.vue" component is not being recognized properly.

<template>
  <span :class="classes"><slot /></span>
</template>

<script lang="ts">
import Vue from 'vue';

export default Vue.extend({
  name: 'a-tag',
  props: {
    // prop definitions here
  },
  computed: {
    // computed properties here
  },
});
</script>

It appears that there might be an issue with how the upstream declaration files are being used in the build process or potentially a problem within Nuxt itself. Since this is my first experience with TypeScript, I'm still learning the nuances of it all.

If you have any insights into what I might be missing or where the problem lies, I would greatly appreciate the assistance! Thank you!

Answer №1

I suspect the reason for this is related to your exporting method. Typically, I tend to write my export statements in an index.ts file like so:

export ATag from './components/ATag.vue';
export AnotherThing from './components/AnotherThing.vue';

Answer №2

To include custom UI components in your Nuxt project, create a declaration file called custom-components.d.ts in the root directory with the code snippet below:

declare module 'custom-components'

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

In TypeScript, use a Record<string, any> to convert to {name: string}

I have developed a custom react hook to handle API calls: const useFetch: (string) => Record<string, any> | null = (path: string) => { const [data, setData] = useState<Record<string, any> | null>(null); var requestOptions: Requ ...

What is the best way to employ document.addEventListener in TypeScript?

I am currently learning Ionic v2 and I am using document.addEventListener, but I am encountering errors as shown below: > [11:10:21] ionic-app-scripts 0.0.47 [11:10:21] build dev started ... [11:10:21] clean started ... [11:10:21] clean finished in ...

In Vue, props are not automatically assigned; be sure to avoid directly mutating a prop when assigning it manually to prevent errors

I am working with two Vue components: GetAnimal.vue and DisplayAnimal.vue. GetAnimal.vue sends a JSON object containing animal data to DisplayAnimal.vue using router push. DisplayAnimal.vue then displays this data. The process flow is as follows: I navigat ...

Renderer's Delayed Electron Loading

I am in the process of developing a Electron application using TypeScript and React. This application serves as an account manager, allowing users to retrieve and view data for specific accounts. However, I have encountered an issue with the ipcMain.on(&a ...

Visibility in classes can shift once a new file is imported

Currently, I am encountering a puzzling issue in my angular2 project using typescript. In my main.ts file, which contains a component along with some imports at the start of the file, there is a custom type class (let's call it TypeFoo) located in mod ...

Incorporating Apollo Mutations into a Vue composables

One of the challenges I encountered was encapsulating Apollo mutations in a Composable in Vue to enable reusability across multiple components. However, for a specific instance, I needed to access the parameters of the mutation to create the update functio ...

Trying to modify a read-only property in Ionic 2 and Angular 2 is causing an error

Utilizing the Ionic refresh API allows for pulling and refreshing a page with updates to be accomplished. Within an Angular2 page, I am trying to use the refresh method to clear existing rows and then add new rows using the .push() method. However, when I ...

What is the best way to set parameters in a modal window?

Here is how I utilize the ng2-bootstrap modal in my code snippet: import {Component} from '@angular/core'; import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; @Component({ selector: 'add-customer-modal', template: ` ...

Validation of forms using the server

I am facing an issue with my form validation in which I am trying to check the existence of an email through HTTP validation, but encountering an error. Here is a snippet of my code: Within the form component constructor( private _formBuilder:FormBui ...

Is it possible for me to use ts files just like I use js files in the same manner?

So I recently stumbled upon TypeScript and found it intriguing, especially since I enjoy adding annotations in my code. The only downside is that I would have to change all my .js files to .ts files in order to fully utilize TypeScript's capabilities. ...

Issue with JQuery Promise: fail() being invoked before promise resolution

In my TypeScript code, I encountered a peculiar issue with a jQuery promise. The fail() function is being executed immediately, logging an error message to the console, despite the promise resolving successfully afterwards. Here is the code snippet: ...

The watch function was triggered for a different key in the array from the one that

What is the best way to limit the watch function to only trigger for the selected key in the data array? import { useSessionStorage } from "@vueuse/core"; const defaultState = { data: [ { key: 1, name: "A" }, { key: 2, nam ...

Cut through a Vue array with GraphQL's data layer

Struggling with slicing an array of items in Vue - check out my code below! <template> <div v-for="item in Items" :item="item" :key="item.id">{{ item.name }}</div> </template> <page-query> ...

Printing values of an object in an Angular/HTML script is proving to be quite challenging for me

In my Angular project, I have created a service and component for listing objects. The list is functioning correctly as I have tested it with 'console.log()'. However, when I try to display the list on localhost:4200, nothing appears. <table&g ...

Decorators do not allow function calls, yet the call to 'CountdownTimerModule' was executed

While building production files, the aot process is failing with this error message: Function calls are not supported in decorators but 'CountdownTimerModule' was called. I run the build command using npm run build -- --prod --aot and encounter ...

Vue Vee-validate incorrectly flagging valid URLs as invalid

During the development of my Vuejs application, I have implemented Vee-validate for validation purposes. However, there seems to be an issue where valid URLs such as http://localhost:3000 are being marked as invalid. This problem can also be observed on t ...

Guide on displaying multiple vue single file components together on a webpage

As a newcomer to Vue, I am experimenting with basic examples to grasp the framework better. Currently, I have created two single file components named navigation.vue and pageoptions.vue. These components are imported in the main.js file successfully compil ...

What could be causing the getComputedStyle() method to malfunction within my Nuxt application?

In my Nuxt app, I am working with SSR mode and Vuex. On one of the pages, I have a two-column layout where the text content comes from Vuex data. Here is the code snippet for that page: <template> <div> <v-container> ...

Retrieve paginated endpoint using DRF via axios

I am working with a paginated endpoint in my django-rest-framework API. When I send a list GET request, the response is structured like this: { "count": 161, "next": "http://localhost:8000/api/v2/bars/?limit=50&offset=50", "previous": null, "results": ...

Invoking a different module function as a callback in ES6

Question: I am facing an issue with ES6 modules regarding calling functions between them as a callback. In "page_API.js", the callback function is invoked upon receiving data. // Initiating a server request, triggering the callback upon data reception im ...