DEV Community

Cover image for Using npm packages in AdonisJS
Chimezie Enyinnaya
Chimezie Enyinnaya

Posted on • Originally published at mezielabs.com

Using npm packages in AdonisJS

One of the most asked questions in the AdonisJS community has to be about how to use X package in AdonisJS. Those asking this question tend to forget that AdonisJS is a Node.js framework and as a result, we can use an npm package the same way we'd use it in any Node.js project.

In this tutorial, I'll show two ways of using an npm package in AdonisJS. I'll use the Paddle Node.js SDK to demonstrate this but the same principles apply to other npm packages.

Using the package directly

The simplest way to use an npm package in AdonisJS is to install the package and use it directly wherever we want to use it in our codebase.

We need to first install the package:

npm install @paddle/paddle-node-sdk
Enter fullscreen mode Exit fullscreen mode

Let's say we are working with the concept of customer and we have a CustomersController. First, we import the package and then use it in the controller:

// app/controllers/customers_controller.ts

import { Paddle } from '@paddle/paddle-node-sdk'
import config from '@adonisjs/core/services/config'

export default class CustomersController {
  async handle({ auth }: HttpContext) {
    const paddle = new Paddle(config.get('services.paddle.apiKey'))

    await paddle.customers.get(auth.user!.paddleId)
  }
}
Enter fullscreen mode Exit fullscreen mode

Using a service class

The problem with the above approach is that we end up littering our codebase with the same package import (and initialisation) over and over. For some packages that could be okay. For example, using a package like lodash. But with the package and concept we are working with, that might not be the case.

So to take the above implementation one step further, we could create a PaddleService. We could create the service manually but there's a command for that:

node ace make:service paddle
Enter fullscreen mode Exit fullscreen mode

This will create an empty paddle_service.ts file inside app/services. Add the following code inside the file:

// app/services/paddle_service.ts

import config from '@adonisjs/core/services/config'
import { Customer, Paddle } from '@paddle/paddle-node-sdk'
import app from '@adonisjs/core/services/app'

export default class PaddleService {
  private paddle: Paddle

  constructor() {
    this.paddle = new Paddle(config.get('services.paddle.apiKey'))
 }

  async getCustomer(customerId: string): Promise<Customer> {
    return this.paddle.customers.get(customerId)
 }
}
Enter fullscreen mode Exit fullscreen mode

Like before, we import the package and initiate it. For the usage, we create a method that we can use in controllers or other places. Now, we can keep all Paddle-specified implementations contained in one place.

We can make use of the service class in two ways. First, we import and initialise the class manually:

// app/controllers/customers_controller.ts

import { inject } from '@adonisjs/core'
import PaddleService from '#services/paddle_service'

export default class CustomersController {
  async handle({ auth }: HttpContext) {
    const paddle = new PaddleService()

    await paddle.customers.get(auth.user!.paddleId)
 }
}
Enter fullscreen mode Exit fullscreen mode

The other way is to leverage AdonisJS Dependency Injection (DI) to initialise the class with the necessary dependencies. We can do that using the @inject decorator:

// app/controllers/customers_controller.ts

import { inject } from '@adonisjs/core'
import PaddleService from '#services/paddle_service'

export default class CustomersController {
  @inject()
  async handle({ auth }: HttpContext, paddle: PaddleService) {
    await paddle.customers.get(auth.user!.paddleId)
  }
}
Enter fullscreen mode Exit fullscreen mode

Here, we are using method injection but we could also use constructor injection.

Conclusion

So that's how to use npm packages in AdonisJS. Remember, AdonisJS is just a Node.js framework.

Top comments (0)