Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
A package.json file is the standard way to manage your dependencies in a project. In this video you'll learn how to create your first package.json file.
Terminal Commands
- Clear the screen
clear
npm Command Line Usage
-
Get help for a command
npm <command> -h
- e.g.
npm init -h
-
Initializing a
package.json
file-
npm init
-
npm help json
for what each of the fields in thepackage.json
does
-
-
Installing an npm Package as a dependency
npm install colors --save
-
Installing an npm Package as a development dependency
npm install mocha --save-dev
-
Install all packages specified in your
package.json
npm install <flags>
- e.g.
npm install
- e.g.
npm install --python=python2
-
Running npm in a production environment
NODE_ENV=production npm install
Git Usage
To ignore files create a .gitignore
and add the files and folders you want to ignore.
Example .gitignore
file:
node_modules/
.DS_Store
config/database.yml
-
0:00
As you create a project that uses MPM modules, you'll find that your
-
0:04
node_modules folder is filling up with megabytes of package files.
-
0:08
That's fine, you're using those files in your project.
-
0:11
However, what if you want to share your projects on GitHub or
-
0:14
simply use Git to manage your project?
-
0:17
You certainly don't want to upload all of those packages to your GitHub account.
-
0:21
You also don't need Git to keep track of those files.
-
0:24
After all, you will be editing those package files yourself.
-
0:28
In addition, you'll also want to wait to make sure your packages stay up to date.
-
0:33
Sure, you could update each package by hand by using npm.
-
0:37
But there's a simpler way to keep track of your packages and their versions.
-
0:40
We often refer to this as Managing Package Dependency.
-
0:44
With JavaScript projects,
-
0:46
we use a file in the root of the project called package.json.
-
0:51
This is what npm uses to manage all the dependencies for the project.
-
0:55
Let me show you how to create that file.
-
0:58
We're back in our password project, and I'm happy with bcrypt.
-
1:02
I want to make sure that my other colleagues and
-
1:05
I use the same version when we're working on the project.
-
1:09
Instead of manually creating the package.json file,
-
1:13
we can use npm's init command.
-
1:19
Hit Enter, and it says this.
-
1:21
This utility will walk you through creating a package.json file.
-
1:27
It covers the most common items and tries to guest sensible defaults.
-
1:32
See npm help json for definitive documentation on these fields and
-
1:36
exactly what they do.
-
1:38
Use npm install package name --save afterwards to
-
1:43
install a package and save it as a dependency to the package.json file.
-
1:49
Okay, let's remember that --save flag for later.
-
1:52
That'll come in useful when we're adding additional modules to the project.
-
1:56
Now the first question is the name of the project.
-
1:59
By default is suggested the folder we're currently in.
-
2:03
That's what in these brackets, the default value.
-
2:06
And if you don't put anything in, npm will use that.
-
2:10
Let's call this something more descriptive, like hash_generator.
-
2:19
Hit Enter and we'll do 0.0.1 as our version number.
-
2:24
The description,
-
2:28
A password hash generator.
-
2:35
And the entry point, app.js is fine, so I'm just going to hit Enter.
-
2:41
The test command whether we have any tests for our project.
-
2:43
If you're going to create a project professionally you should really have
-
2:47
tests.
-
2:48
Tests are a way to ensure your code works and can handle each case scenarios.
-
2:53
We don't have a git repository at the moment,
-
2:55
we'll have to manually add that to our package.json file later on.
-
3:00
Keywords are used to aid people to find your project when searching on npm.
-
3:06
But this is a private project, so we don't need it.
-
3:08
And for author you can put in your name there.
-
3:12
So I'm just gonna put Andrew Chalkley, and
-
3:15
now any appropriate open source license here.
-
3:20
I'm going to use MIT.
-
3:22
Including a license in your project helps other people understand how they can use
-
3:26
your code in their project.
-
3:28
MIT is a common license used for open source projects.
-
3:31
Some examples of projects that are under the MIT license are jQuery and
-
3:37
Ruby on Rails.
-
3:38
And if everything looks okay, press Enter.
-
3:41
Now this has created a package.json file.
-
3:45
Let's take a look inside.
-
3:50
It's included all of the information we inputted, and
-
3:52
it's looked in the node modules folder and has found that there's the bcrypt module.
-
3:57
And it's included that as a dependency for our project.
-
4:01
How about another dependency?
-
4:03
How do we add that to the package.json file?
-
4:07
Instead of adding it manually, let's let npm do that for us.
-
4:10
I want to add some color to my program.
-
4:13
I want the generated hash to show up in green in the console.
-
4:17
I know that there's an npm module called Colors,
-
4:19
that lets you add colors to your command line applications.
-
4:23
To install it, we type,
-
4:29
npm install colors --save.
-
4:36
Remember when we did the npm init command?
-
4:39
It said that we could use the save flag for it to get saved as the dependency and
-
4:44
update the package.json file.
-
4:48
Hit Enter and we see that colors is being added to our package.json file.
-
4:56
You may see warning messages like this when you install packages.
-
5:00
Warnings are not errors.
-
5:02
Errors are things that would prevent your code from working.
-
5:05
Because we didn't include a Git-repo or
-
5:08
a README file, it's bugging us to create one.
-
5:11
For now we can ignore it.
-
5:13
Let's include colors in our project.
-
5:17
Open up our app.js file.
-
5:20
We need to require the colors module.
-
5:30
Inside the console.log method, we can add the .green
-
5:34
property at the end of the hash stream to make the color of the output,
-
5:38
when we call the console.log method, green.
-
5:47
Let's run node app.js.
-
5:52
Awesome.
-
5:52
Our hash is green.
-
5:54
The package.json file makes it easy to install node modules used by our project,
-
6:00
even if you haven't already installed those modules.
-
6:03
This is useful when sharing your project.
-
6:06
For example, you don't want to put all
-
6:08
of the many modules used by your projects up on GitHub.
-
6:12
Because that wastes just space and updated versions of the module maybe available.
-
6:16
In other words, if we put this project on GitHub we should include the package.json
-
6:21
file but not the node_modules folder.
-
6:24
There's a file that you can create that will instruct Git
-
6:29
to ignore files and folders and that's a .gitignore file.
-
6:42
In there you would just add the path to the file or folder you want to ignore.
-
6:48
In this case it's node modules.
-
6:50
Since I can't edit it in workspaces, I can just rename this to .txt.
-
7:00
And type what I want in it.
-
7:03
And then rename it back
-
7:11
Imagine we've cloned the project from a GitHub Repo, and
-
7:15
we don't have the node_modules folder.
-
7:19
Let's delete the node_modules folder to replicate that scenario.
-
7:25
When we run our app now, it says it can't find the module colors.
-
7:33
That's because it's no longer in the project folder.
-
7:37
Let's install the packages.
-
7:39
You could do this by manually adding each module
-
7:42
using the npm install followed by the module name.
-
7:45
However, if you have a lot of modules in a project that would be a lot of work.
-
7:51
There's a much easier way, remember the first usage of the npm install
-
7:56
command was just npm install, where you don't specify a package.
-
8:01
Well, npm will look for the package.json file and
-
8:05
install all the dependencies it specifies.
-
8:09
Remember we had to include a build flag for our bcrypt module.
-
8:14
So in this case we just need to include
-
8:18
--python=python2 on any of the device that has Python 2 installed.
-
8:23
Or if your packages don't require it, you don't need to include a flag, or
-
8:27
you can just do npm install.
-
8:30
And let's see if our project now works.
-
8:36
Yes, it does.
-
8:37
And there's our generated password.
-
8:40
Finally, there's another type of dependency, a development or
-
8:43
dev dependency.
-
8:45
There dependencies aren't needed for the application to run, but
-
8:48
are used as you work on your project.
-
8:51
A good example of a development dependency is a test framework.
-
8:54
The reason why you'd want to list this module as a development dependency is to
-
8:58
reduce the amount of unneeded files in production.
-
9:01
For example, if you have a website,
-
9:03
you may have testing in your development environment.
-
9:06
But you don't need that test for a MAC once it's deployed.
-
9:09
When you deploy your code, you can just use npm install.
-
9:12
And it will install the dependencies it needs for your project to run.
-
9:16
A popular test framework in JavaScript is Mocha.
-
9:19
To add this as a DEV dependency,
-
9:23
we'll type npm install mocha --save-dev.
-
9:34
Take a look at the package.json file now, And
-
9:40
you'll see there's a devDependencies key, and there's mocha there now.
-
9:45
Sweet.
-
9:46
Without going in depth on what's involved with testing,
-
9:49
let's see how we can use mocha in our development environment.
-
9:54
Remember, mocha is now installed in this environment.
-
9:57
So we can now use it.
-
9:59
When we first created our package.json file we were asked about our tests.
-
10:04
We can now fill that in.
-
10:06
The mocha package installs a mocha command that can be utilized by npm.
-
10:12
We can replace this with just mocha.
-
10:16
We can get npm to run our mocha command by using npm test.
-
10:21
But before we do, mocha requires a test folder.
-
10:24
So let's create that.
-
10:36
Let's use the npm test command and hit Enter.
-
10:41
And it says no tests are passing.
-
10:44
Because we have no tests.
-
10:46
Once again, this course is about npm and not testing.
-
10:50
However, if you're interested you can check out the mocha website.
-
10:54
Let's delete the node modules folder again, And
-
11:01
play out the two scenarios of installing your dev environment or your production.
-
11:06
Let's try npm.install and see what it does by default.
-
11:32
Notice how mocha is in the node_modules folder.
-
11:37
That's because by default npm believes that an environment is a development
-
11:42
environment and not a production one which is sensible when you think about it.
-
11:46
There's probably more developers working on a project in development
-
11:51
than there are projects running in production in the wild.
-
11:55
Let's delete the node_modules folder again.
-
11:58
Now, how do we set the environment?
-
12:00
With command line applications you can include
-
12:03
environment variables before the command you're executing.
-
12:07
We can set NODE_ENV,
-
12:13
Which means the node environment, to =production, followed by npm install.
-
12:27
And this will only install the dependencies it needs to run,
-
12:31
not the dev dependencies.
-
12:39
Notice now mocha isn't installed, just bcrypt and colors.
-
12:45
There are more ways to set environment variables, but
-
12:48
this is one of the easiest to do to test it on your local machine.
-
12:54
In a company, there are system administrators who
-
12:56
deal with this end of things when deploying applications.
-
13:00
As a developer, it's just important to know that there are these variables set on
-
13:05
a machine which can influence how an application is run.
-
13:09
In this case, npm will install the required dependencies and
-
13:12
not dev dependencies.
-
13:15
There you have it.
-
13:15
You've installed packages both globally, for modules that you want to use anywhere
-
13:20
on your system, and locally, for modules that you want to use in a single project.
-
13:25
You've also used a package.json file to manage dependencies for
-
13:29
your application to run and to help in development.
-
13:32
Well done.
You need to sign up for Treehouse in order to download course files.
Sign up