JavaScript Modules with Import/Export Syntax (ES6) - JavaScript Tutorial

Поделиться
HTML-код
  • Опубликовано: 12 сен 2024
  • In this video I'll be showing you how to use native JavaScript Modules with the import and export syntax in JavaScript - this works on major browsers such as Chrome, Firefox, Safari and Edge.
    Modules allow you to separate your code into individual chunks or components, allowing for easier maintainability and better file organization.
    Support me on Patreon:
    / dcode - with enough funding I plan to develop a website of some sort with a new developer experience!
    For your reference, check this out:
    developer.mozi...
    Follow me on Twitter @dcodeyt!
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

Комментарии • 222

  • @dcode-software
    @dcode-software  2 года назад +2

    *HOW TO BUILD YOUR OWN JAVASCRIPT LIBRARY WITH MODULES:*
    ruclips.net/video/sZQEa13r22I/видео.html
    🏷 *THE ULTIMATE JAVASCRIPT DOM CRASH COURSE* 👇
    www.udemy.com/course/the-ultimate-javascript-dom-crash-course/?referralCode=DC343E5C8ED163F337E1

  • @daquirisoft3603
    @daquirisoft3603 4 года назад +76

    God, this type of clarity and conciseness is so rare. Excellent explainer.

    • @secretcaptain7694
      @secretcaptain7694 3 года назад

      True, this type of videos we can't get on paid course also.

  • @wianll
    @wianll 5 лет назад +31

    I've been using a form of this in Angular this whole time and didn't even know you could do this in vanilla js. Thanks for the run-through! 🐱‍👤

    • @dcode-software
      @dcode-software  5 лет назад +2

      No dramas, mate

    • @RyanValizan
      @RyanValizan 3 года назад +1

      Wian Lloyd, I’m learning TypeScript from Open Source projects when my background is in PHP/JS. I’m here to see what I don’t know know, ya know?

  • @adriansun2999
    @adriansun2999 5 лет назад +5

    This is how a tutorial is made. No introduction. Straight to the point and only the point. Everything about it is given.

  • @deniskomarov189
    @deniskomarov189 2 года назад +1

    Been trying to get these imports to work for a long time now. Thanks so much!

  • @XxPepper17xX
    @XxPepper17xX 2 года назад +1

    NO ONE ON STACKEXCHANGE COULD EXPLAIN THIS. Thank you!

  • @sparshgupta8078
    @sparshgupta8078 4 года назад +2

    YOU DESERVE A MILLION + SUBSCRIBERS SIMPLE BUT GREAT EXPLANATION IN A SHORT VIDEO...AMAZING

  • @pjf7044
    @pjf7044 2 года назад +1

    Thanks so much. I was so confused over import and export for a long time but you really simplified it! Makes total sense

  • @Ginfio
    @Ginfio 2 года назад

    Thanks for this video. I used import/export last year, now I'm learning the same thing as a refresher, and it makes a lot sense watching this video.
    Thx

  • @espanol9750
    @espanol9750 4 года назад +7

    Thx so much for making this video. Finally I'm able to understand it :D

  • @jahmiel1945
    @jahmiel1945 Год назад +2

    i was struggling with js modules, as soon as i watched this video i figured out my mistake for not including the .js extention. Thanks to dcode, i now uderstood why its was giving mw errors

  • @rachidahireche4165
    @rachidahireche4165 10 месяцев назад

    The explanation in this video is amazing, thank you

  • @sher.5027
    @sher.5027 2 года назад

    Nice. Video. Commenting her to tell the universe that I understood your video. Crystal Clear Concept. Thankyou for the upload.

  • @anthonyfrimpong5473
    @anthonyfrimpong5473 3 года назад +2

    Great explanation. i'm new to the coding business and learning on my own. your explanation is so simple to understand as compared to some for the resources i have bought online. Thanks

  • @jasonrbodie
    @jasonrbodie 4 года назад +3

    Great tutorials man! Great explanations, great examples, great everything. Thank you!

  • @kingofstride
    @kingofstride 2 года назад

    Thank you so much; great video! Cheers from Sweden 🇸🇪😊

  • @gerardpalagalofre8119
    @gerardpalagalofre8119 Год назад +1

    Perfectly clear, well explained. Thank you :)

  • @sadface
    @sadface 2 года назад

    I have been putting everything in the same JS file for years... never knew this was possible in JS. This changes everything.

  • @Epikht
    @Epikht Год назад

    Thanks a lot. you are the only person to explain this topic so well in youtube

  • @mdrisulamin7389
    @mdrisulamin7389 4 года назад +1

    Just I want to say Thank You for this informative video. Thank You again!

  • @trinadhr1993
    @trinadhr1993 3 года назад +1

    awesome bro , thank you soo much

  • @devinstinct1778
    @devinstinct1778 3 года назад

    very clean and crisp explaining

  • @spencergresoro
    @spencergresoro 3 года назад

    Excellent, short and concise { completelyUnderstand }.

  • @anitasunildesai
    @anitasunildesai 7 месяцев назад

    Thanks for the great, detailed explanation.

  • @michajankowski3615
    @michajankowski3615 3 года назад

    Boom! native js modules just work:D thank you for your clear explanation.

  • @jowsad2857
    @jowsad2857 4 года назад +9

    hi my browser says " index.html:1 Access to script at 'file:///C:/Users/User/Desktop/Learning/js/main.js' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. " and I cannot use modules like you teach. any help?

    • @jorgeortiz7926
      @jorgeortiz7926 4 года назад +1

      I have the same problem, and I found the solution, you need to set a remote server.
      I installed node and them i made cmd npm install --global http-server.
      then i went into my directory root and typed " http-server . " with the "." and withouth " ".

    • @UnderGun81
      @UnderGun81 4 года назад

      @@jorgeortiz7926 the server must be running while you're importing and exporting modules I assume...

    • @jorgeortiz7926
      @jorgeortiz7926 4 года назад

      @@UnderGun81 Right, a local server must be running in order to work the modules importing

    • @UnderGun81
      @UnderGun81 4 года назад

      @@jorgeortiz7926 it's not really working for me, where did you modify the http-server dependency? In the package.json?

    • @jorgeortiz7926
      @jorgeortiz7926 4 года назад

      @@UnderGun81 i did not modify anything i just ran a command, here www.npmjs.com/package/http-server
      You can check the doc

  • @spartanboss7411
    @spartanboss7411 3 года назад

    I really like your tutorials alot. You are the best teacher i have ever met

  • @mihaipascu4975
    @mihaipascu4975 2 года назад

    Very usefull and cleary explained! Merci !!!

  • @iamreg1965
    @iamreg1965 4 года назад

    Nice video. Short and to the point.

  • @pauloffborba
    @pauloffborba 4 года назад +1

    Very clear! Thanks!

  • @mohamedazard7585
    @mohamedazard7585 3 года назад

    Awesome explanation. You made it so easy bro.

  • @tornadoreaper
    @tornadoreaper 2 года назад +1

    what a hero

  • @genralwalker9538
    @genralwalker9538 4 года назад +1

    You did excellent. I was only able to understand js modules after watching your video. 🙏🏽

  • @akengestudios5394
    @akengestudios5394 4 года назад

    Impossible to explain better, bless you

  • @Azar211
    @Azar211 4 года назад

    Thank you for this short lesson without bla bla

  • @danishshah2531
    @danishshah2531 3 года назад

    very helpful video. finally i'm able to understand. good work.

  • @dwarslopers
    @dwarslopers 2 года назад

    Wow. Exzellent Explanation!

  • @daniellambert726
    @daniellambert726 2 года назад

    Thank you so much for taking the time.

  • @rikkarth_
    @rikkarth_ 2 года назад

    Excellent explainer. Thank you very much.

  • @parasarora5869
    @parasarora5869 5 лет назад +1

    woww...thanks for introducing it 😃 ... really loved this video 👍

  • @anisdzed9681
    @anisdzed9681 5 лет назад +3

    Very helpful, thanks a lot

  • @bigblue531
    @bigblue531 3 года назад

    Geat speaker, very easy to understand

  • @nonchalant8473
    @nonchalant8473 5 лет назад +2

    Great tutorial. Thank you!

  • @cirusMEDIA
    @cirusMEDIA 3 года назад

    If there was an option to love this video i would, but for now... i will have to settle with a like.
    Thank you for the knowledge and tutorial!

  • @juanpedrososa1118
    @juanpedrososa1118 3 года назад

    Thank you. This video has been very helpful

  • @SuhailkcOfficial
    @SuhailkcOfficial 7 месяцев назад

    Thank you for this video. very helpful 👍

  • @bestchoice7431
    @bestchoice7431 4 года назад

    Great bro. Great explanation. Keep it up.

  • @prashjeevrai2246
    @prashjeevrai2246 3 года назад +1

    you sound a bit like Korg. Thumbs up on the video btw

  • @Speed-TV
    @Speed-TV 9 месяцев назад

    such clear tutorial!

  • @mbs2488
    @mbs2488 2 года назад +1

    Excelente ! Muito bem explicado ! ! Parabéns pelos vídeos ! ! !

  • @SabaFesvianidze
    @SabaFesvianidze Год назад

    very easy explain bro!!

  • @agent-33
    @agent-33 3 года назад

    Explained well. Thank you.

  • @Kdcloop
    @Kdcloop 3 года назад

    Simple and clean,Thanks !!

  • @NuNaKri
    @NuNaKri 4 года назад

    Very helpful and well explained :D

  • @MINECRAFTLOVER4000
    @MINECRAFTLOVER4000 3 года назад +1

    when reloading the console says "Access to script at '(path to file)' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https."

  • @ashleysangma407
    @ashleysangma407 2 года назад

    Thank you so much. Now I can write my code in much more peace.

  • @nikhilmathew9029
    @nikhilmathew9029 4 года назад +1

    Simple and awesome excellent video

  • @podlak
    @podlak 3 года назад

    Awesome. Nice and easy. Many thanks!

  • @localbuy8287
    @localbuy8287 3 года назад

    very clear ---- Thanks

  • @sebastianonofrei8293
    @sebastianonofrei8293 3 года назад

    Thanks a lot dcode.

  • @Akbar-ux3bc
    @Akbar-ux3bc 9 месяцев назад

    Well explained.

  • @davidponc
    @davidponc 4 года назад +2

    silly question 😅.
    What is that theme? and what font do you use?

  • @anjalipawar2132
    @anjalipawar2132 2 года назад

    very well explained...

  • @ayushtrivedi2335
    @ayushtrivedi2335 4 года назад +1

    Love from india💖💖thanks bro

  • @jogoeire
    @jogoeire 3 года назад

    Good clean tut.

  • @vahesevachyan9399
    @vahesevachyan9399 3 года назад

    great explanation, thank you.

  • @JoseGonzalezUwU
    @JoseGonzalezUwU 2 года назад +1

    muy buen video, muy claro, me sirvio mucho :)

  • @CharlyBGood11
    @CharlyBGood11 3 года назад

    Great! thanks for this!!

  • @salv236
    @salv236 4 года назад +3

    i tried to use this feature i get a message "Access to script at [path to root js file] from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."

    • @seanc846
      @seanc846 4 года назад

      Any answer on this?

    • @dcode-software
      @dcode-software  4 года назад

      I made this video which should help you guys out:
      ruclips.net/video/0IMz8d9Cby4/видео.html

  • @ausar2379
    @ausar2379 Месяц назад

    Good lesson. Now how do I import js files into React as modules? Wonder if I can import the whole file instead of just functions inside of them?

  • @ganeshpatil6020
    @ganeshpatil6020 3 года назад

    Thanks a ton. This is really helpful.

  • @ohad7
    @ohad7 3 года назад

    Great tutorial. Thank you for sharing .

  • @Ayntak
    @Ayntak 4 года назад

    Awesome, got it! Thank you :)

  • @VisioGuy
    @VisioGuy 3 года назад

    Can someone explain WHY this import/export stuff is needed? I've created a complicated web page that has 13 js files. I mostly use the "class" syntax, since it makes js seem more like a "real" language (haha!). I have exactly ZERO import/export statements, and everything works just fine. But every example I find when searching talks about import/export. I don't get it. It seems really, really cumbersome to have to explicitly list each function you want to use at least TWICE (one for export, N for import). Is this just a crappy way of doing public/private?

  • @Martin958
    @Martin958 Месяц назад

    Doesn't work at all. It tries to find every single variable on my exporting script and not just the ones I specified. Then returns null for them.

  • @NathanMcclure
    @NathanMcclure 2 года назад

    YES! Thank you!

  • @Hunter-bz1ns
    @Hunter-bz1ns 3 года назад

    Thank You Man!

  • @IamFrankEU
    @IamFrankEU 3 года назад

    Have my upvote sir! Very understandable for a starter. Thanks for all your effort. There's actually one thing I still don't really understand. Why would you ever use the [export default] option? The reason for me to start code-splitting is to make your code more readable... For me, it looks like a lot more work to understand what you did wrote. Anyway, thanks again!

  • @in2minutesorless64
    @in2minutesorless64 3 года назад

    Thank you that was great! I wonder if you could make a separate video detailing how to set up and use modules in three.js in the browser... but not using node.js or express servers. Many thanks!

  • @CodingJourney
    @CodingJourney 3 года назад

    Flawless!! 🙂💙

  • @Tanna308
    @Tanna308 2 года назад

    very tanks for this video

  • @sifatimtiaz9171
    @sifatimtiaz9171 4 года назад

    10/10. Superb!

  • @maryampi6055
    @maryampi6055 Год назад

    I created a chrome extension. I want to add an encryption that is gonna work on a web page and I need to use CryptoJS library but whenever I do import CryptoJS, I get an error: Uncaught SyntaxError: Cannot use import statement outside a modul

  • @sseruwagifaishar1134
    @sseruwagifaishar1134 2 года назад

    when I use the same language when console log ,it indict error as ( i is not defined)
    why showing i not defined when console.log?

  • @rolandpierrelouis4013
    @rolandpierrelouis4013 4 года назад +1

    Thank you for this tuto about module in JS. I code a web app in JS with different modules implemented and I get cors error when trying to launch the app localy. how can I fix this problem please ?

  • @alfonstabz9741
    @alfonstabz9741 3 года назад

    i have errol an error will occur everytime I use import export syntax error: can use statement outside module..

  • @jignasajoshi27
    @jignasajoshi27 2 года назад

    you explain everything very clearly but when i use same files i got CORS error.

  • @shrishyle
    @shrishyle 2 года назад

    Hi I did not quite get the use case of the aggregating feature. In what scenarios would people be using it? And why would people prefer all the modules be exported from a single module....(it actually increases the length of code in case one has got hundred modules....code exporting the modules cannot be skipped...so also code for importing the modules also cannot be avoided....the code for bridging is extra code that one may have to write...although this makes the code structure relatively more systematic...can u please let me know in which scenarios it is used.

  • @piesieyeboah5307
    @piesieyeboah5307 2 года назад

    great content

  • @TheNamesJT
    @TheNamesJT 3 года назад

    In 2021 i'm using require and module but for some reason console.logs wont show up in the console on other scripts even if i export the method and call it in the main script for some reason console.logging an event listener doesn't work nor doest just returning console.log('hello")

  • @jbc5126
    @jbc5126 2 года назад

    is there a way around having to put 'export' in front of every statement in a js file? i find this extremely tedious. Can I just export everything in a js file without mucking up the code with duplicate export calls all down the page?

  • @muradrehimov8325
    @muradrehimov8325 3 года назад

    When I write type = "module" into script tag, no result is obtained.Why is this so?

  • @viswanathbijunair8564
    @viswanathbijunair8564 2 года назад

    When I export variables between two javascript files, other functions in the first javascript file become defunct. Any help on how to solve this is appreciated !

  • @borisbarzotto5785
    @borisbarzotto5785 3 года назад

    Thanks my friend

  • @keivansf3459
    @keivansf3459 2 года назад

    Thanks a lot!

  • @AlHoota
    @AlHoota 3 года назад

    Thank you!

  • @watchmytvjctechnology8448
    @watchmytvjctechnology8448 4 года назад

    thank you for the tutorial
    But I have a problem because I need to use http protocols to use the export module.
    As all of us know we can create android game with the help of phones build,
    my question is what will happen if I create my game with export and import method, there's no server or live server available.
    Thank you in a advance if there's a way to solve it.

  • @ShashankJoshi1000
    @ShashankJoshi1000 3 года назад

    Also show how to use the ES6 functions in HTML button or any input field, onclick or onchange events.

  • @gaminggenes320
    @gaminggenes320 3 года назад

    Two questions here: 1) Why not reference to utils.js directly from tag? Why need to write redundant code to import and export, which may lead to forgetting to export and import new functions in the utils.js? 2) Must the utils.js be in the public accessible folder structure? Can the main.js import functions from other JS files in private folders (but in the same project folder) like node_modules? I'm asking this because some modules, e.g. Entity objects, are interoperable between server and client. Currently I need to duplicate such entity.js file between client and server, which is hard to ensure they're in sync. Not sure if the ES6 import/export could help. Thanks!

  • @lorenzo7098
    @lorenzo7098 2 года назад

    how to use fs with your architecture? I get an error when I do this
    because I don't understand why I can't do this, when I also use import ES6 and added with npm the fs package