02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Setting up dependencies the right way

Setting Up Dependencies the Right Way

In this lecture, we cover foundational concepts for configuring dependencies and dev dependencies using NPM to build a scalable and high-performing React.js application. Here's a breakdown:


Key Concepts Discussed:

  1. Types of Dependencies:

    • Dependencies: Required in the runtime environment (e.g., React, ReactDOM).
    • Dev Dependencies: Only needed during development (e.g., Webpack, Babel).
    • Peer Dependencies: (Not covered in detail) Used when building sharable NPM packages.
  2. Configuring NPM:

    • Initialize a project with npm init -y to auto-generate a package.json.
    • Use npm install --save for runtime dependencies and npm install --save-dev for development dependencies.
  3. Tools Discussed:

    • React and ReactDOM: Added as dependencies.
    • Webpack and Webpack Dev Server: For bundling assets and creating a development server with hot module reloading.
  4. Optimizing for Production:

    • Only include essential packages as runtime dependencies to reduce memory and resource usage.

Hands-on Steps:

  1. Initialize NPM:

    npm init -y
    

    This creates a package.json file with default values.

  2. Add Dependencies:

    npm install react react-dom --save
    

    Stores runtime dependencies in the dependencies section of package.json.

  3. Add Development Tools:

    npm install webpack webpack-dev-server --save-dev
    

    Stores development-only tools in the devDependencies section.

  4. Create Webpack Configuration:
    Generate a webpack.config.js file for packaging JavaScript, CSS, and other assets.


2024 Updates & Best Practices:

  • ES Modules: Use ESM (import/export) natively supported by modern Node.js environments instead of CommonJS (require).
  • Package Manager Updates: Consider using pnpm or Yarn for improved performance and lockfile integrity.
  • Bundler Alternatives: Explore tools like Vite or Parcel for faster development builds over Webpack.
  • Tree-Shaking: Ensure dependencies are optimized using modern tree-shaking capabilities in Webpack or alternative bundlers.
  • Environment Variables: Use .env files and libraries like dotenv to handle environment-specific configurations.

With this foundation, you're now ready to set up your dependencies in a way that optimizes performance for scalable React applications. Up next, configuring Babel 6 for high performance!

Video Transcript

00:00Welcome to Mastering React, scalable
00:03and High Performing Apps.
00:04My name is Ben Fala
00:06and this is a packed publishing production.
00:09Now if you're watching this title, let's make sure
00:12that you're the one that needs to watch this title.
00:14So who is this title for?
00:15Well, you're a React JS developer already.
00:19Uh, for example, you've taken one
00:21of my multiple courses in React or a few of them, um,
00:26or have been already
00:27and built out a React application that leverages
00:32ES six JSX.
00:33You've created a React application.
00:35In short, you have a basic familiarity also with Node
00:39and NPM, although it's not critically mandatory,
00:42but we're gonna be working a lot with a node
00:44and NPM throughout this title.
00:46So you'd be better off to just play around a little bit
00:49with it before you start taking this title.
00:52So with that said, so now that you know we know who you are,
00:56what are you gonna actually get out
00:58of Mastering React scalable and high performing apps?
01:01Well, the answer is by the name implied.
01:04We are gonna be building up really high performing react
01:08apps and we're gonna learn what should you be focused on
01:10when you're building these applications.
01:13We're also gonna take a deep dive into performance tips
01:17for ES six and JavaScript.
01:20We'll scale up our react
01:21by moving our re our initial rendering into the server,
01:26reducing the load on the user,
01:27and also enabling us to cache that rendering up.
01:30With that said, speaking of that, we'll also talk about how
01:33to prepare our build for production as we look at how
01:37to build out our CSS, our SaaS, R js, our React
01:40and our server, what is the most optimized way
01:43to connect all these stuff together
01:44to create a highly performing application?
01:47So by the end of this title, you'll be a React developer
01:51that knows how to create a production ready setup for React.
01:56With that said, let's jump into our, to our first section,
01:59which our first section is really about thinking about the
02:01concepts of performance in it.
02:04We're gonna go ahead and understand
02:06and NPM what our dependencies
02:09and what dependencies should be in what category.
02:13We're also gonna learn.
02:14Besides knowing what dependencies are,
02:15we're gonna take a look at how
02:16to set up Babbel six in the most optimal configurations.
02:20There are many different configurations out there.
02:23Babbel six is a transcriber that takes different versions
02:25of JavaScripts and moves them into ES five.
02:28In this, in this section, we're gonna learn about
02:31what is the best configuration to set that up.
02:34We're also gonna talk about starting to talk about how
02:37to enhance our code by taking a look at constants,
02:40which are new to ES six,
02:42and how could we start working with them already now
02:44and why are they such a big boost on performance.
02:47With that said, let's jump into our first lecture,
02:50which is about setting up our dependencies the right way.
02:54When you're working with NPM, you're gonna find
02:56that there are three types of dependencies,
02:58Ones that are called dependencies, dev dependencies,
03:02and peer dependencies.
03:04Although peer dependencies will not be a subject within this
03:06title because peer dependencies relate
03:10to building NPM packages,
03:12our focus is gonna be on dev dependencies and dependencies
03:16and how they relate to performance.
03:18So let's jump right into it.
03:20To really know how to create highly performing applications
03:24with React, you really need to know how to configure
03:27and set up servers effectively more accurately.
03:31Node PMM and NPM now node js and NPM.
03:35With that said, I just wanna show you the starting file
03:37that we're gonna be working with that is inspired by
03:41a title which I have,
03:42which is called Building React User Interfaces.
03:45In that title we created a quite, quite a few components
03:48and bootstrap and then showed you how
03:51to even animate them using React
03:54and once they're animated even how to animate them out
03:58and quite a lot of things we covered in that title.
04:01And among them, I want to take one step out of the,
04:04of our starter files, which I deleted from that sample.
04:08All the configurations of NPM
04:10and of Webpack, which are, is the technology
04:12that we're gonna use to package up
04:14and prepare our applications.
04:17So let's go ahead and see our starting file.
04:19And our starting file currently is literally
04:21just two folders.
04:23One has a source file source folder, which has quite a lot
04:26of source files that have been prepared in advance in
04:29that previous title that if you wanna learn how
04:31to create them, then uh, urge you to check out our title
04:35Building User React User Interfaces.
04:38Now in our public folders there's a few usable files as well
04:41that have been prepared and ready.
04:43And all that's left for us is to set up
04:45and configure the server, which is exactly what I want
04:47to go ahead and do now.
04:49Now in step number one, I'm just gonna go into
04:52that folder in my terminal.
04:54Step number one is I want to go ahead and call NPM
04:57and initialize a basic package.
05:00Now I'm gonna say yes to the interview.
05:01So automatically all questions will just be filled out
05:04by their default values
05:06and once I've done that,
05:07it's gonna automatically create a package dot jsun file,
05:10which I'll be working with throughout this title.
05:14Now I'm gonna go ahead and modify some of these values such
05:16as name and versions in between the title just
05:19so we have your, the right information,
05:21which is not relevant for the build itself,
05:23but more just aesthetics of giving it a proper name
05:26for our title, um, and so on and so forth.
05:28Now with that said, the next thing that I want
05:31to do is I want to go ahead and I want
05:32to touch a file which we'll be working with quite a lot,
05:35which is called Webpac dot config js.
05:39Alright, so let's continue
05:40to our first performance related task
05:43and this first performance related task, it relates
05:46to installing things and hopefully by now you already know
05:49how to install packages with NPM.
05:52And if you don't, please check out my title.
05:54Um, mastering Well Full Stack React Development,
05:59a Pact Publishing title.
06:00Okay, with that, with that said, let's go ahead
06:02and show you the basics of it.
06:03And I'm gonna go ahead and call in NPM install
06:06and I'm going to put here a dash dash save.
06:09And when I say dash, dash save
06:10what I'm actually telling Pact, not pact,
06:13that's the publisher what I'm actually telling NPM
06:15or my project to store this dependency that I'm saving
06:20into our configuration file.
06:22Now our configuration file is that file
06:24that we've just created a moment ago,
06:26which was called Package js ON,
06:28which has the basic information of our package.
06:31For example, for our name, I'm gonna call it our um, react
06:36dash performance, perform
06:41Beautiful React perform will be the name
06:44of our project is version number one.
06:45Now, by the way, importantly, oh,
06:48that was a very Donald Trump thing to say.
06:49Very importantly, um, bear in mind
06:53that you don't wanna make changes
06:54to this file while you're installing packages.
06:58'cause what's actually gonna happen is
06:59as items are gonna be saved, specifically
07:02what I'm putting here, dash, dash save,
07:04and I'm gonna go ahead here
07:05and save a dependency that we're gonna be working with.
07:07Two of them actually react and react dumb.
07:10Now, I need know already
07:12because our project is also using an Add-on,
07:15which is called CSS Transition Group,
07:20which we're not gonna get into
07:21that we've explored in our UI title.
07:25Well, what's happening here in this process
07:27as this is being installed, as these packages are being sold
07:30because they're gonna be used with our library
07:33in our real world live environment once we start our
07:36application, because our real world application needs
07:39to know how to use React to add more items.
07:42So we don't, it's not something
07:44that we could move onto the server
07:45because this is something
07:46that we need our client to be able to do.
07:48It needs to interact with the React Library to be able
07:51to add elements.
07:53So is the animation.
07:54So it needs to be able to interact
07:56with the React animation library.
07:58And that is why when we're installing these packages
08:02and notice how they got into our dependencies,
08:04we're installing them into our dependencies.
08:08Whenever we put something into our dependencies, it means
08:10as when the server is already live
08:12and running, we need those packages.
08:15With that said, whenever you get to a scenario
08:17where you just don't need these packages anymore,
08:20once your build is complete, then those type
08:23of packages you don't want to put in your dependencies
08:26'cause they will add memory le memory usage into
08:30your dependency list.
08:31So in this case, what I'm gonna go ahead
08:33and do is let's go ahead
08:34and install a few packages that are not dependent,
08:37that don't need to be in our live environment.
08:41One of them, for example, is that web pack
08:43that we started talking about.
08:45So let's go ahead and install web pack.
08:46So I'm gonna go ahead to npm install dash, dash save only.
08:50This time I'll add another dash for Dev.
08:53And what that is gonna say is that we're gonna need,
08:55need this for the development of the server
08:58but not for the server once it's running live.
09:01And in this case, I'm gonna go ahead and call the Webpac
09:04and Webpack dev server.
09:07Now Webpac is a packager
09:08that packages up our application more accurately,
09:11usually CSS and JavaScript
09:13and anything that is, um, client driven for that matter,
09:18although could be used also for the server.
09:19But generally webpac is used to package up things
09:23for the web and the Webpac dev server is an a intermediate,
09:27not production, but development server that enables you
09:30to refresh things and create hot refreshes on the fly
09:34and other things of that sort,
09:35which will speed up your development.
09:38Now, uh, we've talked about it a lot in other titles,
09:40but we'll talk about it briefly in this title as well.
09:43Actually more than briefly
09:44because we're gonna take a deeper look into how
09:46to build those, that Webpac dev server
09:49configurations in general.
09:51Okay? So that's an important aspect.
09:53Step number one is things that you're only going
09:55to use in the production itself.
09:58Well, things that you're gonna use in production
09:59or development, you're gonna put in your dependencies
10:02by using the dash dash save.
10:04And whenever you need to use the dependency
10:06that is only used for the development purposes,
10:08then you're gonna use the dash dev.
10:11And once you do that,
10:12then you're gonna add dev dependencies.
10:14And in those dev dependencies,
10:16you're gonna have those packages.
10:19In this lecture, we saw the difference
10:21between dev dependencies
10:22and dependencies in the next lecture.
10:25We'll continue down the path as we configure Babel six
10:28to be highly performing.
10:30Okay.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!