Skip to content

[V4] TS Global Middleware not picked up #20322

@antokhio

Description

@antokhio

Bug report

Following from https://discord.com/channels/811989166782021633/1240319511857463429

Seems there is error in doc's or in the way the global middleware imported.
I try to follow steps from docs https://docs.strapi.io/dev-docs/backend-customization/middlewares to add global middleware and ran in to issue.

Required System information

  • Node.js version: v18.18.2
  • NPM version: 8.13.1
  • Strapi version: 4.17.1
  • Database: Postgres
  • Operating system: windows 10 pro
  • Is your project Javascript or Typescript: Typescript

Describe the bug

A clear and concise description of what the bug is.

Steps to reproduce the behavior

  1. Add middleware to an empty project src/middlewares/test.ts
export default (config, { strapi }) => {
  return async (ctx, next) => {
    console.log('test');
    await next();
  };
};
  1. Add middleware to config/middlewares.ts
export default [
  'strapi::errors',
  'strapi::security',
  'strapi::cors',
  'strapi::poweredBy',
  'strapi::logger',
  'strapi::query',
  'strapi::body',
  'strapi::session',
  'strapi::favicon',
  'strapi::public',
  'global::test ',
];
  1. Error:
Error: Middleware global::test  not found. 

Expected behavior

It should work

Additional context

$ strapi middlewares:list
┌─────────────────────────────────────┐
│ Name                                │
├─────────────────────────────────────┤
│ admin::rateLimit                    │
├─────────────────────────────────────┤
│ admin::data-transfer                │
├─────────────────────────────────────┤
│ global::test                        │
├─────────────────────────────────────┤
│ strapi::compression                 │
├─────────────────────────────────────┤
│ strapi::cors                        │
├─────────────────────────────────────┤
│ strapi::errors                      │
├─────────────────────────────────────┤
│ strapi::favicon                     │
├─────────────────────────────────────┤
│ strapi::ip                          │
├─────────────────────────────────────┤
│ strapi::logger                      │
├─────────────────────────────────────┤
│ strapi::poweredBy                   │
├─────────────────────────────────────┤
│ strapi::body                        │
├─────────────────────────────────────┤
│ strapi::query                       │
├─────────────────────────────────────┤
│ strapi::responseTime                │
├─────────────────────────────────────┤
│ strapi::responses                   │
├─────────────────────────────────────┤
│ strapi::security                    │
├─────────────────────────────────────┤
│ strapi::session                     │
├─────────────────────────────────────┤
│ strapi::public                      │
├─────────────────────────────────────┤
│ plugin::users-permissions.rateLimit │
└─────────────────────────────────────┘

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: bugIssue reporting a bugseverity: mediumIf it breaks the basic use of the product but can be worked aroundsource: typescriptSource is related to TypeScript (typings, tooling, ...)stalestale-q1-q2-2024status: pending reproductionWaiting for free time to reproduce the issue, or more information

    Type

    No type

    Projects

    Status

    Fixed/Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions