Trying to follow some Vue tutorials and I can't currently import Vue in a .js
file and then import this file in my index.html
. This is how I'm importing the script in my index.html
:
<script src="./js/main.js" type="module"></script>
If I do this in my main.js
file:
import Vue from 'vue';
I get the following error in the browser console:
Uncaught TypeError: Failed to resolve module specifier "vue". Relative references must start with either "/", "./", or "../".
If my import line to:
import Vue from '../../node_modules/vue';
Then I get a different error:
http://localhost:63342/vue-official-tutorial/node_modules/vue net::ERR_ABORTED 404 (Not Found)
What am I doing wrong?
If you are working with ES6 then you should NOT manually inserting your
main.js
intoindex.html
- this will be handled by Webpack. Actually, the simplest tutorial for Vue goes like this:./dist
folder of your projectAlso, you should import Vue like this
and not like this
EDIT
Okay, if you insist on going through the beginners' path and not using Webpack and single-file Vue components - then you should start like this:
And your
/app/app.js
will look like this:And your
/app/login.js
component will look like this:You can only use "import vue..." if you are using the CLI and webpack, etc.
If you are using Vue directly in a web page, you can follow the instructions at https://vuejs.org/v2/guide/installation.html#Direct-lt-script-gt-Include and include a line like this in your HTML file:
Do this before you import your own script, and then
Vue
will be defined and you don't need to import it again. Import your script without the "module" attribute. In the script, you can run:This assumes that somewhere on your HTML page you have: