Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.4k views
in Technique[技术] by (71.8m points)

vue.js - VueJS/Typescript error: Cannot find module 'my-module' or its corresponding type declarations

I have a Vue2 setup with Nuxt and Typescript. I'm trying to install the vue-slick-carousel module using yarn but Typescript is giving me the error: "Cannot find module 'vue-slick-carousel' or its corresponding type declarations". Typescript newbie over here! This issue happens when I try to import the module in my component:

<script lang="ts">
  import Vue from 'vue'
  import { VueSlickCarousel } from 'vue-slick-carousel'
 
  export default Vue.extend({
     components: {
       VueSlickCarousel
     }
     ...
  })
</script>

Contents of tsconfig.json file:

{

"compilerOptions": {
    "target": "ES2018",
    "module": "ESNext",
    "moduleResolution": "Node",
    "lib": [
      "ESNext",
      "ESNext.AsyncIterable",
      "DOM"
    ],
    "esModuleInterop": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "experimentalDecorators": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./*"
      ],
      "@/*": [
        "./*"
      ]
    },
    "types": [
      "@types/node",
      "@nuxt/types",
      "nuxt-i18n"
    ]
  },
  "exclude": [
    "node_modules",
    ".nuxt",
    "dist"
  ]
}

And my vue-shim.d.ts file, not sure what to add there:

import Vue from 'vue'
declare module 'vue/types/vue' {
  interface Vue {
    $i18n: {
      locale:string,
      locales:{code:string, name:string, file:string}[]
    }
  }
}

I know the issue comes from Typescript not being able to find the module types. I found a library called "@types/slick-carousel" that installs the types but I'm not sure what to do next. Do I need to manually define the types?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

in tsconfig.json add @types/slick-carousel to types :

  "types": [
      "@types/node",
      "@nuxt/types",
      "nuxt-i18n",
      "@types/slick-carousel"
    ]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...