How to structure reusable code to import in various Strapi files

System Information
  • Strapi Version: 3.1.3
  • Operating System: Linux
  • Database: MySQL
  • Node Version: 12.18.3
  • NPM Version: 6.14.6
  • Yarn Version: 1.22.4

I wrote some reusable functions that I want to import into various controllers. Where can I place the code so that the controller can import it?

At the moment I placed it in ./api/utils/utils.js and the code looks like:

export function foo() { 
  console.log('It works'); 
}

However, when I am trying to import that code to the ./api/blog/controllers/blog.js as:

import { foo } from `../../utils/utils.js`;

And then run with yarn develop I get the following exception:

SyntaxError: Cannot use import statement outside a module

As far as I understand the reason why this was not working is that I was using the wrong way of importing the function from the module. Based on CommonJS the appropriate way to include a module in ./api/blog/controllers/blog.js is by:

const { foo } = require('../../utils/utils.js');

This has nothing to do with Strapi.

However, I would be interested to know if there is any best practice on where to place reusable code in Strapi.

See: https://strapi.io/documentation/developer-docs/latest/concepts/configurations.html#functions

:wink:

This link is broken

To solve the error, set the type attribute to module when loading the script in your HTML code. When working with ECMAScript modules and JavaScript module import statements in the browser, you’ll need to explicitly tell the browser that a script is module. To do this, you have to add type=“module” onto any ‹script› tags that point to a JavaScript module. Once you do this you can import that module without issues.

<script type="module" src="./index.js"></script>

If you are working on Node.js or react applications and using import statements instead of require to load the modules, then ensure your package.json has a property “type”: “module” as shown below.

{
  // ...
  "type": "module",
  // ...
}

Moreover, In some cases, you may have to use both import and require statements to load the module properly.

// import { parse } from 'node-html-parser';
parse = require('node-html-parser');

This error "Cannot use import statement outside a module " can happen in different cases depending on whether you’re working with JavaScript on the server-side with Node.js , or client-side in the browser. There are several reasons behind this error, and the solution depends on how you call the module or script tag.

Im not getting how to do this in my case. I have custom API’s that I create manually. So what I did now is create a config/medusa-api/routes folder with a cart-routes.js file in there with the following content:

// Cart routes for medusa API

module.exports = [
  {
    method: 'POST',
    path: '/medusa/carts',
    handler: 'medusa-api.postCart',
    config: {
      policies: [],
      middlewares: [],
    },
  }
]

Now I want to use that in my src/api/medusa-api/routes/medusa-api.js file but I have no clue how.
It should be something like this I think, but I cant get it to work:

module.exports = {
  routes: [
    ...strapi.config.get('medusa-api.routes.cart-routes'),
  ],
};

Can you help me out here?

Did you get help with it?

Yeah I ended up using regular imports and exports @LuisAlaguna

is it required to synchronize Strapi and Medusa databases? I think, as Strapi is a CMS and it will handle the static content, we would not need to synch them but give different responsibilities.