Nuxt-Medusa Module: Integrate Medusa with your Nuxt.js application

jacobandrewsky

Jakub Andrzejewski

Posted on March 16, 2023

Nuxt-Medusa Module: Integrate Medusa with your Nuxt.js application

Great news for Nuxt fans. Jakub Andrzejewski recently created a new nuxt-medusa module, that simplifies the process of integrating Medusa’s commerce modules with a Nuxt application.

Access the module here, including a tutorial, documentation, video, and release notes. You can also try it directly in your browser using the module sandbox on Stackblitz.

W*hat is Medusa?Medusa provides modular and open commerce infrastructure built to make custom-development processes easier for developers. Medusa’s components and APIs are built in abstract layers, making it easy for developers to customize the core functionalities for different commerce cases.

Technical aspects

The Nuxt-Medusa module is built on top of Nuxt.js, a popular framework for building server-side rendered Vue.js applications. It is designed to work seamlessly with Medusa.

The module uses @medusa/medusa-js under the hood to query the Medusa API and retrieve data, which is then exposed to the Nuxt.js application.

Features

Setting up the Nuxt-Medusa module is a relatively straightforward process. The module is relatively new, but it includes short and concise documentation that provides step-by-step guidance on configuring and using its two available features.

At this moment, there aren’t more features available, but the module maintainer opened an issue on the GitHub repository, so other users can ask for new features.

Why use it

  • It simplifies the process of integrating the Medusa engine with a Nuxt.js application. This can save you significant time and effort, as they no longer need to manually configure and integrate the two frameworks.
  • It’s the first module in the Nuxt 3 version that specifically targets digital commerce functionality. This opens the door for developers interested in building commerce applications using Nuxt.js, providing a streamlined and efficient way to integrate Medusa with their Nuxt.js applications.

How to Install the Module

Install the module in your Nuxt.js project:

npm install --save-dev nuxt-medusa
Enter fullscreen mode Exit fullscreen mode

Then, add nuxt-medusa to the modules section of nuxt.config.ts:

export default defineNuxtConfig({
  modules: ['nuxt-medusa'],
})
Enter fullscreen mode Exit fullscreen mode

Finally, in your .env file add a MEDUSA_URL variable with its value being the URL of the server:

MEDUSA_URL=<YOUR_MEDUSA_URL> # By default http://localhost:9000
Enter fullscreen mode Exit fullscreen mode

How to Use the Module

You can fetch data from Medusa in Nuxt like this:

<script setup lang="ts">
  const client = useMedusaClient();
  const { products } = await client.products.list();
</script>
Enter fullscreen mode Exit fullscreen mode

And you will get the data about your products. It’s that simple!

This approach certainly works in case of fetching products’ data on the client (browser). However, in some cases you may want to fetch this data on the server side of Nuxt (for example, to reduce network latency of customers visiting your website). You can do this with the module as well!

To fetch the data on the server side, let’s first enable this functionality by setting the server option in the configuration to true:

export default defineNuxtConfig({
  modules: ['nuxt-medusa'],
  medusa: {
    server: true
  }
})
Enter fullscreen mode Exit fullscreen mode

This will enable the server utility to be imported in your project and you can use it like following:

import { serverMedusaClient } from '#medusa/server'

export default eventHandler(async (event) => {
  const client = serverMedusaClient(event)
  const { products } = await client.products.list()

  return { products }
})
Enter fullscreen mode Exit fullscreen mode

As you can see, the approach here is almost the same as in the client, with the only difference that we are using the event Handlers of Nuxt to handle this functionality on the server.

Finally, we can call this event handler from the frontend like the following:

<script lang="ts" setup>
  const { products } = await $fetch('/api/products')
</script>
Enter fullscreen mode Exit fullscreen mode

And that’s it! You can now fetch the products on the server. Keep in mind that you are not limited to products only, as you can fetch basically whatever you like as you have access to the Medusa client.

Happy coding!

Want to contribute to the Medusa Community?

If you are also interested in building a community plugin for Medusa and showcasing it to the broader Medusa community, do not hesitate to contact nick@medusajs.com.

We appreciate the contributions!

💖 💪 🙅 🚩
jacobandrewsky
Jakub Andrzejewski

Posted on March 16, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related