0
votes

I have been trying to migrate a vue project and I unfortunately cannot use import/export in where I am building the application. Therefore, to simplify my problem, I created a project from scratch to build a vue component without the vue-cli.

I have been successful to bring up the vue app with webpack before I created .vue files but after that point I started to get the following error and my .vue file wouldn't render.

 [Vue warn]: Failed to mount component: template or render function not defined.

The code is the following and I'll try to keep it as simple as possible:

index.js

var Vue = require('vue');
var App = require('./App.vue').default;

var app = new Vue({
  el: '#app',
  render: h => h(App)
})

App.vue

<template>
  <div>
    <h1>Hello World!</h1>
  </div>
</template>

index.html

<!DOCTYPE html>
<html>
<head>
    ... (meta stuff)
</head>
<body>
    <div id="app"></div>
</body>
    <script src="dist/bundle.js"></script>
</html>

and lastly the webpack.config.js

const path = require('path');

module.exports = {
    entry: {
        main: './index.js'
    },
    output: {
        filename: 'bundle.js',
    },
    resolve: {
        extensions: ['.js', '.vue'],
        alias: {
          vue: 'vue/dist/vue.js',
          'vue$': 'vue/dist/vue.js'
        }
    },
    watch: true,
    mode: 'development',
    module: {
        rules: [
          {
            test: /\.vue$/,
            loader: 'vue-loader'
          }
        ]
      }
};

I run webpack-dev-server --config ./webpack.config.js from npm to get the webpack server running. I also did webpack --config ./webpack.config.js but then I have to run the index.html in chrome and I thought running a server might actually be better for the bundle.

I am aware that a lot of people had similar problems but for some reason none of the solutions I found out there worked for me. I'd really appreciate some help about which direction I should take to fix this situation.

This article might be helpful but I am not sure how to fix my project with it: https://vuejs.org/v2/guide/installation.html#Runtime-Compiler-vs-Runtime-only

Edit On Request

dev-Dependencies and dependencies:

{
  "name": "vueTest",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "build": "webpack --config ./webpack.config.js",
    "dev": "webpack-dev-server --config ./webpack.config.js"
  },
  "license": "MIT",
  "devDependencies": {
    "vue-loader": "^14.2.2",
    "vue-template-compiler": "^2.5.16",
    "webpack": "^4.5.0",
    "webpack-cli": "^2.0.14",
    "webpack-dev-server": "^3.1.3"
  },
  "dependencies": {
    "vue": "^2.5.16",
    "vue-router": "^3.0.1"
  }
}
1
Do you use babel ? I do not see itSandwell
I am not using babel for this project because I'm using just ES5Kemal Tezer Dilsiz
Could you provide your devDependencies and dependencies in package.json and npm version?? I run your code on my conditions and everything works well.Guest
I've added them @GuestKemal Tezer Dilsiz
ES5 doesn't support arrow functions, so you'll need to define the render method using a normal function definition: render: function(h) { return h(App); }thanksd

1 Answers

0
votes

What has worked thanks to thanksd's comment:

var app = new Vue({
  el: '#app',
  render: function(h) { return h(App); }
})

However, it is important to have var App = require('./App.vue').default; especially the default part for the vue files. Hopefully that helps other people as well. I understand that my problem was in the end a simple syntax problem.