This article was originally published at https://www.telerik.com/blogs/a-first-look-at-the-vue-cli
Hello again! 😃
I've recently completed a beginners tutorial series for Vue.JS (shameless self promotion, sorry 😅), in which we went over all the basics of how Vue works.
However, we only looked at Vue from the perspective of adding a <script>
tag to a static HTML file and setting it up directly on the page's JS.
It's time to graduate to the big girl tools, and to look at one of the most pleasurable aspects of working with Vue - the CLI (Command Line Interface).
Fear not, it's super easy!
Sadly, for this tutorial i'm going to have to assume that you have a little knowledge of how to navigate inside the terminal with basic
cd
commands, because this is way out of scope of what Vue CLI is or does. Thecd
command is super basic though, and you can easily google for a video or quick tutorial if you need a refresher
Getting set up
There's a couple of things that we need to get set up in your computer before we begin. Feel free to skip over some of these if you already have them, but be super careful that you're not skipping over something vital.
NodeJS
In order to get the CLI working on our system, we're going to need to install Node first, particularly at the time in writing this we need to have at least version 8.9 but 8.11+ is recommended.
If you've installed Node before, but are unsure of which version you're using, open up your terminal and run the following command node -v
. You should get an output like v11.9.0
, if you get an error then you probably don't have Node installed on your machine.
Head over to the official page for node https://nodejs.org/en/ and on the very homepage you'll see two green buttons for downloading. Go ahead and click on the version that says LTS
(Long term support) unless you know what you're doing and wanting the latest version.
You will get a download for your current operating system, double click it and go through the installation wizard. Once you're done, fire up the terminal once again and try running node -v
once more to check that everything is working.
One more thing, when installing Node you also get NPM
(Node package manager) installed on your computer for free! We're going to be using this later, so just keep that in mind in case you wonder where this came from.
Yarn (Optional)
Some people prefer to use yarn
over npm
as their choice of package manager, personally I don't have a preference and will use both depending on the project and the team's requirements - but if you want to take it for a go just head over to https://yarnpkg.com/en/ and click on the Install Yarn
button.
Once again, download the file and follow the install wizard. Once it's done, you can check that yarn
was correctly added to your machine by running yarn -v
on your terminal.
Installing the CLI
Sweet! Now that we have all that we need, we can actually go ahead and install the Vue CLI on our computer.
Open up your terminal, and run the following command. (Note, that i'm going to be showing both NPM and YARN commands, you don't have to run both - pick the one that you want to use and that you have installed from the previous section).
npm install -g @vue/cli
yarn global add @vue/cli
When you run this command in your terminal, it's going to execute a handful of scripts and you're going to get some very cryptic output. Don't worry about this, go get yourself some coffee and wait until it's done installing everything.
Note the -g
and global
flags on both of these commands, what this means is that you're installing this package globally
on your computer. In short, this means that you will be able to use the commands from anywhere inside your filesystem, without having to navigate to a specific folder.
Once more, let's check that everything was installed properly by running vue --version
on the terminal, you should get output back with the version number of the CLI.
Creating our first project
It's time to get our hands dirty and actually use this thing. Fire up your terminal if you haven't already and navigate to the folder in which you want to create your project.
The command you want to run now is vue create <name>
, where <name>
is the name of your project - and also the folder that will be created.
Let's create our first project then by running:
vue create avocados
Fitting, right?
You're going to get hit with a bunch of questions that will help you configure your project, but don't panic, they're all very self explanatory.
The first screen will allow you to either select a default configuration (which is ok for beginner use), or to hand pick your options through the manual configuration option.
Don't worry if you didn't select an option such as say, Vuex, on your project set up - there's always a chance to reinstall all of these at any point in time later on on top of your project.
If you picked a manual setup, you're going to go through a wizard of options that will configure the options for your project. You can navigate this with your arrow keys, select and unselect options with the spacebar, and skip to the next screen with the enter key.
In here you can make choices like adding TypeScript support, Router base configuration or even Vuex for global state management.
Once you're done the CLI will do its thing, and after a few seconds your shiny new project will be ready. Go ahead and cd
into it - and let's check out the folder structure together.
The Folder Structure
Alright! I'm going to open this new project inside VS Code, and i'm using the Material Icon Theme to make the icons pretty, in case you were wondering.
Quick run down!
-
node_modules: Holds your dependencies code, the ones that you can install or remove using
npm
andyarn
-
public: This folder will hold the
index.html
that your webserver will load up when you navigate to the app's url. All the files that it will need will be auto injected by Vue, so you don't really need to worry much about what happens here. - src: This is where you will put all your code, components, assets, etc.
-
root files: On your project root you will see a bunch of configuration files like
.eslintrc.js
for your ES Lint configuration,.gitignore
for GIT, yourpackage.json
andpackage-lock.json
oryarn.lock
files for package management, and others depending on your previous choices.
So ok yeah, now what?
A good rule of thumb is that when you have a new project and you want to see your available scripts you should check out the package.json
file. Go ahead and open it and you'll see an entry in the JSON that is called scripts
.
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
On the left side you'll get the name of the script, and on the right side of the key:value pair you'll get what that script actually does for you.
How do I use them? Well, it's actually very simple.
If you're using NPM, you would type into your terminal npm run <scriptname>
, for example npm run serve
. If you're using YARN, then you would type yarn serve
.
Serve and Build
There's two main scripts that you will be working with when using the Vue CLI, one is serve
and the one other one is build
.
Go ahead and run npm run serve
or yarn serve
on your terminal (you need to be on the project directory before), and give it a few seconds to perform its magic. It will compile and bundle all your current assets, and finally spit out this legend.
A couple of things are now happening.
- This command fired up a server for you, which won't "quit" until you hit
control + c
on your terminal window. - The server will be on the lookout for changes you do on your code and when you save them, it will rebundle your assets immediately (and yell at you if you have errors).
- It gives you this
localhost
url that you can copy and paste or command/control click into your browser and will allow you to serve and view your project. - It has a hot reload function that will dynamically reload parts of your app from within your browser when something changes, so for example if you change a bit of CSS, the browser will auto-magically get updated after the CLI finishes recompiling.
So, bottom line, if you're working on your project you want this running in the background all the time.
On the other hand, you have the build
command.
Go ahead and run npm run build
or yarn build
and give it a few seconds to compile your assets.
You will get a new folder in your root called dist
which will have inside a copy of your index.html
, but this time its minified and it will have the embedded scripts and styles that it needs to load.
Inside this folder you will also get css
and js
folders that hold your compiled project.
In short, this is the folder that you would eventually want to put into your web server to deploy your application.
Bonus
Vue CLI actually has a GUI. 🤯
Head over to your project root in the terminal and run the command vue ui
and prepare to be amazed. You will get a full web-app GUI that will allow you to view/remove/install plugins, check out your dependencies, play around with your project's configuration and even execute the scripts you learned earlier!
Conclusion
Knowing and using the Vue CLI is a -must- for any developer that wants to use Vue to make SPAs. I know the terminal can be a dark and scary new world for first timers, but I promise once you go through these steps a couple of times it'll become less and less daunting to use. (And if everything else fails, you have the web UI to back you up!)
Top comments (0)