<?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: sifatul</title>
    <description>The latest articles on DEV Community by sifatul (@sifatul).</description>
    <link>https://dev.to/sifatul</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%2F544171%2F5e88c771-6a13-4e95-add7-0e9aac5e1f1f.jpeg</url>
      <title>DEV Community: sifatul</title>
      <link>https://dev.to/sifatul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sifatul"/>
    <language>en</language>
    <item>
      <title>NestJs Course - part 2: Providers and Services</title>
      <dc:creator>sifatul</dc:creator>
      <pubDate>Sun, 20 Nov 2022 11:16:55 +0000</pubDate>
      <link>https://dev.to/sifatul/nestjs-course-part-2-providers-and-modules-4pio</link>
      <guid>https://dev.to/sifatul/nestjs-course-part-2-providers-and-modules-4pio</guid>
      <description>&lt;p&gt;Learn NestJs by creating a user management system, Part -2.&lt;br&gt;
While you follow along with us it's highly recommended to go through the official documentation.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/sifatul/nestjs-course-user-management-part-1-12jm"&gt;Part 1&lt;/a&gt; you learned to work with the Controller and Routing.&lt;/p&gt;

&lt;p&gt;The goal of part 2 is to learn to work with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;interface&lt;/li&gt;
&lt;li&gt;Providers (Service)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Providers are plain JavaScript classes that are declared as providers in a module – services, repositories, factories, helpers, and so on. &lt;/p&gt;

&lt;p&gt;Let's start with where we left off. Type check could be managed with an &lt;code&gt;interface&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You may use &lt;a href="https://docs.nestjs.com/cli/usages"&gt;CLI to generate an interface&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest generate interface users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b8gv3FT---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09y8rx0e4p1hhrw4bem3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b8gv3FT---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09y8rx0e4p1hhrw4bem3.png" alt="user interface" width="880" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And update the response type for each route in the controller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Delete, Get, Param, Patch, Post, Put } from '@nestjs/common';
import { Users } from './users.interface';

let sampleUsers = [{
  id: 1,
  name: 'user1',
  email: 'user1@email.com'
}]
@Controller('users')
export class UsersController {
  @Get()
  findAll() : Users []{
    return sampleUsers
  }
  @Get(':id')
  findOne(@Param() params) : Users{
    const id = params.id
    return sampleUsers.find(user =&amp;gt; user.id === id)
  }
  @Post()
  createOne() : Users []{
    const paramsBody = {
      id: 2,
      name: 'user2',
      email: 'user2@email.com'

    }
    sampleUsers.push(paramsBody)
    return sampleUsers
  }
  @Put(':id')
  updateOne(@Param() params) : Users {
    const id = params.id
    const paramsBody = {
      id: id,
      name: 'updatedUser',
      email: 'email-updated@email.com'
    }
    const indexOfUser = sampleUsers.findIndex(item =&amp;gt; item.id === id)
    sampleUsers[indexOfUser] = paramsBody
    return sampleUsers[indexOfUser]
  }
  @Patch(':id')
  updateOnePartly(@Param() params) : Users {
    const id = params.id
    const paramsBody = {
      email: 'email-update-only@email.com'
    }
    const indexOfUser = sampleUsers.findIndex(item =&amp;gt; item.id === id)
    sampleUsers[indexOfUser].email = paramsBody.email
    return sampleUsers[indexOfUser]
  }
  @Delete(':id')
  deleteOne(@Param() params) : Users []{
    const id = params.id
    sampleUsers = sampleUsers.filter(user =&amp;gt; user.id === id)
    return sampleUsers
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Services are a good example of a Provider. Services are responsible for data storage and retrieval and are designed to be used by the Controllers. Instead of data storing or retrieving in the controller (like you did in part 1), data management will be done inside the service and the controller will use the service at its need.&lt;br&gt;
Let's transfer codes from the controller and create a service &lt;code&gt;src/users/users.service.ts&lt;/code&gt; and use the interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nest generate service users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of using the CLI if you manually create the service then do remember to import the service inside &lt;code&gt;app.module.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersController } from './users/users.controller';
import { UsersService } from './users/users.service';
@Module({
  imports: [],
  controllers: [AppController, UsersController],
  providers: [AppService, UsersService],
})
export class AppModule {}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the service would look like the following: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CqPqzoHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6a6ie0zacjlf22bstdoc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CqPqzoHB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6a6ie0zacjlf22bstdoc.png" alt="user service" width="880" height="601"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order for the services to be digested by another class (controller) &lt;code&gt;@Injectable()&lt;/code&gt; decorator needs to be declared and the controller must inject the service in its constructor to use the service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor(private usersServices: UsersService) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you create the service with CLI you may notice that a &lt;code&gt;.spec.ts&lt;/code&gt; is generated at &lt;code&gt;src/users/users.service.spec.ts&lt;/code&gt;. &lt;br&gt;
In next lesson we will see the usage of this file.&lt;/p&gt;




&lt;p&gt;Part 2.&lt;br&gt;
Source code: &lt;a href="https://github.com/sifatul/user-management-with-nestjs/tree/part-2"&gt;https://github.com/sifatul/user-management-with-nestjs/tree/part-2&lt;/a&gt;&lt;br&gt;
Topic: Provider (Service)&lt;/p&gt;

&lt;p&gt;Part 1.&lt;br&gt;
Source code: &lt;a href="https://github.com/sifatul/user-management-with-nestjs/tree/part-1"&gt;https://github.com/sifatul/user-management-with-nestjs/tree/part-1&lt;/a&gt;&lt;br&gt;
Topic: Routing (Controller)&lt;/p&gt;




&lt;p&gt;I will be posting bi-weekly for javascript developers. Feel free to request topics that you would like to learn or recommend in the comment section.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>nestjs</category>
      <category>node</category>
      <category>api</category>
    </item>
    <item>
      <title>NestJs Course - part 1: Controller and Routing</title>
      <dc:creator>sifatul</dc:creator>
      <pubDate>Mon, 14 Nov 2022 13:09:15 +0000</pubDate>
      <link>https://dev.to/sifatul/nestjs-course-user-management-part-1-12jm</link>
      <guid>https://dev.to/sifatul/nestjs-course-user-management-part-1-12jm</guid>
      <description>&lt;h2&gt;
  
  
  Learn NestJs by creating a user management system, Part -1
&lt;/h2&gt;

&lt;p&gt;While you follow along with us it's highly recommended to go through the &lt;a href="https://docs.nestjs.com/"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The goal of this part: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create NestJs project&lt;/li&gt;
&lt;li&gt;Create routes or controllers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step1: Let's start by creating a project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i -g @nestjs/cli
$ nest new user-management-with-nestjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can run go inside the project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd user-management-with-nestjs 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and run the project&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4D60WFpO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0birp75kywm2ah63b8fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4D60WFpO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0birp75kywm2ah63b8fc.png" alt="localhost:300 preview" width="880" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may configure the project to listen to any other port by updating in main.ts file.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;await app.listen(&amp;lt;another-port&amp;gt;);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;




&lt;p&gt;Step 2: Now you need a route to get a list of users and to create a route you need a controller. &lt;br&gt;
You may use &lt;a href="https://docs.nestjs.com/cli/usages"&gt;CLI to generate a controller&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; nest generate controller users
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or &lt;br&gt;
create a &lt;code&gt;users/users.controller.ts&lt;/code&gt; file manually but remember need to register the controller in &lt;code&gt;app.module.ts&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UsersController } from './users/users.controller';

@Module({
  imports: [],
  controllers: [AppController, UsersController],
  providers: [AppService],
})
export class AppModule {}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Presently controller file is empty and you can add decorators.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@Controller()&lt;/code&gt; decorator to easily group a set of related routes&lt;/li&gt;
&lt;li&gt; Standard HTTP methods: &lt;code&gt;@Get()&lt;/code&gt;, &lt;code&gt;@Post()&lt;/code&gt;, &lt;code&gt;@Put()&lt;/code&gt;,  &lt;code&gt;@Patch()&lt;/code&gt;, &lt;code&gt;@Delete()&lt;/code&gt;, &lt;code&gt;@Options()&lt;/code&gt;, and &lt;code&gt;@Head()&lt;/code&gt;. In addition, &lt;code&gt;@All()&lt;/code&gt; defines an endpoint that handles all of them.&lt;/li&gt;
&lt;li&gt;Route parameters declared in this way can be accessed using the &lt;a class="mentioned-user" href="https://dev.to/param"&gt;@param&lt;/a&gt;() decorator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can try to create a rest API &lt;a href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete"&gt;CRUD&lt;/a&gt; to create, read, update, and delete a user.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;@Get /users/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@Get /users/:id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@Post /users/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@Put /users/:id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@Delete /users/:id&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As you don't have a database connection yet you can begin with an array to hold the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sampleUsers = [{
  id:1,
  name:'user1',
  email:'user1@email.com'
}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;simple implementation of CRUD can be as follows in &lt;code&gt;users.controller.ts&lt;/code&gt; and a few more additional http methods are shown.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller, Delete, Get, Param, Patch, Post, Put } from '@nestjs/common';

let sampleUsers = [{
  id: 1,
  name: 'user1',
  email: 'user1@email.com'
}]
@Controller('users')
export class UsersController {
  @Get()
  findAll() {
    return sampleUsers
  }
  @Get(':id')
  findOne(@Param() params) {
    const id = params.id
    return sampleUsers.find(user =&amp;gt; user.id === id)
  }
  @Post()
  createOne() {
    const paramsBody = {
      id: 2,
      name: 'user2',
      email: 'user2@email.com'

    }
    sampleUsers.push(paramsBody)
    return sampleUsers
  }
  @Put(':id')
  updateOne(@Param() params) {
    const id = params.id
    const paramsBody = {
      id: id,
      name: 'updatedUser',
      email: 'email-updated@email.com'
    }
    const indexOfUser = sampleUsers.findIndex(item =&amp;gt; item.id === id)
    sampleUsers[indexOfUser] = paramsBody
    return sampleUsers[indexOfUser]
  }
  @Patch(':id')
  updateOnePartly(@Param() params) {
    const id = params.id
    const paramsBody = {
      email: 'email-update-only@email.com'
    }
    const indexOfUser = sampleUsers.findIndex(item =&amp;gt; item.id === id)
    sampleUsers[indexOfUser].email = paramsBody.email
    return sampleUsers[indexOfUser]
  }
  @Delete(':id')
  deleteOne(@Param() params) {
    const id = params.id
    sampleUsers = sampleUsers.filter(user =&amp;gt; user.id === id)
    return sampleUsers
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get the source code in &lt;a href="https://github.com/sifatul/user-management-with-nestjs/tree/part-1"&gt;Github&lt;/a&gt;. Like the post for part-2. &lt;/p&gt;

&lt;p&gt;I will be posting bi-weekly for javascript developers. Feel free to request topics that you would like to learn or recommend in the comment section.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>typescript</category>
    </item>
    <item>
      <title>10 string methods you must know as a js beginner</title>
      <dc:creator>sifatul</dc:creator>
      <pubDate>Fri, 11 Nov 2022 15:35:50 +0000</pubDate>
      <link>https://dev.to/sifatul/10-must-know-string-manipulation-for-beginners-a87</link>
      <guid>https://dev.to/sifatul/10-must-know-string-manipulation-for-beginners-a87</guid>
      <description>&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Naming_convention_(programming)#Multiple-word_identifiers"&gt;Naming conventions&lt;/a&gt; are most commonly used. Good to remember these terms.&lt;/p&gt;

&lt;p&gt;1.UPPERCASE&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world".toUpperCase()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.lowercase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world".toLowerCase()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.camelCase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world"
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) =&amp;gt; {
 if(index === 0) return word.toLowerCase()
 return word.toUpperCase();
})
.replace(/\s+/g, "");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;4.PascalCase&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello world"
 .replace(/(?:^\w|[A-Z]|\b\w)/g, (word) =&amp;gt; {
  return word.toUpperCase();
 })
.replace(/\s+/g, "");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Few more that are commonly used with forms.&lt;/p&gt;

&lt;p&gt;5.is a valid url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/^(ftp|http|https):\/\/[^ "]+$/.test("https://leetcode.com/");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;6.get list of query strings from url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const url = "https://www.google.com/doodles/maurice-sendaks-85th-birthday?hl=en-GB"
const params = (url.split('?')?.[1]?.split('&amp;amp;') || [])
 .reduce((prev, current) =&amp;gt; {
  const keyVal = current.split('=');
  const key = keyVal[0];
  const val = keyVal[1];
  prev[key] = val;
  return prev;
 }, {});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;7.remove special string from a text&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"hello@world".replace(/[^\w\s]/gi, '')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;8.get ascii value of a character&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'a'.charCodeAt(0)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;9.convert json to string&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const obj = {
 key:'value'
}
JSON.stringify(obj)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;10.convert object string to json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;JSON.parse('{"key":"value"}')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can get all these and many more commonly used functions in a single &lt;a href="https://www.npmjs.com/package/js-string-helper"&gt;npm package&lt;/a&gt; but as a beginner it's good to know how these are done.&lt;/p&gt;

&lt;p&gt;I will be posting bi-weekly for javascript developers. Feel free to request for topics that you would like to learn or recommend in the comment section.&lt;/p&gt;

</description>
      <category>string</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Simple guide to create package in Github registry</title>
      <dc:creator>sifatul</dc:creator>
      <pubDate>Wed, 10 Aug 2022 01:38:00 +0000</pubDate>
      <link>https://dev.to/sifatul/create-package-in-github-registry-51p9</link>
      <guid>https://dev.to/sifatul/create-package-in-github-registry-51p9</guid>
      <description>&lt;p&gt;Creating a package in the GitHub registry is very simple.&lt;/p&gt;

&lt;p&gt;Firstly &lt;code&gt;package.json&lt;/code&gt; needs to be updated; so that name, repository and publishConfig does not mismatch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "name": "@&amp;lt;owner&amp;gt;/&amp;lt;repo-name&amp;gt;",
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/@&amp;lt;owner&amp;gt;"
  },
  "repository":"https://github.com/&amp;lt;owner&amp;gt;/&amp;lt;repo-name&amp;gt;.git",

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Secondly, &lt;a href="https://docs.github.com/en/enterprise-server@3.4/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token"&gt;create a &lt;code&gt;GITHUB_TOKEN&lt;/code&gt; &lt;/a&gt;which would be used in verification.&lt;/p&gt;

&lt;p&gt;The package can be published by: &lt;br&gt;
a) GitHub action or &lt;br&gt;
b) manually in terminal&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a) To publish with GitHub action&lt;/strong&gt;&lt;br&gt;
add a workflow in &lt;code&gt;.github/workflows/&amp;lt;workflow-name&amp;gt;.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Node.js Package

on:
  push:
    branches: ["&amp;lt;branch-name&amp;gt;"]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 12
      - run: npm ci
      - run: npm test

  publish-gpr:
    needs: build
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 12
          registry-url: https://npm.pkg.github.com/
      - run: npm ci
      - run: npm publish
        env:
          NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;b) publish package manually in terminal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Firstly, create a &lt;code&gt;.npmrc&lt;/code&gt; file at the root of the directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@&amp;lt;owner&amp;gt;:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=&amp;lt;GITHUB_TOKEN&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Secondly, login to npm &lt;br&gt;
&lt;code&gt;npm login --scope=@&amp;lt;owner&amp;gt; --registry=https://npm.pkg.github.com&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Finally, publish the package.&lt;br&gt;
&lt;code&gt;npm publish&lt;/code&gt;&lt;br&gt;
&lt;a href="https://github.com/Sifatul/simple-github-package/tree/publish-manually"&gt;sample project in GitHub&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use a private package in another repository
&lt;/h2&gt;

&lt;p&gt;create a &lt;code&gt;.npmrc&lt;/code&gt; file at the root of the directory with github token having access to the private package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@&amp;lt;owner&amp;gt;:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=&amp;lt;GITHUB_TOKEN&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;npm install&lt;/code&gt; will install the package normally&lt;/p&gt;

&lt;p&gt;PS: for public package in github repository no additional task is required&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Useful links:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/packages/quickstart"&gt;quickstart&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry"&gt;working-with-the-npm-registry&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Or &lt;br&gt;
if you want to &lt;a href="https://dev.to/sifatul/simple-guide-to-create-react-cra-component-test-locally-and-publish-to-npm-119c"&gt;publish package in npm registry&lt;/a&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>npm</category>
      <category>beginners</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Simple guide to create React CRA component, test locally and publish to npm</title>
      <dc:creator>sifatul</dc:creator>
      <pubDate>Sun, 20 Dec 2020 20:47:00 +0000</pubDate>
      <link>https://dev.to/sifatul/simple-guide-to-create-react-cra-component-test-locally-and-publish-to-npm-119c</link>
      <guid>https://dev.to/sifatul/simple-guide-to-create-react-cra-component-test-locally-and-publish-to-npm-119c</guid>
      <description>&lt;p&gt;In this post we will come to understand how to make a simple REACT SPA(single page application) and publish in NPM.&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;
&lt;h4&gt;
  
  
  Step 1: Create a new React app with CRA
&lt;/h4&gt;



&lt;p&gt;&lt;code&gt;npx create-react-app app-name &lt;br&gt;
cd my-app&lt;br&gt;
npm start&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Create component
&lt;/h4&gt;

&lt;p&gt;Lets create a button component as ButtonComponent.js inside src/components/ folder.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--68gUJ99_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mdb3wh8jjqnq9kafj968.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--68gUJ99_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mdb3wh8jjqnq9kafj968.png" alt="src/components/ButtonComponent.js" width="492" height="171"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 3: Create babel.config.js
&lt;/h4&gt;

&lt;p&gt;Create babel.config.js in the root.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
   presets:[
        "@babel/preset-env",
        "@babel/preset-react"
   ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 4: Update package.json
&lt;/h4&gt;

&lt;p&gt;Now, to publish the button component add the following line to scripts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"publish:npm": "rm -rf dist &amp;amp;&amp;amp; mkdir dist &amp;amp;&amp;amp;  babel                      src/components -d dist --copy-files"
This script recreates builds React application, recreates the /dist folder , and stores files in /dist directory. Lastly change private from true to false and main to dist/ButtonComponent.js.

"private": false,
"main": "dist/ButtonComponent.js",
"scripts": {
            "start": "react-scripts start",
            "build": "react-scripts build",
            "test": "react-scripts test",
            "eject": "react-scripts eject",
            "publish:npm": "rm -rf dist &amp;amp;&amp;amp; mkdir dist &amp;amp;&amp;amp;  babel src/components -d dist --copy-files",
            "publish:window": "cross-env NODE_ENV=production &amp;amp;&amp;amp; npx rimraf dist &amp;amp;&amp;amp; mkdir dist &amp;amp;&amp;amp;  babel src/components -d dist --copy-files"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample of package.json and file directories are as follows :&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--H27EbQz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tgwwa14mlo18txjo4ex7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--H27EbQz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tgwwa14mlo18txjo4ex7.png" alt="Alt Text" width="700" height="504"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Build, Test locally, and Publish
&lt;/h2&gt;

&lt;p&gt;login to npm &lt;code&gt;npm login&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 6: Build the project
&lt;/h4&gt;

&lt;p&gt;Transpile code from Es6 to Es5.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run publish:npm&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;For windows&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm run publish:window&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 7: Test locally
&lt;/h4&gt;

&lt;p&gt;Run npm link inside the project to create a global module of ButtonComponent.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm link&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now, to use the module run npm link  inside other project from which to test.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm link app-name&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 8: Publish
&lt;/h4&gt;



&lt;p&gt;&lt;code&gt;npm publish&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Congratulations you just created your first npm package!!!
&lt;/h1&gt;

&lt;p&gt;Another simple way could be to &lt;a href="https://dev.to/sifatul/create-package-in-github-registry-51p9"&gt;publish your package in github registry&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>react</category>
      <category>npm</category>
      <category>opensource</category>
      <category>firstpost</category>
    </item>
  </channel>
</rss>
