<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Hays Stanford</title>
    <description>The latest articles on Forem by Hays Stanford (@hays).</description>
    <link>https://forem.com/hays</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F259878%2F018cb32d-724e-4b0c-a44f-f06ba0da76d6.jpg</url>
      <title>Forem: Hays Stanford</title>
      <link>https://forem.com/hays</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/hays"/>
    <language>en</language>
    <item>
      <title>Create &amp; Publish a Node.js NPM Package in 7 Minutes</title>
      <dc:creator>Hays Stanford</dc:creator>
      <pubDate>Sun, 26 Jan 2020 03:18:09 +0000</pubDate>
      <link>https://forem.com/hays/create-publish-a-node-js-npm-package-in-7-minutes-4mek</link>
      <guid>https://forem.com/hays/create-publish-a-node-js-npm-package-in-7-minutes-4mek</guid>
      <description>&lt;h1&gt;
  
  
  Why care about NPM?
&lt;/h1&gt;

&lt;p&gt;NPM, or Node Package Manager, is the default package manager utilized by the infamous Node.js runtime environment for JavaScript.&lt;/p&gt;

&lt;p&gt;Instead of downloading packages directly &amp;amp; placing them into your Node.js source directory while writing a bunch of quirky import statements, you can simply:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Run &lt;code&gt;npm install PACKAGE_NAME&lt;/code&gt; to add the package to the &lt;code&gt;/node_modules&lt;/code&gt; directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;import VAR_NAME from 'PACKAGE_NAME';&lt;/code&gt; to your source.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Utilize the package with the defined variable name, such as &lt;code&gt;example()&lt;/code&gt; or &lt;code&gt;example.includedFunction()&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The vast majority of JS developers utilize NPM, or its faster kin Yarn, to install their packages in minutes.&lt;/p&gt;

&lt;p&gt;If you’re looking to open-source a library for Node, you’ll want to ensure it is all published on NPM. Otherwise, it may fail to be seen at all.&lt;br&gt;
Publishing to NPM&lt;/p&gt;

&lt;h2&gt;
  
  
  Publishing an NPM project
&lt;/h2&gt;

&lt;p&gt;The neat thing about publishing a package to the NPM registry is that it’s about as simple as installing a package from the registry.&lt;/p&gt;

&lt;p&gt;Open your CLI, type a few commands, then BOOM… your code is live. For a more descriptive approach, see the steps below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to make sure the directory you're publishing is an NPM project. Do this by running &lt;code&gt;npm init&lt;/code&gt; then complete the automatic prompts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A9B-P3AA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xlcatgyr3bpw3w9t19o8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9B-P3AA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/xlcatgyr3bpw3w9t19o8.gif" alt="npm init"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensure that any code you want in the library can be utilized by properly exporting it in Node. For our example, we’ll be exporting &lt;code&gt;printMsg()&lt;/code&gt; from &lt;code&gt;index.js&lt;/code&gt; as a simple test command:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;exports.printMsg = function() {
 console.log('Here is our test message from our NPM package.')
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Open up the generated &lt;code&gt;package.json&lt;/code&gt; and make sure that the &lt;code&gt;"main"&lt;/code&gt; key is set to the entry file for your library. Here’s an example using &lt;code&gt;index.js&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "test-test-test-npm-package",
  "version": "1.0.0",
  "description": "A test npm package",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
  },
  "author": "Hays Stanford",
  "license": "ISC"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Once you have everything setup as stated above, it’s time to publish! All you have to do is run &lt;code&gt;npm publish&lt;/code&gt; from within the directory of your NPM project.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lSUgUUYn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/puh3sf40mz9f8jcq23hm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lSUgUUYn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/puh3sf40mz9f8jcq23hm.gif" alt="npm publish"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If there are no errors, then you’re done! Your NPM package is officially published to the public NPM registry. It’s that simple…&lt;/p&gt;

&lt;p&gt;If you want to see your NPM package in the public registry of NPM, you can do that by searching for it on &lt;a href="http://www.npmjs.com"&gt;www.npmjs.com&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rk-PWSup--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/4sixuaimj7fpa0ahkkx8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rk-PWSup--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/4sixuaimj7fpa0ahkkx8.png" alt="view of test package on npmjs.com"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The package created in this tutorial was named test-test-test-npm-package and you can view it by &lt;a href="https://www.npmjs.com/package/test-test-test-npm-package"&gt;clicking here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hope this helps you guys! Let me know if there’s anything else you’d like to see, definitely DM me on Twitter. Click below to follow my Twitter:&lt;/p&gt;



&lt;blockquote class="twitter-tweet"&gt;
&lt;br&gt;
&lt;p&gt;Building your own app and getting it running is the best way to learn to code. That's how I figured out how to build Ice Breakerr. Here's more info: &lt;a href="https://t.co/6VnKjK3nzR"&gt;&lt;/a&gt;&lt;a href="https://t.co/6VnKjK3nzR"&gt;&lt;/a&gt;&lt;a href="https://t.co/6VnKjK3nzR"&gt;https://t.co/6VnKjK3nzR&lt;/a&gt; &lt;a href="https://t.co/6H5umiEmaq"&gt;pic.twitter.com/6H5umiEmaq&lt;/a&gt;&lt;/p&gt;— Hays Stanford (@haysstanford) &lt;a href="https://twitter.com/haysstanford/status/1065759954945171456?ref_src=twsrc%5Etfw"&gt;November 23, 2018&lt;/a&gt;&lt;br&gt;
&lt;/blockquote&gt; 

</description>
      <category>npm</category>
      <category>node</category>
      <category>javascript</category>
      <category>react</category>
    </item>
  </channel>
</rss>
