Register components inside a plugin

I have a Strapi app I want to split into multiple plugins. Migrating models seems easy and straightforward - I just move the model configurations into the models folder of the plugin. But the same thing doesn’t work for components.

Is there a way to register components inside a Strapi plugin?

Also, this is the error I receive when trying to do the above:

[2021-06-08T12:02:46.852Z] error TypeError: Cannot read property 'attributes' of undefined
    at Object.getNonVisibleAttributes (/home/dimitar/dev/boom/api/node_modules/strapi-utils/lib/content-types.js:76:11)
    at getNestedFields (/home/dimitar/dev/boom/api/node_modules/strapi-admin/services/content-type.js:30:51)
    at /home/dimitar/dev/boom/api/node_modules/strapi-admin/services/content-type.js:43:31
    at /home/dimitar/dev/boom/api/node_modules/lodash/lodash.js:928:11
    at /home/dimitar/dev/boom/api/node_modules/lodash/lodash.js:4967:15
    at baseForOwn (/home/dimitar/dev/boom/api/node_modules/lodash/lodash.js:3032:24)
    at /home/dimitar/dev/boom/api/node_modules/lodash/lodash.js:4936:18
    at baseReduce (/home/dimitar/dev/boom/api/node_modules/lodash/lodash.js:925:5)
    at Function.reduce (/home/dimitar/dev/boom/api/node_modules/lodash/lodash.js:9749:14)
    at getNestedFields (/home/dimitar/dev/boom/api/node_modules/strapi-admin/services/content-type.js:32:12)
    at /home/dimitar/dev/boom/api/node_modules/strapi-admin/services/content-type.js:132:11
    at Array.reduce (<anonymous>)
    at Object.getPermissionsWithNestedFields (/home/dimitar/dev/boom/api/node_modules/strapi-admin/services/content-type.js:126:18)
    at Object.resetSuperAdminPermissions (/home/dimitar/dev/boom/api/node_modules/strapi-admin/services/role.js:409:50)
    at async module.exports (/home/dimitar/dev/boom/api/node_modules/strapi-admin/config/functions/bootstrap.js:51:3)
    at async Strapi.load (/home/dimitar/dev/boom/api/node_modules/strapi/lib/Strapi.js:379:5)
    at async Strapi.start (/home/dimitar/dev/boom/api/node_modules/strapi/lib/Strapi.js:196:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
System Information
  • Strapi Version: 3.6.2
  • Operating System: Ubuntu 20.04.2 LTS
  • Database: MySQL 8.0.22
  • Node Version: 14.17.0
  • NPM Version: 7.16.0
  • Yarn Version: 1.22.10

2 Likes

Sadly, components aren’t working from inside plugins. I’d super love to see this as well.

Hi,
I’m the same situation above… there is any news about this topic? :frowning:
Vito

hi there,

i have the same problem right now and came across the site via google. even if the post is already 2 years old. here is the solution

// src/plugins/strapi-plugin-pages/server/register.ts
import { Schema, Strapi } from '@strapi/strapi';

export default ({ strapi }: { strapi: Strapi }) => {
  // register phase
  const data: Schema.Component = {
    collectionName: "components_test_bars",
    category: "test",
    modelName: "bar",
    globalId: "ComponentTestBar",
    uid: "test.bar",
    modelType: "component",
    attributes: {
      foo: {
        type: "string",
      },
    },
    options: {},
    pluginOptions: {}
  };

  strapi.components[data.uid] = data;
};

Afterwards you can use your component inside your schema. For example

{
  //...
  "attributes": {
    "dyn": {
      "type": "dynamiczone",
      "components": [
        "test.bar"
      ]
    },
//...