I get following error when I import *.vue files without .vue extension?

Viewed 9837

I get following error when I import a vue file without .vue extension .

ERROR in ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!./src/App.vue Module not found: Error: Can't resolve './components/Navbar'

My web back configuration is as below

module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: {
          loaders: {
            // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
            // the "scss" and "sass" values for the lang attribute to the right configs here.
            // other preprocessors should work out of the box, no loader config like this necessary.
            'scss': 'vue-style-loader!css-loader!sass-loader',
            'sass': 'vue-style-loader!css-loader!sass-loader?indentedSyntax'
          }
          // other vue-loader options go here
        }
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        loader: 'file-loader',
        options: {
          name: '[name].[ext]?[hash]'
        }
      }
    ]
  },
  resolve: {

    alias: {
      'vue$': 'vue/dist/vue.esm.js'
    }
  },
3 Answers

The problem is in your Webpack configuration. To make Webpack automatically resolve .vue extensions use the resolve.extensions option, and include the defaults.

resolve: {
    extensions: ['*', '.js', '.vue', '.json']
}

If you are following eslint and you getting this error, try to add the below lines in your eslint config

...
rules: {
    // other stuff
    "import/extensions": ["error", "always", {
      "js": "never",
      "mjs": "never",
      "jsx": "never",
      "ts": "never",
      "tsx": "never",
      "vue": "never"
    }]
  },
...
Related