8

I have setup Nuxt with TypeScript using the instructions from https://typescript.nuxt.org. The transition was pretty seamless and ok, except that I can't get rid of this error that nuxt:typescript "Cannot find module", which obviously is a false-positive.

My imports look like this:

import InputField from '@/components/InputField.vue'

I have tried with ~/ and without the .vue extension. Nothing works.

My tsconfig.json looks like this:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "lib": ["esnext", "esnext.asynciterable", "dom"],
    "esModuleInterop": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"],
      "~*": ["src/*"],
      "@/*": ["src/*"]
    },
    "types": ["@types/node", "@nuxt/types"]
  },
  "exclude": ["node_modules"]
}

And I have this object in my nuxt.config.js:

typescript: {
  typeCheck: {
    eslint: true,
    vue: true
  }
}

Error example

5
  • Try to change to "@/*": ["./src/*"]
    – Aldarund
    Oct 20, 2019 at 18:27
  • @Aldarund sadly, that didn't help.
    – aborted
    Oct 20, 2019 at 19:17
  • create a minimal reproduction on github please
    – Aldarund
    Oct 20, 2019 at 20:04
  • There's a fix here, but it only covers .vue files, and not .ts file imports: github.com/nuxt/typescript/issues/153
    – aborted
    Oct 20, 2019 at 20:34
  • ye, thats right, didnt know u dont have it :)
    – Aldarund
    Oct 20, 2019 at 23:45

1 Answer 1

20

I found the solution here: https://github.com/nuxt/typescript/issues/153#issuecomment-543010838

Basically, create a ts-shim.d.ts in your root directory with these contents:

declare module '*.vue' {
  import Vue from 'vue';
  export default Vue
}

As for your tsconfig.json, make sure you have these entries in there:

{
  "compilerOptions": {
    ...
    "baseUrl": ".",
    "paths": {
      "~/*": ["./*"],
      "@/*": ["./*"]
    }
  }
}

This solved my problems with both .vue imports and .ts imports.

Note! It is now absolutely mandatory to include .vue at the end of your Vue imports, otherwise the imports will fail:

import SomeComponent from '~/components/SomeComponent.vue'
1
  • 1
    "It is now absolutely mandatory to include .vue " helped me thanks
    – Adrien G
    Jan 8, 2021 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.