Nuxt-Medusa Module: Integrate Medusa with your Nuxt.js application
Tags: javascript, webdev, programming, showdev
Published: 2023-03-16 08:59:03


Great news for Nuxt fans. [Jakub Andrzejewski](https://twitter.com/jacobandrewsky) 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](https://github.com/baroshem/nuxt-medusa), including a tutorial, documentation, video, and release notes. You can also try it directly in your browser using the [module sandbox on Stackblitz](https://stackblitz.com/github/baroshem/nuxt-medusa?file=.stackblitz%2Fnuxt.config.ts).


{% embed https://www.youtube.com/watch?v=EuakPz8M7CM %}


> W**hat is Medusa?** [Medusa](https://medusajs.com/) 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](https://www.npmjs.com/package/@medusajs/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](https://nuxt-medusa.vercel.app/getting-started/setup). 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.

- [**useMedusaClient**](https://nuxt-medusa.vercel.app/features/usemedusaclient) **composable**.
- [**serverMedusaClient**](https://nuxt-medusa.vercel.app/features/servermedusaclient#servermedusaclient) **utility.**

At this moment, there aren’t more features available, but the module maintainer [opened an issue ](https://github.com/Baroshem/nuxt-medusa/issues/13)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:


```bash
npm install --save-dev nuxt-medusa
```


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


```typescript
export default defineNuxtConfig({
  modules: ['nuxt-medusa'],
})
```


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


```bash
MEDUSA_URL=<YOUR_MEDUSA_URL> # By default http://localhost:9000
```


## How to Use the Module


You can fetch data from Medusa in Nuxt like this:


```html
<script setup lang="ts">
  const client = useMedusaClient();
  const { products } = await client.products.list();
</script>
```


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`:


```typescript
export default defineNuxtConfig({
  modules: ['nuxt-medusa'],
  medusa: {
    server: true
  }
})
```


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


```typescript
import { serverMedusaClient } from '#medusa/server'

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

  return { products }
})
```


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:


```typescript
<script lang="ts" setup>
  const { products } = await $fetch('/api/products')
</script>
```


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](https://medusajs.com/) community, do not hesitate to contact nick@medusajs.com.


We appreciate the contributions!