19

I'm going crazy trying to reconcile the Vue 3 CLI output into something that works with various tutorials and plugins that all use the Vue object, as in Vue.createApp(.... In my project, I can use

import {createApp} from 'vue';
createApp(...

but import Vue from 'vue'; results in Vue still being undefined. I have Vue 3 installed via NPM. Clearly there is something critical that I don't understand about NPM imports, how could the {createApp} import work if importing the whole module does not work?

From package.json:

"dependencies": {
    "@babel/polyfill": "^7.12.1",
    "apexcharts": "^3.22.1",
    "core-js": "^3.6.5",
    "d3": "^6.2.0",
    "vue": "^3.0.0",
    "vue-router": "^4.0.0-rc.1",
    "vue3-apexcharts": "^1.0.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "babel-eslint": "^10.1.0",
    "babel-plugin-transform-regenerator": "^6.26.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0-0"
  },

Here is my temporary main.js. This prints 'undefined' followed by the correct createApp function definition:

import Vue from 'vue';
import {createApp} from 'vue';
console.log(Vue);   
console.log(createApp);   
1

1 Answer 1

20

If you're working with CDN Vue is available as global variable which could be used to create instance by calling the method createApp like Vue.createApp({...}), but if you're working with a bundler which uses npm modules, there no Vue object imported from vue module so you should import createApp from it to create a new instance like :

import {createApp} from 'vue';
let app=new createApp({...})
app.use(somePlugin)
app.mount("#app")

for more details please check the migration guide of global API

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. The reason I was looking into this is because one of the dependencies I am using, vue3-apexcharts, is throwing an error saying there is no global Vue object. I guess I'll file an issue for that project.
@Gregorio246 according to this comment apex charts is not compatible with vue 3
The component that he talks about in that comment was released about a week ago, which is what I am using.
You're talking about this one?
Yes, that's the one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.