The course is part of this learning path
This course offers an introduction to the NPM, Node Package Manager. NPM allows for the installation of third-party node modules to be used in your project. NPM is also an online registry for third-party node modules.
- Learn how to initialize a Node project using NPM
- Learn how to install a third-party module using NPM
- Learn the difference between dependencies and dev dependencies
- Learn how to write and modify existing scripts in the package.json file
- Learn how to install packages from an existing package.json file
This course is intended for anyone who wants to learn about Node.js.
There are no prerequisites for this course but an interest in Node.js would be beneficial.
Introduction to Node.js - NPM. NPM which stands for Node Package Manager is one of the many package managers available for node. NPM is included with the installation of node. NPM is also an online registry for node packages. This allows developers to create packages and share them with other developers. In order to use these packages and for other use cases, NPM requires a package file that is written in JSON to be included in your node project. Now back at the lab's environment, I have the terminal open. In order to create this file, I need to use the NPM init command. In the terminal, I will type npm init and this will trigger a series of questions. The package name is for the name of the project.
Now in the package.json file, there is a new property called devDependencies which currently lists jest. This only happened because I used the save dev flag. Otherwise, jest would be listed under regular dependencies. Packages that are listed under devDependencies are excluded from the application when the application is built for production or deployment. Now, an important aspect of the package JSON file is the scripts section. This section allows developers to create command shortcuts and automation options for a startup. Earlier, when I initialized the package, it asked me for a test command which I left blank. And the current message listed under test is the default when one is not given. And the reason why I installed jest was for demonstration purposes, and the execution command for jest is jest. So, in order for me to set up a test command, I will change this message to jest. Now in order to execute a script, I have to type NPM in the script name. So, in the terminal, I will type npm test. And the terminal shows jest being executed. And it comes back with a message, 'No tests found,' which is expected.
Now, I can expand upon the scripts and add as many scripts as I like. To demonstrate this, I'm going to create an index.js file, that entry point that was given earlier in the video. In the file, I'm going to console.log, start script. Going back to the package JSON file, under scripts, I will add a new property called start and I will give it a value of node index.js. So, what I'm indicating here is that when I type npm start, I want the node command to be executed on the entry point, the index file. In the terminal, I will type npm start and hit 'Enter.' And start script appears in the terminal.
So, the package.json file serves a few purposes, and I will cover one more use case for this video. To the left, there is this node_modules folder. I'm going to expand it and there are many subfolders here. Inside of these subfolders are more folders and files. This folder is initially created when the first package is installed. As additional packages are installed, their corresponding files and folders go into this folder. It is not considered a best practice to include these folders in your remote repository. That package.json file accessing multiple tracking system, it lists the packages and their version numbers currently used for this project. When somebody is working off a clone or fork of your repository, they can quickly get up and running by using this file. To demonstrate this, I will delete the node_modules folder. Now that the folder is gone, I will type npm install. In the terminal, it lists that 352 packages were installed in two seconds. Now, this was most likely faster than normal because there's probably some caching involved with the recently deleted node modules. And to the left, the node_modules folder has reappeared. And that's it, thanks for watching at Cloud Academy.
Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.
Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.
Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.
Farish is an avid powerlifter, sushi lover, and occasional Funko collector.