Are you using .mjs file extension ? | ECMA Module (ECMAScript Module) in Node. js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the channel my name is mahesh karya you're watching me on youtube now and in this session i'm gonna take you through mjs file format if you haven't heard about this mgs file format let me tell you guys uh if you are a node developer and if you are not aware about mjs file extension this is something that allows you to write import statements classes and all the advanced modular javascript code within with using that mds extension so you don't need to use payable uh anymore so that's what i think and i have recently tried most of the projects using uh this file extension instead of js so it worked for me so let me show you quickly so we will search for mgs file extension quickly okay and you just need to search here you can go through the dock if you wanna like read something about it so it's kind of modular javascript that supports uh advanced uh import statements especially so let me create a new uh let me resize uh this thing uh so hopefully this is visible to you guys and command plus g or control plus practice for if you are on windows uh to open this terminal and i'm gonna create a new file extension file with using mgs extension and i'm gonna write the normal express code okay so i'm gonna uh use npm init hyphen let me create a new directory uh test let's say okay and cd into test enter okay here we go npm in it hyphen y okay now i'm gonna install express so okay so once once this is done you just need to write import sorry import express from express then uh const app equal to express then normal app.gate hello world then you just need to listen a server just said this is done close this thing so yeah it was the simple thing before that we need to write the bible things we need to install dave dependencies and all stuff now i'm gonna run this thing quickly okay so clear uh i'm gonna say node uh what was the file name index dot mgs and uh i just need to check that index.js mgs is available here or not oh yeah inside test i need to move this thing okay so now let me run this thing again and here we go this is running completely fine so now let's consider if you have made any project uh with using paper let's say so let's create quickly a project or a let's try a complete uh project with using this mgs file extension so you will get that kind of confidence that yes we are ready to use mgs extension instead of people so i'm gonna quickly install uh let's say npm install mongoose okay uh no need and try okay and i'm gonna quickly create a normal user user.model model.js okay and let me create a connection dbconnection.js oh and yes sorry i'm keep using it uh js format so rename this thing to mgs okay and let me run mongodb i hope yeah is running completely fine inside my uh docker if you don't have it you just need to write a simple docker compose file dot yaml let me click here create a new file 3.8 version 9 whatever it may be services that i'm gonna use a db and hit enter then you just need to write the image no need to write version and here we go container name restart always volume enhance no need to write a moment and stuff for now so this is how you you can create a new mongodb let me delete this thing and let me rerun this thing so you will get idea how to run mongodb quickly uh if you have a docker of course so simply run sudo docker docker compose uh have enough name let's no need to write name actually up and have an event build so yeah looks good to me i guess uh i have make sure to you there should be a docker compose file uh wherever you are so container name writing is uh important by this container it will be created image name it will gonna fetch this image from the docker hub version 3.8 you can use the any latest version uh and volumes that used to persist your data so you won't lose your data every time you restart the mongodb so quickly let's create our password because i'm using sudo so this is it got created i forgot to run this thing in a background so let me open another terminal let me go there let me go to the docker extension if you are not familiar about this docker extension you can grab this from here simply search for docker uh i don't want to make this tutorial more over docker centric but uh yeah this is it i guess so you got it right now we can start with the coding part quickly so ctrl kw to close all the windows ctrl p let's go to the uh db connection and here i'm gonna use import uh mongoose from mongoose okay and i'm gonna say no need to write config files you can write if you have a dot env i'm gonna write directly which will be mongoose dot connect okay and here i'm gonna write the localhost colon 2707 slash test okay so after that here i'm gonna write cons db go to slot connection and i'm gonna import this thing to index.mjs here somewhere uh here so import uh that file name that i have inside my yeah this one so it will create or run the uh it will start i'm going to be it will be connected with mongodb now here i'm i'm gonna uh use that user instead of res.in i'll say arias.js and i'll simply return the users okay and users would be my of it i'm not sure if i can use the submit like this await i'll say user that i need to import okay so for that i'll say i don't have user yet so let me create user so user.model dot mgs here i'm gonna say import mongoose or directly that schema okay schema and what do we say a mongoose dot model hopefully or anyways let's go with the traditional way for now then we'll separate out the code so our cons to user quickly which will be a new store schema name email password okay here we go password then i don't wanna use date let's export this thing out quickly quickly yeah good mongoose dot model model let's use user user and export default export default yeah this is it let's uh insert this thing here user from yeah user dot model and here i'm gonna say user user.fine quickly and here i'm gonna say yeah like this no need to map though just simply to json y to json it's fine yeah here we go for now so let's run this thing quickly i'm not sure why i'm getting uh this error of where i'm missing something yeah here somewhere like this yeah so yeah let's run this thing so npm start let's add this to package.json so package.json and i'm gonna add this thing as a start script where i'm gonna say instead of that people node it's directly say node index how simple it is and npm start let's add node 1 alphas hyphen d node 1 and let's use node 1 here and hopefully i got the dave dependency if not let me add again oh sorry into test folder okay and here let me add node born here we go let me see dave dependency we got node 1 then npm start hit enter i am getting this i don't want yeah oh so yeah so now let's see quickly uh what we have uh user test index uh if i'm missing something or not the user is not found my god in package.json index.js make sure to uh define this mgs format uh otherwise it won't work so this time it should work hopefully and yeah oh wait so for that we need to use async of course so let's go there let's use async hopefully this time it should work let's see now yeah it's running fine so let's go there let's go to local histolony 300 hit enter you got your users so finally it's running completely fine and what was the traditional way if you are not familiar about that let me tell you you can you can like stop the video if you want right now but uh because i'm gonna compare with wha how difficult it was before okay so before we need to install uh add babel core then add babel node then we'll say uh add people slash reset hyphen env okay so nodemon of course we have node one already so before that we used to uh install that menu of dependencies okay to just support that import statements so classes we nowadays we it's uh it's official nowadays we can support or we can write code in with using classes constructor if you are not familiar about that i have also made video on that so make sure to write classes as much as possible which is kind of good thing and and let's let's set up this thing so i need to create i need to remove this it's not needed though delete it quickly yes moto trash let's go there let's create dot pablo rc okay here we need to write presets so it will be presets and here we need to write add paper no need to write react so it was a basic config okay if you are not if you don't want to use this mgs if you are not comfortable with it so definitely go with that and here in packet.json here you just need to write start with let's say start bubble let's say bubble i used to pronounce to uh remember that uh spelling so i don't so here instead of mgs you can prefer to use chairs and just rename all the files with js and you're good to go so this is how that i don't like to go with that thing so i'm just i like him just go with that so this is it i guess for today's session and uh before that it was experimental i actually so uh i was facing many issues when i started to make my own framework so then i realized it was experimental and nowadays it's working fine so that's it for tutorials so thank you so much all of you guys for being with me supporting me all the time it's been pleasure to be here please let me know inside comment box what do you want from my side definitely i'll make it for you and don't forget to subscribe my channel if you haven't already and smash that like button and you can use this thanks button as well in order to contribute so it even one dollar is so enough for us to grow our channel so thank you so much for your time guys good day bye
Info
Channel: Mahesh Kariya
Views: 5,167
Rating: undefined out of 5
Keywords:
Id: rQhZ6OkH2bc
Channel Id: undefined
Length: 13min 33sec (813 seconds)
Published: Sun Aug 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.