Home
Blog
Community

Nuxt-Medusa Module: Integrate Medusa with your Nuxt application

Mar 15, 2023 by

lorem-ipsum avatar

Jakub Andrzejewski

lorem-ipsum avatar

Jakub Andrzejewski

Understand how the new Nuxt-Medusa module works and how to get started with it.
Nuxt-Medusa Module: Integrate Medusa with your Nuxt.js application
Notice Medusa is under active development, so the content of this post may be outdated. Please check out our documentation instead.
Great news for Nuxt fans. Jakub Andrzejewski recently created a new
Copy to clipboard
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.
What 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
Then, add
Copy to clipboard
nuxt-medusa
to the
Copy to clipboard
modules
section of
Copy to clipboard
nuxt.config.ts
:
export default defineNuxtConfig({
modules: ['nuxt-medusa'],
})
Finally, in your
Copy to clipboard
.env
file add a
Copy to clipboard
MEDUSA_URL
variable with its value being the URL of the server:
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:
<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
Copy to clipboard
server
option in the configuration to
Copy to clipboard
true
:
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:
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:
<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 community, do not hesitate to contact nick@medusajs.com.
We appreciate the contributions.

Share this post

Try Medusa

Spin up your environment in a few minutes.