Code Monkey home page Code Monkey logo

nestjsplaywithdynamicmodulesandlocalpackage's Introduction

README

project as a problem with Error: Cannot find module 'reflect-metadata' when launch consumer app....

a simple and naife example to pass a UserService to a DynamicModule (NestJs Library)........won't work this is one of the tries without sucess until find how to do it the "right way"

in th end the only thing that matters in this poc is the userService: new UsersService() and try to figure out a way to inject the userService, seems more idiomatic than create a instance of it in.....

@Module({
  imports: [
    UsersModule,
    AuthModule.register({
      // naife way to pass userService
      userService: new UsersService(),
      // config
      config: {
        folder: '../consumerapp/config'
      }
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})

export class AppModule { }

Install dependecies

Error: Cannot find module 'reflect-metadata'
It's a peerDependency.
You need to install it alongside rxjs aswell.
npm install --save reflect-metadata rxjs

Build local unpublished package

$ cd nestjs-package-starter
$ npm i
$ npm run start:dev

Install Package in consumer app

to use local package nestjs-package-starter

$ cd consumerapp
$ npm i
# not need here only as a reference to how to import it into consumer app
$ npm i ../nestjs-package-starter

Start consumer app

$ cd nestjs-package-starter
$ npm run start:debug

Some Curls to test

$ curl 127.0.0.1:3000
{
  "message": "hello john"
}
$ curl 127.0.0.1:3000/config
{
  "HELLO_MESSAGE": "hello Hello there, world!"
}
# OK
$ curl -X POST 127.0.0.1:3000/auth/login \
  -H "Content-Type: application/json" \
  -d '{ "username": "john", "password": "changeme" }' \
  | jq
{
  "logged": true
}  
# KO
$ curl -X POST 127.0.0.1:3000/auth/login \
  -H "Content-Type: application/json" \
  -d '{ "username": "john", "password": "whatpass" }' \
  | jq
{
  "logged": false
}

nestjsplaywithdynamicmodulesandlocalpackage's People

Contributors

koakh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.