<?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>DEV Community: nestedifsdotcom</title>
    <description>The latest articles on DEV Community by nestedifsdotcom (@nestedifsdotcom).</description>
    <link>https://dev.to/nestedifsdotcom</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%2F680610%2F6e473cec-3013-4705-8eda-12b2a11b9d04.png</url>
      <title>DEV Community: nestedifsdotcom</title>
      <link>https://dev.to/nestedifsdotcom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nestedifsdotcom"/>
    <language>en</language>
    <item>
      <title>A comprehensive guide to publishing and maintaining javascript libraries part 1</title>
      <dc:creator>nestedifsdotcom</dc:creator>
      <pubDate>Sat, 25 Jun 2022 19:52:59 +0000</pubDate>
      <link>https://dev.to/nestedifsdotcom/a-comprehensive-guide-to-publishing-and-maintaining-javascript-libraries-part-1-30jk</link>
      <guid>https://dev.to/nestedifsdotcom/a-comprehensive-guide-to-publishing-and-maintaining-javascript-libraries-part-1-30jk</guid>
      <description>&lt;p&gt;As developers, we always have our favorite utilities that we carry around from project to project, usually a &lt;code&gt;utils&lt;/code&gt; folder that contains snippets and scripts that we created or shamelessly stolen from StackOverflow.&lt;/p&gt;

&lt;p&gt;We decided with my friend Youcef to create an npm package that includes those utilities, and after &lt;del&gt;helping him&lt;/del&gt; watching him do that, I wanted to share and document the process in a series of articles.&lt;/p&gt;




&lt;h2&gt;
  
  
  Let's start with the definitions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  what's a package ?:
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;A package is a file or directory that is described by a package.json file. A package must contain a package.json file in order to be published to the npm registry &lt;br&gt;
-- &lt;cite&gt;source &lt;a href="https://docs.npmjs.com/about-packages-and-modules"&gt;npm.com&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What's npm?
&lt;/h3&gt;

&lt;p&gt;npm stands for Node Package Manager. It's a :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  repository of javascript packages that are primarily open-source and publicly available, but you can also create private ones&lt;/li&gt;
&lt;li&gt;  a command-line tool that manages project dependencies (installing packages, updating their version, or uninstalling them)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Before starting:
&lt;/h2&gt;

&lt;p&gt;Make sure you have Node and npm installed.&lt;/p&gt;

&lt;p&gt;Run the command &lt;code&gt;npm --version&lt;/code&gt; on the Terminal; if you don't have a version, then you should install &lt;a href="https://nodejs.org/en/"&gt;Node.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a package.json file
&lt;/h2&gt;

&lt;p&gt;Now that we know our vocabulary and have the necessary tools, let's create our first package it will be named &lt;code&gt;yanis-array-utils&lt;/code&gt;, and it's an array utility library.&lt;/p&gt;

&lt;p&gt;Create a folder and run &lt;code&gt;npm init&lt;/code&gt; on the console.&lt;br&gt;
Answer every question the command will ask you (give a different name to your package so you can publish it later as &lt;code&gt;yanis-array-utils&lt;/code&gt; will be taken).&lt;/p&gt;

&lt;p&gt;You will then have a file named &lt;code&gt;package.json&lt;/code&gt; containing :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"the-name-of-your-package"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"description"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"echo &lt;/span&gt;&lt;span class="se"&gt;\\&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="err"&gt;Error:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;no&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;test&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;specified\\&lt;/span&gt;&lt;span class="s2"&gt;" &amp;amp;&amp;amp; exit 1"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"ISC"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  writing the code:
&lt;/h2&gt;

&lt;p&gt;our first utility will be a function that returns the last element of an array, create a file named &lt;code&gt;index.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// last(["first","last"]) will return "last"&lt;/span&gt;
&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it you now have an npm package.&lt;/p&gt;

&lt;h3&gt;
  
  
  Publishing the package:
&lt;/h3&gt;

&lt;p&gt;Now that we have the code of the package let's share it with the world.&lt;/p&gt;

&lt;p&gt;Go to &lt;a href="http://npmjs.com/"&gt;npmjs.com/&lt;/a&gt; and sign up.&lt;/p&gt;

&lt;p&gt;In your command line run &lt;code&gt;npm login&lt;/code&gt; then &lt;code&gt;npm publish&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using the package
&lt;/h2&gt;

&lt;p&gt;you can create another project and install you package as a dependency&lt;/p&gt;

&lt;p&gt;with &lt;code&gt;npm install yanis-array-utils&lt;/code&gt; and use it as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yanis-array-utils&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;in the next article coming soon we will discuss tooling to support exporting to different modules format and then in the third part of this series we will discuss about CI/CD. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
