Placeholder Image

Subtitles section Play video

  • Hello.

  • Thanks for tuning in.

  • This is a recording of an online training session, which was a beginner level class on the note.

  • Yes, run time.

  • This was a freestyle interactive, no slides, just me typing could and talking about it.

  • So let me apologize in advance for all the speech fillers.

  • I did, however, out the pauses and the not useful parts so you can watch the whole training here on YouTube.

  • In one video, it's 2.5 hours, but it's also available at Jake's.

  • Complete that com slash gs and 19 where I split it by topic and too many videos.

  • You can also use the time stamps links in the description to navigate through the different sections.

  • If you think of any questions while you watch this, please feel free to join the just complete slack channel at just complete dot com slash help and ask us there.

  • I hope you enjoy this class and please share it with your network.

  • If you know anyone who plans on exploring no jazz, let's get started.

  • So this is ah, beginner class on notes.

  • Oh, no assumptions about what you know about No, this is your first steps in note, I might be assuming that, you know, a little bit of Java script, and I am definitely assuming that, you know, programming in general.

  • So depending on your skills, you might be you might be overwhelmed, or you might be disappointed with the content.

  • We all have different skills, and this is natural, but I hope most of you will learn a few things anyway, the classes recorded.

  • So if you're lost in the class, go fill the gap in your knowledge and come back and watch the recorded class s Oh, my name is Sam, are bona and I have been doing notes for awhile.

  • Currently, I do note on the back, and then I do react on the firmament.

  • But before that, I did a lot of other programming languages.

  • I did some tickle ruby.

  • I did a lot of peace equal.

  • Uh, but now it's all new.

  • So and I am a really big fan of note and everything.

  • All the libraries that make that are added on top of the jobs language, which itself is very flexible, and it can do whatever you want it to do.

  • S O If you want to see the stuff that I do.

  • I put wings here, feature links on this website, and also I publish everything I deal in Jay's complete dot com.

  • So there are video courses, books, interactive labs and a lot more just complete dot com, by the way, is a node project.

  • So this is written in note and react on the front.

  • And, um so I have been hacking on this platform, and it's really fun.

  • So no, no, does not is really funny.

  • Well, at least for me.

  • Uh, so let me set some expectations about the class today before we begin.

  • So this is not going to make you an expert on note that this is just your first step.

  • So just to get you familiar to raise your confidence in the platform and to get you started on the right path to learning, though, because I think ah, lot of people learn node the wrong way.

  • They focus on the packages that comes with no door like the third party packages that are very useful.

  • But I think there is a big benefit into learning the internal, You know, the raw running time before you go and use the packages.

  • And that knowledge is gonna make you make you use of those packages much better.

  • So the one thing that you need for sure is you need no eso.

  • So if you have a node version of me, make this a little bit bigger.

  • If you have a nude version that is 10 or higher, you're fine.

  • I think it will be fine, too.

  • But if you have an older note version, so go ahead and install Note.

  • I encourage you to always experiment with the latest and greatest always have the at least the latest lt s release.

  • But I actually do my development on the cunt version, even if it's an odd number.

  • His odd number, they're not gonna be lt s s o The way this works is even numbers are lt s which is long term support.

  • Odd numbers are not so right now we don't have any odd numbers.

  • This is an even number, but it's not lt s yet it goes into lt s maybe in a few months, right?

  • So lt s will become no.

  • 12 will become lt s and they start with No.

  • 13 which is not lt s it's just experimental.

  • But I always tried to have the latest node s so that you get to start exploring the new features that come the language and so that you you detect problems going away.

  • Because once you have the latest, if you're using something that is deprecate ID, you're gonna get warnings, and it's just so much better overall.

  • But definitely you have to test your systems on the lt s That is that you're gonna use in production.

  • Right?

  • So, uh, for example, just complete that Tom is running right now on this.

  • Lt s So even though I developed on my ad features on 12 440 if I have it, I still locally I still switch things, do the lt s and test things on.

  • Lt s just give the whole system ago on the lt s before releasing a big release s o.

  • There are tools for you to switch around, eh?

  • So if you're on Mac, you should definitely have homebrew.

  • Home brew is just the best way to, um to this one.

  • The missing package manager from note The best way to install note.

  • And it still many other packages around North But there's also, uh, MGM, so envy em and there's a version for it for Windows as well is no diversion manager.

  • So if you have envy em install, you can install note, and you can install different versions of note, and you can switch between these versions with the press.

  • If it meant.

  • So you get to experiment with many versions of off note, and that's that's great.

  • So what is what is known exactly?

  • So when you have the node command, when you install note, you get three commands.

  • You get the note command, you get me on PM command and you get the MP excrement.

  • All of these comes built in with them.

  • So no is a wrapper around the eight.

  • So v eight, If you're not familiar with the eight, the eight is the Java script engine, Not the juice.

  • The Java script engine.

  • Ah man, the A Java script, the Java script engine that powers crew itself.

  • So I am in crime right now.

  • And if I go in here and open up the council and put in any JavaScript the math at random, for example, this is a Java script function.

  • This is a job from Standard Library and Grown just executed that Java script.

  • It did that through the eight.

  • So they extracted everything Java script from Chrome into this V eight engine.

  • And, of course, every browser has its engine a different agent, so I e has chakra.

  • There's chakra.

  • There's Spider Monkey, and, uh, and a few of them.

  • Now this is different than executing a browser FBI.

  • So, for example, if I do document dot get element by I d right and or get element by whatever, uh, that is that is Dom a p I.

  • That has nothing to do with Java script it just have a job.

  • A script.

  • Intervene.

  • So the this is a job script object document is Jonah Strip object.

  • Get element by i.

  • D is a a p I on that object.

  • Same thing for things like set timer set time outs that interval.

  • These air just functions that gives you access to the browser, FBI, these air not Java script.

  • So set time is not implemented by V eight.

  • It's implemented by the browser itself.

  • So when you get the browser, you have two things.

  • You have V eight that runs everything Javascript and you have the browser itself that runs everything.

  • Dom and scrolling, for example, handles, scrolling, handle, re sizing.

  • All of these events are coming from the browsers.

  • But here's the thing.

  • Everything runs in a single thread.

  • So if you have a big giant for looping here, that takes for a 1,000,000,000 times that Luke is gonna block the browser from doing things like scrolling.

  • So if if the four loop is running in the background, the user cannot scroll your page.

  • So it is important for you to understand that this is a single threaded environment.

  • However, the browser gives you, uh, a p I a synchronous ap eyes to do things a ching earnestly so that you don't have to block a single thread that you have.

  • The exact same story is in note, so note.

  • Just like the browser is also a wrapper around V eight.

  • So you get to execute S O.

  • If we have the note command, just type in the no command note and you get into this mode that we call rappel mode running Val print Luke and its exact same mode that you get when you open the browsers.

  • Consul.

  • This is also a rappel moat eso in here.

  • You can just do math at random math at random and you get exacting.

  • Think so.

  • This is through note.

  • Right?

  • But note really, really didn't do anything here.

  • No, just past this command to V eight and VH executed math at random.

  • Veet told knowed what the result of math random is and notes printed it to our screen right here.

  • So it's going back and forth between node and M V.

  • Eight.

  • So the question is, why can't we just use the eight directly?

  • You can, of course.

  • But no, it comes with a lot of other benefits around the core Standard library, Right?

  • For example, there is, Let's say, uh, actually to be server, you can just start using an S T to be servers.

  • There's a package here called actually to be, and it gives you access to starting and asked you to be server and just like actually to be, there is a lot of them.

  • In fact, let's take a look at all of them right away.

  • So I'd like to point your attention to a very, very important key on your keyboard.

  • Ready?

  • It is the tab key, which is ah, above the caps lock.

  • This tacky is important because of the discovery ability it gives you discover, discover, ability, powers.

  • So, for example, if you are a ray is a Java script class, right, and it has many methods.

  • If you can't double tab on a ray dot so just do a ray dot and double tap.

  • You get everything that available on Ray.

  • This is This is a great discovery ability because I don't have to look up the things that you can do, especially if you have an object like a mystery object that you don't know what it is, right?

  • So if you have give you em and you don't know what em EZ, you can just see all the methods that you can do.

  • Look that look like Emma's Honore, because I get to join and split and keys and supplies in reverse so it isn't really everything I can do.

  • And if you double tap on an empty screen on an empty line, you get everything that you can start with.

  • So all the Java script classes you'll see here all the Java script classes because you know it's a wraparound job script, but also get everything that is note.

  • So you see, actually to be asked me to be s everything that starts with a lower case letter.

  • Most of the things that start with a lower case letter here O r built in note.

  • So libraries that just come with note you don't have to install them.

  • Those are things that you can just start using.

  • Eso no, is not a product from Cru Node is an open source community managed project.

  • But it uses a product from Google, which is the eight.

  • So v eight is Google.

  • Google manages the eight and node uses the eight and it uses other stuff, not just the eight.

  • For example, it uses open SSL uh, on a few other libraries like that.

  • Also, you can use node with other V ems so you don't have to.

  • So there's a new experiment or a project where you can use note with chakra, which is the chakra core v i e.

  • Thing.

  • You know the powers I ej s o.

  • You can run note without the eight with using chopper record, but by default.

  • When you don't know, you get the note that's wrapped around V eight.

  • Okay, cool.

  • So this list here is really important.

  • In fact, I would say, If you want a master note, you have to learn every single item on this list.

  • I still I'm still learning new stuff because a new stuff comes all the time.

  • But most of this stuff is JavaScript.

  • So if you are not familiar with things like date function, not a number errors objects reflect simple.

  • These are JavaScript things.

  • Week map.

  • Modern Java script.

  • Right, um buffers, Ray integers, ray floats, ray map promise, regular expressions.

  • Weeks at all of these are modern Java script that you get to use out of the box with notes and the other stuff that I'd like to point out to the things that we will explore today.

  • We So counsel is, is the same library that the same object that you have in your in your browser's council and they try to mirror the exact same interface for in note.

  • But counsel is note.

  • It's not V eight.

  • It's written in note on It's in the browser.

  • It's not V eight.

  • It's the browser itself.

  • So today we're gonna talk about console we're gonna talk about the module object will mention path will mention timers.

  • We're gonna talk about timers in general.

  • Uh, we're also going to talk about actually to be maybe a little bit of net s.

  • So there's a question.

  • What is mpm?

  • How is it related to note?

  • We're gonna talk about that.

  • So you're running node Js on shell on Mac.

  • What?

  • I should run on Windows Command Prompt.

  • Yeah.

  • So, on Linda's, you can run the cmd.

  • I would recommend that you instead of CMG that you install power shell eso power shell by in Microsoft is just a much better terminal than CMD.

  • And by the way, there is a new terminal coming to windows.

  • Who?

  • That is gonna be much better, right?

  • But power shell is the way to give for now.

  • What is the i d toll?

  • Too cold?

  • The idea either I'm gonna use is called V s code.

  • So, um, is probably, uh, the most popular editor.

  • Um, so if you will not run anything on, we're gonna We're gonna use it today, and I would recommend that you use it.

  • In fact, on Windows V s good will give you a better terminal so you can use the terminal in V s good instead of using, you know, the command prompt or anything.

  • All right, What is I turn to?

  • So I turn to is just a replacement for the built in terminal.

  • Um, it's just a little bit better than the build Internal has better team monks integration, but you don't have to use it, but you put it on your list.

  • Explore it.

  • If you if you see note, is not a recognised command, that means you have to fix the path.

  • So you have to put the node that was installed in the path.

  • So we talked a little bit about node.

  • Rebel right note also is a command line tool.

  • So if you do know Dash H, you get everything that you can do in node as a command line to Lucy, Ally of known.

  • So this is This is the seal I that's going to run a script in general, but also you can use it with other options so you'll see they use each year note options, and then you give it a script if you want it to run a script.

  • Another another usage is no.

  • The inspector will talk about that.

  • Have to inspect a square in note on all of these options.

  • Just scan through them.

  • They're handy.

  • I'd like to point out one particularly, uh, or a few particularly helpful options.

  • If you want to check the Centex of a nude file and not run it, just check the Semtex desks.

  • See if you want to execute.

  • Don't use e used P because P is executed and print s.

  • Oh, sure.

  • Let's, uh, let's explore that.

  • So if I have no dash B and you just give it any JavaScript and it will execute it and print it right away so that P is for print handy to just do things really quick on you and the other one that I'd like to point your attention to is V eight options the eight options.

  • So if you do dash dash V eight options, what you get is a big list of, uh, all the options that note can pass to V eight.

  • So let's let's explore that I'm gonna pipe it unless so that we get it.

  • Peach pie, peach.

  • So V eight options.

  • So in here you're gonna see a lot of things right.

  • You're going to see Harmony Flags.

  • You're going to see tracing flags and this list of, like, 400 or 500 options that you can do and and these are options that note passed directly to the eight s O.

  • If you wanna change the way the eight behaves, change the A little optimization off the way the eight behaves or or change some experimental flags, right?

  • If you want to experiment with Webber simply right here, there's a flags represent plea.

  • So most of these options are actually advanced.

  • I just want you to know of them s so you can come to these options and search if you're if you're looking to do partake some something particular with with V eight like something advanced.

  • Like like, for example, if you're If you're to work with the garbage collector in the eight Dr V.

  • Eight has a garbage collector.

  • You have some, uh, options here to control the garbage pointer to expose the garbage collector to manually run in the garbage collector eso.

  • You'll come here and see all the options that you can do.

  • All right, cool.

  • Another thing that you should be familiar with is knowed environment variables.

  • So when you run a node process right when you run the note command, whether you put in a file or you just run it with, you know, the rebel mode What I did here is I started on operating system process, right?

  • A process with a process I, D and node, uh, exposed this process object.

  • So there's a process object that has a few methods.

  • A lot of methods, actually.

  • And this process object is basically the interface between your node environment and the operating system environment.

  • So if you need to read anything from the operating system or if you need to write anything to the operating system, then he used this process object.

  • So it's the interface.

  • Eso This interface, for example, exposes wth e environment variables.

  • So all the environment variables that you have in your system just with process that if this give you access to everything, invite in the environment that you have, which is a cool way to customize your note session.

  • So you just put something in the environment.

  • Hopefully, I don't have any passwords in here.

  • You put something in the environment and you can just access it in a note, right, Including things like, um, you know, including, uh, the built in environment variables.

  • So the built in environment variables are, ah, special environment variables that if you set them, if you set them, then you get to change the behavior of note.

  • However, before I talk about that, let me show you how to control the environment.

  • Is this is This is important.

  • So let's say no dash P on.

  • Let's let's print process the end.

  • The user user is standard environment variable.

  • Right, So this is the user than a music.

  • If you want to change that, all you need to do is just pacify a different user, right?

  • And no one will just use a new value that you put in here instead off.

  • You know that set involved because I changed the environment variable before executing.

  • Know that of course I can do export or set on windows to change it for ah to change it forever.

  • You cannot change the very good question from a meat.

  • You cannot change the environment variable through process.

  • The end.

  • It's read on Lee thing, although you can actually change it s so you can do, you know, equal test.

  • But this will not change the environment.

  • Variable here.

  • Because if you do that right and try to echo dollar sign user after that, it did not change it.

  • So, no, I will not tell you that.

  • Oh, you can't change it.

  • It's not really immutable.

  • But, uh, you get a copy.

  • You don't you get a copy off the option.

  • All right, so let's keep going if you have.

  • So if you want to change some kind of behavior, I'd like to point your attention to a few of these environment variables.

  • Uh, 111 handy in particular is no debug.

  • So if you do, uh, if you use a library like, for example, actually to be and you'd like, note to the Biltmore information about what it's doing in actually to be you can you can do something like this.

  • You can know debug.

  • I think you just do, actually to be and then run your note command.

  • And this will tell no to debug more information when it uses the S t to be library.

  • So very handy.

  • When you start running into problems, you can change the path.

  • So this is another important one that we're gonna talk about.

  • We're gonna actually talk about that.

  • Next is how to control how No discover modules.

  • So let's go ahead and jump into the more serious mode, which is to run node in Tirana script.

  • So I'm gonna create a directory.

  • Um, eso no just do 06 19.

  • And I'm gonna go into this directory.

  • Uh, not this one.

  • And, uh, I'll just run V s code on this directory to start exploring things, and I actually let me just do everything envious goat s.

  • Oh, let's see.

  • I'll have the terminal in here.

  • Yes.

  • Good.

  • And make it bigger.

  • So if you have yes, good, you can use the terminal built internal.

  • Otherwise, you can still use the other terminal.

  • So I'm gonna do is I'm gonna create a violin here and call this file index dot Js and we'll start exploring things in Index digest.

  • Uh, so don't worry about us, Lynn.

  • For now, eso in here.

  • You can do anything that we just did.

  • So you could do math at random, right?

  • And now math at random.

  • And you can execute this file.

  • However, it will not happen Anything because it just executed, executed a value.

  • So this is not rappel note.

  • So if we execute note index that yes, it will do nothing.

  • Output nothing.

  • Because we're not a rebel mode.

  • Now we're in script execution node, which means if you want to put something, do the operating system the operating system that's running the process.

  • Then you have to use the interface to the operating system.

  • For example, you could do process.

  • So process the STD out.

  • Don't worry about the syntax, that right?

  • And in here you would do math at random and it will probably not work because right expects a string.

  • So let's try that.

  • I think he will not work yet.

  • It did not work because right here expects a strength.

  • So what do you do?

  • Well, you can change this to to streak, So in here.

  • What I did is Java script.

  • This is a job expression.

  • I just made it interesting.

  • Let's make sure this is working.

  • Yep, this works.

  • So this is the Java script expression, right?

  • And I fed it to node a p I.

  • So this is a note a p I process the STD out that right is a known a p I.

  • Now, of course, this is very raw.

  • Uh, you know, lower level because you have the consul object.

  • The consul object is also supported.

  • So I could do math at random through the council object, and that is going to put it as well on then.

  • It's a lot easier.

  • I didn't have to get it into a string.

  • I did not have Thio put a new line after the output actually colored it differently.

  • So counsel, that log is another note a p I.

  • And actually, if you go find the source code for counsel that log for note, you'll find that it uses process the STD out.

  • Right?

  • So there are many, many ways to do things that are lower level AP eyes and higher level A p I What if you counsel that log an object?

  • What if we cancel that log process for example?

  • Process is an object.

  • What's gonna happen?

  • Well, it's gonna it's gonna print a huge This is the default behavior for council alive.

  • It will print a huge object on go through all the nesting, so everything that you could do on not just the first level, uh, properties for process, but everything in it, including a raise and all the environment variables.

  • Right.

  • So this is not a very handy output.

  • What?

  • What do you do to make it handy?

  • Well, you can use other, higher level AP eyes, for example, the dirt one might be a little bit better.

  • Let's actually there.

  • One is exactly the same.

  • But the other one, except, uh, kept fighting or like except, uh, an object.

  • And in here, you can say depth is zero, I think.

  • Let's try that.

  • Yeah.

  • So it gave me on Lee the first level.

  • So without any nesting, what is the first level off properties that you have on the process?

  • Object.

  • So this is a cool trick where you can start exploring.

  • So it did not expand the big things that you have in process.

  • So this is me executing a script, right?

  • So we're gonna talk about how no does this magic?

  • How does it execute us grapes?

  • Can you turn on the bucking for all libraries using no debug?

  • I don't think so.

  • I think you can have a comma separated list, eh?

  • So you could do like f s and path.

  • But usually you want to debug one library, because if you turn on everything, there will be thousands and thousands of debugging lines, and it's not really helpful.

  • All right, so I am going to ask you a question and let's pretend that we are in an interview and I'm interviewing you for a node position.

  • So I'm gonna ask you an interview question.

  • I am gonna so you know about the, uh, you know, function X and function X, and we're gonna call it.

  • And Function X is gonna, uh, counsel that log arguments.

  • So arguments is a special key word that present everything that you passed X as an argument.

  • Right.

  • So let's pass X three arguments.

  • So 17 and nine.

  • So what is arguments in this case?

  • If I execute this code, it will be 17 night.

  • Now you notice the arguments.

  • Looks like an object, not honoree.

  • That's because Ray's in JavaScript are just objects.

  • They're just special objects where the properties are numeric just like that.

  • So it is.

  • This isn't already.

  • All right.

  • Cool.

  • So you understand that right now my question is, what is gonna happen if I just cancel all arguments without any function.

  • What is the output in this case?

  • So that's your first interview question.

  • Tell me what is the up in this case?

  • And if your answer is going to be undefined, you're gonna be surprised.

  • Arguments in this case is another.

  • All right, there isn't Ray here with how many arguments with five exact arguments.

  • Uh, so what are these five arguments and why is arguments working here?

  • Arguments is something that you get in a function, right?

  • Uh, why is it working top level?

  • Not what?

  • We're not in a function.

  • And what are these five arguments that you get?

  • This is core knowledge in nude.

  • So the reason arguments worked is because node has this concept of rapping rapping modules.

  • Right?

  • So this is what it does.

  • It wraps your code in a function.

  • So you get this IFI by default.

  • You everything you do in those gets this iffy.

  • Uh, if he is immediately invoked, function expression immediately involved function expression.

  • So you get this IFI by default?

  • No.

  • Just does this wrapping automatically.

  • And he also does other stuffs.

  • For example, it returned something from your from your file.

  • We're gonna talk about that.

  • But just in general, I want you to know that you get this.

  • If he by default, This is why arguments work.

  • So I kind of like this.

  • No.

  • Has this if he annexed, executed it immediately, That's what you get.

  • And the arguments that you get our, um, Mach one of them is module.

  • One of them is exports.

  • One of them is far, far mean on Durning.

  • So let's actually take a look at them real quick.

  • Thinking missing room?

  • Yes.

  • Oh, require is the 1st 1 What exports is the 1st 1?

  • Yeah, Exports is the 1st 1 I think.

  • And then you get require, and then you get module exports is the 1st 1 then acquire, then module.

  • You can actually take a look at them if you just do.

  • If you require module, you can take a look at the wrapper.

  • Ah, require module.

  • The rapper.

  • You see that Require module duck rapper require module that robber.

  • So this is exactly what you get.

  • Exports require module, file name and Durning.

  • This is the actual wrapper that no uses.

  • It's exactly that.

  • All right, cool.

  • So Here's the thing if you, um if you have two files.

  • Right, So we're gonna have another file in here.

  • Index one index to, uh, index one and index to dodgy s, and I'm just going to split them split down.

  • All right, So you've got index one an index, dude.

  • Right.

  • What I want you to know is that every function, every file that you initialize gets this magic iffy, right?

  • Eso if I, um if I cancel log exports in here, right, experts is something that you get from the arguments.

  • Right?

  • And I counsel log it in here.

  • These exports are different right there.

  • Not the exact same object.

  • Although, uh, if you execute them, they're exactly the same.

  • It's an empty object, right?

  • But this empty object Oh, uh, Index, to save this empty object that you get an index doing in it, X one, These are different objects.

  • They're not exactly the same object.

  • Why?

  • Because they're coming from an argument that no passes to your if he function.

  • So that's important.

  • So all of them are different, right?

  • Exports require module Found them in their name.

  • Finally it is.

  • Of course, they know the name of the father you're executing.

  • And their name is the name of the directory where your file is that you're executing.

  • So you need to learn about exports and, uh, on the quiet.

  • These are two basic things in No, this is the note, you know, module dependency system.

  • So next we're gonna talk about these two in rebel mode.

  • You don't have arguments because you're not really executing a file.

  • This is when you execute a file.

  • This is how node manages files on modules.

  • Ah, how is Consul of working when you run node file name as consul Log is available only in browser.

  • Yes, Consul law is available in notes, so node dries to mirror the browsers.

  • A p I right.

  • So when it comes to input and output not not done so there's no doubt you can't do document.

  • Uh, you can't in here.

  • You can't say Oh, look anywhere.

  • Really?

  • You can't say document document.

  • Don't get elements by I d.

  • That is not going to work.

  • Why?

  • Because you don't have dumb.

  • So this will tell you undefined.

  • Some kind of better document is not defined.

  • Right?

  • So it's not exactly the browsers however, node opted to implement things like council like sets time out.

  • Ah, and many f and a few other things that exist in the browser environment.

  • But they're not JavaScript.

  • Right?

  • So these AP eyes are built in node and their implementation is different than the implementation of the browser.

  • Eso and V eight has nothing to do with them.

  • The eight is just gonna organize the execution of functions on callbacks.

  • But these, uh, these AP eyes are note cool.

  • That's why.

  • So if you go grab the source good of note, you're gonna find the council object and the log function in the node source code, right?

  • Not envy.

  • Eight.

  • And not for any problems.

  • Okay, cool.

  • Good questions.

  • Keep them coming.

  • So we're learning about this.

  • Uh, if he function if he rapping function right.

  • Cool.

  • So what is exports?

  • Exports is an object that you can change.

  • So exports is immutable object.

  • So we can say exports dot Answer is 42 you can put any number of properties on exports.

  • And when we execute this file, export is gonna be whatever you do, you define it.

  • So it's just a special object that is available globally for you.

  • It's also an alias to module exports.

  • So if I do module the exports another answer, then I am changing the exact same property.

  • So you see how I changed it here and I changed their so exports is just an alias to module the exports.

  • Cool.

  • And here's the thing, node.

  • When it manages a file, it returns module the exports.

  • So whatever you put on exports, that would be the return value off the index file the index module.

  • Now, why is this handy?

  • Because another file might require your module.

  • So let's call this module one.

  • This index is your module, right?

  • And you want index to to use your module to use index.

  • What you do is you require you just used require and you pass on a string.

  • And you say index to is going to require index one Dutch.

  • Yes, just like that index to is going to require in this one.

  • So note what's gonna happen.

  • Uh, and in here, I'm not gonna cost a log.

  • I'm just gonna Consul log.

  • Uh, let's Consul log in Index Duchess.

  • Right.

  • Let's constant love and index suggest so just by requiring index if I execute index to what's gonna happen is note is going to look at this fine require and it will require this file so it will execute this five.

  • So I'm gonna get the council long message.

  • Although I'm executing index dude, So that's that's the story of require.

  • But here's how it's helpful.

  • It's helpful because require gives you access to whatever this module, the export things is returning.

  • So whatever you put in modular expert, you get it in here.

  • So in here, you get an object index.

  • I'm just gonna call index, and I'm just gonna log Index.

  • So guess what index is going to be index after index that in index that?

  • Yes, this Here, uh, you get the exported module.

  • So this is module the experts.

  • So whatever you put on module, the exports or its alias exports, you get it out when you require a file, so require is dependencies module dependencies.

  • It's similar to import and export in jobs.

  • In fact, you can also use important export.

  • And I'm gonna show you how that because this is new, this is new.

  • In knowed you get to use exports and imports as well.

  • So any questions about require and this exports business because that is that is core knowledge, all all the packages out there that are written for note.

  • They use this Centex, they use exports and require.

  • All right, cool.

  • So I'm gonna clean all this and just possibly log Hello?

  • And in here I am going to require in that study s and I'm gonna execute the father.

  • That's requiring.

  • So this will output.

  • Hello.

  • Right.

  • Cool.

  • Here is your second interview question.

  • What's gonna happen if I do that?

  • If I require Index three times what's gonna happen?

  • So go ahead and try and answer.

  • This will output.

  • Hello.

  • One time.

  • What?

  • Why did they put hello?

  • One time?

  • I'm not three times right.

  • And the reason is note has this require function in node has some magic cashing.

  • It doesn't evaluate the file every time you require it.

  • Evaluated once and then the second time it reads it from the cash.

  • So this is why in a application states react application or an angular application you require react all over the place.

  • You require angular all over the place.

  • Multiple files are going to require reactor angular or view.

  • And on Lee, the first require is gonna actually do the work.

  • The second acquire will just read it from the cash s.

  • Oh, well, another interview question is, what if I do want this file to counsel law?

  • Hello?

  • Every time I require it.

  • And the answer is you really can't you require you get cash?

  • That's just that's just the reality.

  • However, you can delay what you execute in the file by making it return a function.

  • So remember, module the experts right module, the exports is our FBI.

  • So instead of having the file council log directly, you can have it export a closure.

  • You can have an export, a function closure, and you execute that function every time you want it.

  • So if I make module the exports into ah function and I'm gonna use error function in here, but you can just use a regular function and I make this function Consul love.

  • Hello.

  • Instead of that now, I have delayed execution.

  • Now, every time I require index dot Yes.

  • What do I get every time I require index suggest I get back a function, it does not have any code.

  • It will just cash the function reference.

  • And I can just execute dysfunction.

  • Doesn't get back from index suggest.

  • So if I do that and I execute this, I get hello three times.

  • So we use this trick.

  • Ah, lot.

  • You know, we make the module exports a function, and we execute the cook in that function.

  • So a little bit of delayed execution gets cached.

  • Is the function itself, not the function Execution.

  • Cool.

  • All right, so there are many cases where you get me.

  • You want to execute something in the file?

  • Multiple times.

  • Right?

  • So this is how you do it?

  • You always ex execute a function.

  • In fact, if you need the file to have multiple capabilities, what you do is you make it export an object.

  • And in the object, you have your capabilities.

  • You do like function.

  • One function to function.

  • Three.

  • Right.

  • So this is another way to export FBI.

  • So how would you do that?

  • How would you invoke function one.

  • Now, when you get back is an object, right?

  • It's not a function.

  • It's an object.

  • And because it's an object, it's an object that has f one Aiken do dot f one right dot F one because it's an object and execute F one right, and this will work as well.

  • So whatever you put in here, this require is just going to use it, right?

  • So and you can put anything right, you can put primitive values.

  • You can put a raise object.

  • You could have like a proxy, anything that is supported by the language.

  • You can use it as the exports value in your module, and you get to use it exactly as we define it, right?

  • Whatever you have, we have an object has an F one property, so I'll just call it like get the F one property.

  • And then because the F one property itself is a function, I can execute F one right?

  • So there's nothing magic in here is just Java script mixed with the A P I.

  • That note gives you to acquire functions.

  • Now this is a little bit different than the import export Centex, that is, that is native to the JavaScript language, right?

  • This module require business is no good way to do module dependency and existed since they won in notes.

  • So, like 10 years before Java script decided to have a mantra dependency as well.

  • Now, the problem is you have two systems.

  • One has been used for 10 years, and the brand new one that everybody is gonna start using because it works in browsers as well s So what do you do?

  • Well, here's what you do you really understand.

  • Require module export, understand it really good, because you're gonna have to deal with packages that are written in this Centex for at least 5 to 10 years into the future.

  • If you're working in the project but in your own product, you're starting a note project today.

  • I would recommend that you use the import export Semtex, not the requires indexed.

  • Now they have different implementation.

  • And I'm gonna tell you about that in just a little bit.

  • Just should so that you aware.

  • But I'm gonna show you the import export Centex first.

  • So let's go ahead and do that.

  • So let's do this exact same example.

  • But instead of working with require, let's do it with with important expert Right, So here's the thing.

  • If you have knowed 12 if you have No.

  • 12 you can use import and export out of the box.

  • It's actually supported.

  • But remember that it's not lt s so No.

  • 12 is not l FIA's yet.

  • Let's see.

  • N v M l s.

  • Ah, system No.

  • 12 NDM use system envy MLS remote.

  • While we're talking about it, I'm just gonna get the latest and greatest note, which is V 12.

  • 14.

  • I'll use that.

  • So envy em, install this thing, and this will install the latest, eh?

  • So what do you do here is you create a package that Jason file to start managing your dependencies, Right.

  • Let's actually name this M 1 a.m. two because they're kind of, uh, different.

  • So m one m two.

  • And I'm just gonna call him to in here someone someone that just in one and into the exact same example.

  • I just need them in m one and two instead of what I had before and on.

  • You can run them with the latest notes.

  • So this syntax is not going anywhere.

  • So although we have knowed v 12 now, we can still run em too, and we get the exact same behavior.

  • So expert on require is exactly the same.

  • But I want to do is I'm gonna change this into important export.

  • But to do that, I need the package.

  • Jason, file.

  • So what is the package That Jason file a package?

  • That Jason file is the file that you can use to manage matter information about your node projects.

  • So every node package out there has a package that Jason file to tell you its name.

  • Its version.

  • It's get repositories e how to run things in it.

  • So you create a package that Jason file with the N.

  • P M.

  • Command.

  • So what?

  • N p.

  • M is the node package manager on.

  • We're gonna talk about it in the next hour after the break.

  • But for now, the first step is to use the M.

  • P.

  • M.

  • Innit command to create a package duck Jason, file that we can use to control things in notes.

  • So at PM in it and this will ask you a few questions us or it can detect if you do dash.

  • Why, which is for yes.

  • It says, Just try to detect whatever you can detect from this.

  • This object.

  • Just give me a package that Jason file.

  • So this will create a package that Jason file using the name of the directory using the fault version will try to find if you have get installed, it will try to find the get remote that you have.

  • It just detects a few things.

  • So in package that Jason, you get to specify information about your package.

  • So, for example, if you bump put your name on the package so that when you push it people know if you wanna have keywords so people can find your package in the search, you can use this meta information So there is a new, by the way, the only two required properties and packaged adjacent just name and version.

  • These are the only required.

  • Everything else is optional.

  • So the nuke the new, uh, property that you can use here on package that Jesus is the type property.

  • And if you specify your type as module instead of common Js.

  • So the other one, I think, is common common jazz, which is the name that is that we use for, you know, the require exports things.

  • But if you specify your type as module, then node will switch years and just assume that you're just gonna use ECMO script modules all over the place.

  • So there's no require and experts.

  • In fact, I think if we try to execute it now, it will not execute.

  • It will not be happy, it will say.

  • Uh, well, I actually indicated that does not work.

  • Oh, we need a flag.

  • Um, so we need the experimental modules.

  • There you go.

  • See?

  • So this flag is actually gonna go away when node goes into lt it.

  • I'm not sure when they're going to remove it, but they will remove it.

  • But this is this is how it is gonna behave when it switches to the ECMO script the s and modulator.

  • See, this is now the sm modulator.

  • Eso it tells you I don't know require because I'm assuming that you are in a in an ECMO script module, right?

  • You're not in a node larger.

  • You're an ECMO script module, which means he cannot use exports you cannot use required.

  • So what can you use?

  • You use export default and in here you passing the object that you'd like to export.

  • So it's kind of similar to what you did.

  • But in here, where we have a little bit of different Centex export default.

  • So instead of module the experts, you do export default and instead of require what you do is you import, you use the import keyboard.

  • So import em one from dot slash and one Duchess import M one from the slashing Monday jazz.

  • And now you get to do em Wonder F one and execute that three times so you'll see a little bit of different Centex.

  • Uh, but it's basically the same module dependency.

  • So this works right.

  • You get a warning that it is experimental, but it's really just it works fine.

  • They're just making sure that the FBI is stable.

  • So

Hello.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it