Check out a free preview of the full Creating an Open Source JavaScript Library on Github course

The "NPM Configuration and NPM Init" Lesson is part of the full, Creating an Open Source JavaScript Library on Github course featured in this preview video. Here's what you'd learn in this lesson:

The “npm init” command will initialize a project and create the package.json file. There are a few questions asked by NPM each time the “init” command is run so Kent demonstrates how to update the .npmrc file with pre-populated default values. He then completes the initialization of the star-wars-names project. The code for this example can be found on the FEM/03.0-add-data branch.

Preview
Close

Transcript from the "NPM Configuration and NPM Init" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Kent C. Dodds: NPM Init so now we're finally gonna have a package J son. So what we just did is pretty much applicable to everything, every open source library regardless of the platform you're delivering it to. Package.kson very specific to NPM you're not gonna be publishing to other registries most of the time with a package.json.

[00:00:24]
So, there are a couple of things with actually, let's go to npmjs.org. So, there are a couple of things about. Weird. We need docs. Npm Npmjs docs. There we go, docs.npmjs.com. Actually just let me quickly explain why we're about to do what we're gonna do, so normally to start a package json.

[00:00:58]
You can create your own package json yourself, but npm comes prebundled with a really cool command called npm init. And it's gonna ask you a whole bunch of questions. It will probably, your first question will probably be something different. It might ask you for your name or one of the questions and I don't like, this is my machine I don't like having to answer the same questions all the time so I'm gonna show you how you can get around that.

[00:01:26]
With more than you'll probably want to know about the npm configuration. So we have a couple of properties. The npm configuration consists of an npm rc file in your home directory. So I'm not gonna actually, I was gonna show you mine, but it has my npm token in it which means you can publish all my libraries for me, so I'm not gonna do that, but I guess I could npm and logout.

[00:01:54]
There we go. Now, I can cat .npmrc. Oops, cat, in my home directory, .npmrc, and that gives you everything that my npmrc looks like. So init author name corresponds to this init author name here, and so on and so forth. We have email, URL, license. I only have to answer these questions once, I do not have to answer them over and over again.

[00:02:24]
The way that you set up this, and save exact, that means when I run npminstall--save, it's going to save the exact version of 4.5.3 rather than the carrot that probably mostly seen in your package.jsons. So there's kind of like a some people like that, some people don't. I like that, so you don't have to do that though.

[00:02:54]
So yeah, the command we're gonna run is,
>> Kent C. Dodds: Yeah, where is the document? Here we go. So there's a fly right here. So yeah, it's npm config set, and then whatever it is that you want to set. So it's npm config set init-author-name, and then put your name in here.

[00:03:19]
So this is one thing that's not committed to our repository. This is gonna be for you, so whatever you wanna put in here. So for me that would be kencdodds. And then if I cat the npmrc, yeah, you're gonna wanna put that in quotes.
>> Kent C. Dodds: Yeah, and then if you look at your npmrc, you should have an init-author-name.

[00:03:43]
So I'm gonna stop right here and let you do the same thing for email, url and license if you want to. You don't have to do that like maybe you don't like the MIT license or something but yeah go ahead and go through that real quick. Feel free to ask questions.

[00:04:05]
Ivan is wondering why we have init-author-email and we also have email, so I'm for sure that init-author-email is what's used when npm init is run. And so when you initialize, we'll see what the package.json looks like in a second. But one of the properties of the package.json is the author.

[00:04:32]
And so in it, author email is used in that package.json. The email is, I'm pretty sure it's used in association with your login. But I'm not 100% positive what it's for or what it uses your email for with your log in. Maybe it's somehow associated with your user name and token, like they use the three of them or something to authenticate you or something, I think.

[00:04:59]
I'm not 100% positive on that.
>> Kent C. Dodds: Everybody have their npmrc set up? Anybody have that done?
>> Kent C. Dodds: Yeah, all right. [LAUGH]
>> Kent C. Dodds: Okay, cool. So with that you should be able to run npm init and it will start asking you questions about your package. So it will ask for the name of your package.

[00:05:31]
You're going to fork this later and change the name later so until we get to that point go ahead and follow along. So we'll default with the directory name, Star Wars names. The version will default to 1.0. We're actually gonna change that version before we publish, then you can enter in any description you want for all your random Star Wars character needs.

[00:05:58]
Entry point is what will be required when somebody goes to require your module. Let's say we had this published on npm, somebody installs it then they went require, starwars-names. The entry point property in the package.json is how Node resolves that require statement. So you set this to whatever you want your main method to be basically.

[00:06:31]
For us that's gonna be src/index.js. And for the test command, that's pretty cool. So I've never actually had all my Node modules installed before I ran npm init, and it looks like it noticed that we have Mocha installed. So it's defaulting to Mocha. I don't know if it's doing that for you, but that's cool.

[00:06:55]
Yeah, you can do Mocha. We're going to actually, when we check out the next branch it's going to blow it away anyway. We'll add that later. The git repository should default to the one, to your origin, and then keywords, you can put whatever you want, star wars. And license will default to MIT.

[00:07:12]
It'll ask you if this is okay, you can say yes and pop open your package.json. Sorry, there's a question.
>> Speaker 2: What was the save exact for?
>> Kent C. Dodds: Yeah, so save exact is, like I said earlier, it allows me to run npm install --save foo. And normally if you run this without anything set, then in your package json you'll get, well, once we have dependencies, you'll get Foo added to your dependencies, you know by running say Foo whatever package you're installing.

[00:07:52]
And it'll add a carrot and then the version number and this has to do with version ranges. I'm not going to get into that too much. But I actually kind of like locking down my version numbers. It makes upgrading dependencies a little less surprising for me. So that's all save exact does.

[00:08:14]
You can do that on the command like per npm install as well if you want to, but you don't have to do that. Any other questions? Joel's license defaulted to ISC. It's probably because your init license is not set properly, and that's fine if you wanna use ISC, but this is what it should look like if you want to initialize to something in particular is init-license=MIT

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now