|
| 1 | +--- |
| 2 | +title: "Publishing to the npm Regristry" |
| 3 | +date: 2018-06-13T09:27:07-04:00 |
| 4 | +draft: true |
| 5 | +--- |
| 6 | + |
| 7 | +**_Note: I am going to assume that if you are reading this post you know what Node and npm are. As well as, you have both installed and have used them to some extent that I will not be covering those topics._** |
| 8 | + |
| 9 | +In my [last post][mcm1], I spoke about how I was going to look into creating the components of the smart mirror into Node packages, or modules. Last night, I decided to sit down and actually look into what it takes to do as such - and was awestruck. _It couldn't be this easy, could it?_ As a [new publisher of two npm modules][mcm2], I can tell you, yes, yes it is that easy. |
| 10 | + |
| 11 | +In this post, I plan to provide you some insight in what I learned while publishing to the [npm Regristry][npm1]. |
| 12 | + |
| 13 | +## The difference between a package and a module |
| 14 | + |
| 15 | +Node.js and npm have very specific definitions of packages and modules, which are easy to mix up. The [docs on npm][npm2] do a great job explaining why certain default files are named the way they are. |
| 16 | + |
| 17 | +### The highlight reel |
| 18 | + |
| 19 | +A package is a file or directory that is described by a `package.json` file. Whereas, a module is any file or directory that can be loaded by Node.js' `require()`. |
| 20 | + |
| 21 | +## Ok, so let's get to publishing! |
| 22 | + |
| 23 | +Before you can publish to the regristry, you need make sure you have an account. You can do this one of two ways: via the browser or in the command line. For this example, let's try to keep in in the command line. |
| 24 | + |
| 25 | +`npm adduser` |
| 26 | + |
| 27 | +It will ask you for your username, password and email address. Ta da! You now have an account on the npm regristry. You will receive an email to verify and you can do that and fill out more details to your profile at your leisure. |
| 28 | + |
| 29 | +Next you are going to create a package.json file. Note, that if you are trying to prep an existing project to publish and you already have a package.json, that's ok. The next command appends whatever information you are missing to the existing file. |
| 30 | + |
| 31 | +`npm init` or `npm init --yes` |
| 32 | + |
| 33 | +The minimal requirements that you have in your package.json to publish are the following: |
| 34 | + |
| 35 | + { |
| 36 | + "name": "my-awesome-package", |
| 37 | + "version": "1.0.0" |
| 38 | + } |
| 39 | + |
| 40 | +Now in my case, I was taking Vue components and turning them into Node modules, so I also had to change `"main": "index.js"` to something link `"main": "index.vue"`. |
| 41 | + |
| 42 | +You are now ready to publish. |
| 43 | + |
| 44 | +`npm publish --access public` |
| 45 | + |
| 46 | +That's it! You have now published your first Node package/module to the npm regristry. |
| 47 | + |
| 48 | + [mcm1]: https://blog.morsecodemedia.com/architectural-short-sightedness/ "Architectural Short-sightedness on the MorseCodeMedia blog." |
| 49 | + [mcm2]: https://www.npmjs.com/~brandonmorse "Brandon Morse on the NPM Regristry" |
| 50 | + [npm1]: https://www.npmjs.com/ "The npm regristry" |
| 51 | + [npm2]: https://docs.npmjs.com/getting-started/packages "Packages vs Modules" |
| 52 | + |
| 53 | + |
0 commit comments