For most of the people, they may think JavaScript is nothing but a browser language, yes, but furthermore, it can be a common purpose programming language too.
open your browser like firefox or chrome, press F12, you shell see a debug window. Click Console, you will see a terminal-like layout, yes, that's it, you can run basic Javascript codes in it.
-
you shell know what is
npmand what isyarn. Basically, they do the same things. Butyarnis a newer tool for managingJavascript packages -
let's creat a
Javascript projectfirst.- install
npmoryarnbysudo apt install npmorsudo apt install yarn
- go to your terminal,
mkdir our_javascript_project,cd our_javascript_project
- use
npm init -yoryarn init -yto initialize your project (you will get a file which namespackage.json, that's thecore configurationfor yourJavascript Project)
- now, let's create our
index.jsfile withvim index.js, typing following:console.log("hello, world!"), save it
- install
-
instead of downloading your packages(like
yarnornpmwill do), if you want to run yourJavaScriptcodes, you have toimportyour package, especially, compile your codes to the native codes that every browser can run. So this time, we have to use a new tool,parcel- install
parcelby this command:sudo yarn global add parcel-bundler(you can google the installation instruction too)
- create a html page which contains
<script src="./index.js"></script>
- render this page using
parcel index.html
- now open the local serving link, for example,
http://localhost:1234, pressF12, you'll see thehello, worldwas printed at your browser console, great!
- install
-
see, what if we want to add a new package to this project, what we should do now?
just use
yarn add your_package_name, that should do it -
and, what if I want to get a single static website, which can run in any browser without the
yarn, parceltools, what I should do?well, try this:
parcel build index.htmlafter that, you can see a new folder
disthas been created in your project folder. Inside it, it's your static website, cheer!
congratulations! you have learned all you should know to make a JavaScript project!
I'll see you soon, don't forget to support me, I am poor in Money: PayPal