Skip to content

Instantly share code, notes, and snippets.

@olivermrbl
Last active February 6, 2022 17:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olivermrbl/abb9300e1bf45dea517cd57a4700054d to your computer and use it in GitHub Desktop.
Save olivermrbl/abb9300e1bf45dea517cd57a4700054d to your computer and use it in GitHub Desktop.
Guide on how to use Strapi with Medusa

Medusa Medusa

Medusa Strapi Plugin

Medusa is released under the MIT license. PRs welcome! Discord Chat Follow @medusajs

Setting up your store

  • Make a working directory

    mkdir medusa-and-strapi
    cd medusa-and-strapi
    
  • Initialize a Medusa project

    yarn global add @medusajs/medusa-cli
    medusa new my-medusa-store
    
    or // if you want Medusa Admin and a Storefront as well
    
    npx create-medusa-app
    
  • Choose medusa-starter-default (only for npx command)

    ? Which Medusa starter would you like to install? …
    ❯ medusa-starter-default
      medusa-starter-contentful
      Other
    
  • Optonal: Pick a storefront starter (only for npx command)

    Which storefront starter would you like to install? …
    ❯ Gatsby Starter
    Next.js Starter
    None
    
  • Make sure redis is installed and running

    $ redis-cli
    127.0.0.1:6379> ping
    PONG
    
  • Go to the Medusa directory (<project name>/backend if you've used npx).

    cd <your project>
    
  • Edit medusa-config.js. Navigate to the end of file and make sure that Redis and Postgres is enabled. These line should be present.

    projectConfig: {
      redis_url: REDIS_URL,
      database_url: DATABASE_URL,
      database_type: "postgres",
      store_cors: STORE_CORS,
      admin_cors: ADMIN_CORS,
    },
    
  • In the same file, add this object to the plugins array

    {
      resolve: `medusa-plugin-strapi`,
      options: {
      strapi_medusa_user: 'medusa_user',
      strapi_medusa_password: 'medusaPassword1',
      strapi_url: '127.0.0.1',
      strapi_port: '1337'
      }
    }
    
  • Navigate to root of working directory and pull medusa-plugin-strapi

    cd ../ <- should be in `medusa-and-strapi` directory
    git clone https://github.com/Deathwish98/medusa-plugin-strapi.git
    
  • Install dependencies and build project files

    cd medusa-plugin-strapi
    npm install
    npm run build
    
  • We are currently simulating a future setup, so some custom linking needs to be done. Navigate to the Medusa project again and do

  cd ../my-medusa-store
  cd node_modules/medusa-interfaces && yarn link

The above will ensure that we use the same version of our interfaces package. Something that will not be an issue when published.

  • Navigate to the Strapi plugin and use local linked interface

      cd ../../../medusa-plugin-strapi && yarn link medusa-interfaces
      yarn link // will make `medusa-plugin-strapi` available as a local package
    
  • Create local Medusa postgres db

    createdb medusa-store
    
  • Navigate to the Medusa project and start your server:

    ../my-medusa-store
    yarn link medusa-plugin-strapi
    npm run seed
    npm run start
    

Your local Medusa server should now be running on port 9000.

Setting up strapi

This plugin assumes that you are familiar with strapi. If you have not used it before, visit the official docs for more info -

https://strapi.io/documentation/developer-docs/latest/getting-started/quick-start.html

  • Navigate to the strapi-medusa directory. It is located in the medusa-plugin-strapi project. Here, the directory strapi-medusa is included inside plugin for simplicity. In real life, you would be using it as a separate project.

  • Install dependencies for strapi.

    cd `medusa-plugin-strapi/strapi-medusa
    npm install
    
  • Choose the database of your choice in config/database.js. For the sake of this guide, please use default Postgres settings.

    client: 'postgres',
    host: env('DATABASE_HOST', '127.0.0.1'),
    port: env.int('DATABASE_PORT', 5432),
    database: env('DATABASE_NAME', 'medusa'),
    username: env('DATABASE_USERNAME', 'medusa'),
    password: env('DATABASE_PASSWORD', 'medusa'),
    ssl: env.bool('DATABASE_SSL', false),
    

    Note: If your Postgres credentials on your local machine is something else, make sure to provide them here.

  • Create local Strapi postgres db

    createdb medusa
    
  • Start strapi server.

    npm run develop
    

    NOTE: If you are using SQLite there is a known knex.js bug -

    error KnexTimeoutError: Knex: Timeout acquiring a connection. The pool is probably full. Are you missing a .transacting(trx) call?
    

    It appears after running npm run develop for the first time . Just run the command again and it should disappear.

Visit docs.medusa-commerce.com for further guides.

Website | Notion Home | Twitter | Docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment