DEV Community

Cover image for Publishing NestJS Packages with npm

Publishing NestJS Packages with npm

John Biundo on August 19, 2019

John is a member of the NestJS core team, primarily contributing to the documentation. This is the first in a multi-part series on building re-usa...
Collapse
 
koakh profile image
Mário Monteiro

Another great post @john
Thanks

Can I use this technique to create a a shared module package.
I have a Lerna monorepo project with two implementations one rest and one graphql, and I really want to create a common shared package, for example to Share the auth module in both rest and graphql packages. Can I extract the auth and users modules (same as nestjs docs) into a package(local ir registery), and reuse it in this two packages, and maybe other new projects?

Or is a better way to share common packages in nestjs to not Dry and duplicate same code?

Thanks

Collapse
 
johnbiundo profile image
John Biundo

Hi, I think you just posted this question on the discord channel? Here's what I wrote there:

I assume you want to keep the packages private, so I think you have a couple of options. One is to use a private npm registry -- either with a paid account at npmjs.com, or hosting one locally (I have experimented with Verdaccio, and it seems excellent, though I've only just played around with it). Another is to consider a monorepo. I candidly don't have any experience with a monorepo, but a lot of folks on here do actively use one, so I'm sure one or more will chime in if you want to go that route. Good luck!

Hopefully some of the other community members will chime in with thoughts.

Collapse
 
koakh profile image
Mário Monteiro

Thanks @john

I already use monorepo and Verdaccio.
But the question os not related with publish. I try to ask about extract auth and users modules to a new package to share in my 2 projects. Ex I want to create for ex a package like @acme/nestjs-auth and re,-use it, that why I ask it. Sorry if I cant explain better, you already note that I have some issues with English eheheh

Thanks

Thread Thread
 
johnbiundo profile image
John Biundo

No worries @Mário.

So I may not be understanding, but it seems like you're asking about re-using a package in another project. To me, that sounds exactly like what I was addressing. So, you would npm publish @acme/nestjs-auth (if you want it public, that's exactly the recipe outlined in this article; if you want it private, I addressed that above), then, in project A, you'd npm install @acme/nestjs-auth and inside your project a, import { blah } from @acme/nestjs-auth. You could do the same inside project b.

If you just mean sharing a module within a project, that is a separate topic, and is about the Nest module system.

I hope that clarifies somewhat. Ask further if I'm missing the point!

Thread Thread
 
koakh profile image
Mário Monteiro • Edited

Thanks John,
Yes Im asking about sharing a module within a project

@rubin sent me a GitHub link in discord that seems will Help me with issue. Thanks John for your paciente and kind answear :)

Looking forward for next great nestjs posts.

Thread Thread
 
samxdesc profile image
Samuel

Hi Mário, how are you doing?

I need to do exactly what you asked for, how did you dealed with the problem?

Thank you.

Thread Thread
 
koakh profile image
Mário Monteiro

Hello Samuel

I don't remember, I must check my project, but the of out friend @rubin put me in the right track.

When I get to home I Will check it....

O see that @rubin repo link os not on this thread, I paste it later

Thread Thread
 
solidarynetwork profile image
solidarynetwork

Hello Samuel

here are my notes
includes the link to repo that I follow to get it working from our friend @rubiin

I hope this helps :)

NOTES

Links

a example to create a nestjs package with dynamic modules :)

Tooling

CLI command to install npm peerDependencies. This can be useful when developing modules.

$ sudo npm install -g npm-install-peers
$ npm-install-peers

Publish

# build
$ npm run build
# publish
$ npm publish
# unPublish
# https://docs.npmjs.com/unpublishing-packages-from-the-registry  
# npm unpublish <package-name>@<version>
$ npm unpublish @koakh/nestjs-auth-quick-config@1.2.2

Us in NestJs from file system, ideal for developing

# create symbolic link
$ cd NodeNestJsHyperLedgerConvectorRestStarter/node_modules/@koakh
$ ln -s ../../../../../Node/@NestPackages/@koakh/nestjs-auth-quick-config/

After changes don't forget to npm run build

Use in NestJs lerna project from NPM registry

# install dependency
$ npx lerna add @koakh/nestjs-auth-quick-config --scope @convector-sample/server-graphql
$ npx lerna clean
$ npx lerna bootstrap

Install in App

app.module.ts

ex from graphql project

import { AuthQuickConfigModule } from '@koakh/nestjs-auth-quick-config';

@Module({
  imports: [
    RecipesModule,
    PersonModule,
    ParticipantModule,
    GraphQLModule.forRoot({
      installSubscriptionHandlers: true,
      autoSchemaFile: 'schema.gql',
    }),
    AuthQuickConfigModule.register({
      jwtSecret: 'secretKey',
      jwtExpiresIn: '1h',
      getByUsername: (username: string) => 'admin',
    }),
  ],
})
Collapse
 
unicop profile image
unicop

@john Biundo

  1. For who prefer to use yarn, it will help if you will add yarn add link:../ for creating the symlink for the auto refresh.
  2. run on the package tsc -w will make it more easy for development to do changes in the package and auto-refresh the server automatically.
Collapse
 
wnbsmart profile image
Maroje Macola

@unicop thanks for the tip when using yarn :)

In my case, to make it work, I had to add symlink to the package itself, not the root folder in which reside both nestjs app & package (i.e. I executed yarn add link:../nestjs-package-starter)

Collapse
 
banesca profile image
JORGE GUTIERREZ • Edited

Hello first great the article just a question after following the steps install @ nestjs / swagger, but it gives me an error when I build node_modules/@nestjs/common/cache/interceptors/cache.interceptor.d.ts:1:28 - error TS2307: Cannot find module 'rxjs'.

Collapse
 
banesca profile image
JORGE GUTIERREZ

ERROR

Collapse
 
martinslae profile image
Alex Martins

Hi guys,
First, congratulations on the excellent post.
What is the best way to create a nest model as an npm package that needs to receive some repository reference to save and get data from the database?

Have I to inject the Prisma reference? - In this case, I think I'll have migrations issues.
Or should I work just with DTO and save on the main App?
or there's a better way?

Thanks

Collapse
 
danicaliforrnia profile image
Daniel Stefanelli

Hi @johnbiundo ,

Great post.

I implemented something similar what you explain in this post but I notice that I can't navigate between classes imported from another package in my IDE. Maybe Is there something I'm missing?

Collapse
 
ruslangonzalez profile image
Ruslan Gonzalez

Amazing! Outstanding lecture!

Collapse
 
johnbiundo profile image
John Biundo

Thanks Ruslán González. Glad you found it useful!

Collapse
 
ruslangonzalez profile image
Ruslan Gonzalez

I am reading again this post because I love it... !!!

Collapse
 
goofiw profile image
Will Chantry

Great post! Looking forward to mixing this up with a lerna monorepo. I have a specific desire to make a mixed cli and app tool and I want to share my database module/service in a non nestjs and nest environment and this will help a lot getting started. (I want to make the scripts very light and only have them install required dependencies so I don't think the nestjs monorepo atrategy will work for me).

Collapse
 
carlillo profile image
Carlos Caballero

Hey!

Thanks for your post!

Collapse
 
zachma profile image
Zach

Thanks for sharing. Mind I ask if there is way to publish single library generated by nest generate library? or What's the best way to sharing modules between different nestjs project?

Collapse
 
iamnotstatic profile image
Abdulfatai Suleiman

Great article, @johnbiundo love the pun it's topnotch

Collapse
 
tata2501 profile image
tata2501 • Edited

How to export an api from your custom package in nestjs?

Collapse
 
danicaliforrnia profile image
Daniel Stefanelli

Hi @koakh,

I need to do the same thing you asked for. Did you accomplish that? Can you share a Github repo or somenthing I can follow?

Hope can you help me.

Regards.