3
votes

General jist:

import Image from '@/assets/default-profile-picture.svg'
//ERROR: Cannot find module '@/assets/default-profile-picture.svg'.Vetur(2307)

I have spent the better part of today trying to find a solution to this. I know there are a lot of other posts like this one, but they are all outdated (all over a year old).

I've just generated a clean Vue CLI app, and still have the same issue.

I'm using Vue CLI Version 4.2.3, and just attempted using Vue CLI Version 4.3.1, but ran into the same issue.

I have checked that the file is in assets. I have checked that the filename is spelled correctly. I have a feeling this is a webpack issue, as require() would not work when called using typescript. I have tried creating vue.config.js and manually setting the path for assets.

Project setup configuration:

  1. Features: Babel, TS, Router, ESLint
  2. not class-style syntax
  3. Babel used alongside Typescript
  4. No history mode for router
  5. eslint with error prevention only
  6. Lint on save
  7. Configs placed in package.json.

Error in Component.vue

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

/* Cannot find module '@/assets/default-profile-picture.svg'.Vetur(2307) */
import Image from '@/assets/default-profile-picture.svg'

export default Vue.extend({
  components: {
  },
  props: [
    'employeeImage',
    'employeeName',
    'employeeAge',
    'employeeSalary'
  ],
  data () {
    return {
      marked: false,
      result: [],
      name: this.employeeName,
      age: this.employeeAge,
      salary: this.employeeSalary
    }
  },
  computed: {
    compClasses: function () {
      return {
        marked: this.marked
      }
    },
    imageDefault: function () {
      if (this.employeeImage === '') {
        console.log('employee image empty: ' + this.employeeImage)
        return '@/assets/default-profile-picture.svg'
      } else {
        console.log('employee image set: ' + this.employeeImage)
        return this.employeeImage
      }
    }
  }
})
</script>

Typescript Config

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
        "paths": {
      "@/*": [
        "src/*"
      ]
        },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "/src/**/*.*",
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}


What am I doing wrong? Thanks in advance!

1
Seems to be a Vetur issue. Top voted solution there: "Manually drag your Vue App's Folder to the top of your opened projects inside of your VS Code workspace." - Dan
@Dan I agree it may be a Vetur issue. I tried your suggestion but alas it hasn't removed the issue. However, it is showing me more linting details than previously. - NoHara
I've updated the main post with my tsconfig.json. Here is some helpful info about Vetur: vuejs.github.io/vetur/setup.html#project-setup ... and path mapping in the TypeScript config: typescriptlang.org/docs/handbook/module-resolution.html Still on the hunt. - NoHara
@NoHara did you find any solution? - Zain Mohsin

1 Answers

1
votes

To whom it may concern, please use absolute path instead of using @ to reference the file when you want to import .vue components. Here is an example:

enter image description here

Instead of this:

enter image description here