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.
- NPM Init. So now we're finally going to have a package.json. What we just did is pretty much applicable to everything, every open source library regardless of the platform you're delivering it to. Package.json very specific to NPM. You're not going to be publishing to other registries most of the time with the package.json. There are a couple of things with ... Actually let's go to npmjs.org, whoa ...
So there are a couple of things about ... Oh, we need docs npmjs docs there we go. docs.npmjs.com Actually let me just quickly explain why we're about to do what we're going to do. Normally to start a package.json ... You can create you're own package.json yourself but NPM comes pre-bundled with a really cool command called NPM Init.
It's going to ask you a whole bunch of questions. You're first question will probably be something different. It might ask you for your name. Or one of the questions. This is my machine. I don't like having to answer the same questions all the time so I'm going to show you how you can get around that with more than you probably want to know about the npm configuration. We have a couple properties ...
The npm configuration consists of an npmrc file in your home directory. I'm not going to actually ... I was going to show you mine but it has my npm token in it which means you could publish all my libraries for me so I'm not going to do that. But, I guess I could npm logout. There we go. Now I can cat npmrc. Oops, cat in my home directory npmrc and that gives you everything my npmrc looks like.
Init author name corresponds to this init author name here. And so on and so forth. We have email, url, license so I only have to answer these questions once. I don't have to answer them over and over again. The way that you set up this ... Oh and user name, email, so on so forth. Save exact ... That means when I run npm install, dash, dash, save it's going to save the exact version of like 4.5.3 rather than the carat that you probably mostly see in your package.jsons so there's kind of like a ...
Some people like that, some people don't. I like that. You don't have to do that though. The command we're going to run is ... Where is that document? Here we go. There's like a fly right here. 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.
This is one thing that's not committed to our repository. This is going to be for you so whatever you want to put in here. For me that'd be Kent C. Dodds. And then if I cat the npmrc. Oh yeah, you're going to want to put that in quotes. Then if you look at your npmrc you should have an init author name. I'm going to 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. Go ahead and go through that real quick. Feel free to ask questions. Ivan is wondering why we have init author email and we also have email. I'm pretty sure ... Well I'm for sure that init author email is what's used when npm init is run.
We'll see what the package.json looks like in a second but one of the properties of the package.json is the author and so init 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 login. 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'm not 100% positive on that. Everybody have their npmrc set up? Anybody have that done? Yeah, all right. Okay. Cool. With that you should be able to run npm init and it will start asking you questions about your package.
It will ask for the name of your package. You're going to fork this later and change the name later so until we get to that point just go ahead and follow along. We'll default to the directory name, Star Wars names. The version will default to 1.0 We're actually going to to change that version before we publish. Then you can enter any description you want for all your random Star Wars character needs.
Entry point is what will be required when somebody goes to require your module. Let's say we had this published on npm, somebody installed it then they went require Star Wars names. The entry point property in the package.json is how node resolves that required statement. You set this to whatever you want your main method to be basically.
For us that's going to be source/index.js For the test command ... Oh that's pretty cool. 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. 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.
Get 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 and it will ask you if this is okay. You can say yes and pop open your package.json Sorry, there's a question. - [Student] What was the save exact for? - Yeah, so save exact is, like I said earlier, it allows me to run npm install save like 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 by running say foo whatever package you're installing and it'll add a carat and then the version number. 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.
That's all save exact does. You can do that on the command line 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. That's fine if you want to use isc but this is what it should look like if you want it to initialize to something in particular is init-license=MIT.
Note: This course was created by Frontend Masters. It was originally released on 08/09/2016. We're pleased to host this training in our library.
- Creating an open-source library
- Linting and testing
- Code coverage
- Installing and configuring Babel
- Peer dependencies
- Forking and renaming
- Continuous integration and automating releases