14

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'
    }
  },
CuriousGuy
  • 3,818
  • 4
  • 24
  • 28

3 Answers3

21

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']
}
Nick M
  • 1,647
  • 13
  • 17
  • 1
    This worked for me but I don't understand why. How does this make Webpack know that when you import a module without an extension it is actually a Vue file? – Neil Nov 06 '19 at 13:40
  • @Neil https://webpack.js.org/configuration/resolve/#resolveextensions. It guesses what your file extension is if you didn't provide on. This is the order it will guess it (incase two files have the same extension) – Joey Carlisle Nov 12 '21 at 16:15
4

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"
    }]
  },
...
Frido Emans
  • 5,120
  • 2
  • 27
  • 42
Nisharg Shah
  • 16,638
  • 10
  • 62
  • 73
2

The problem is in Webpack configuration.

Make Webpack automatically resolve .vue extension by including extension array in Webpack resolve

 resolve: {
    extensions: ['.vue'],
   },
CuriousGuy
  • 3,818
  • 4
  • 24
  • 28
  • 1
    this extension is included by default in vue js, which can be checked with `vue inspect`, adding this extension didn't have any effect for me – Bouke Versteegh Feb 06 '21 at 10:19