[bug]: can't `require` some dependencies on server component

Open
#60 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript, node.js
Domain
backend, tooling

Research direction

Start with the generated server component and the example service.js from the reproduction, then inspect the output at dist/server/index.js after adding uuid with require. Reproduce the failure using the listed sdk-plugin init and Strapi develop steps. Done means the generated plugin starts successfully and the server service can import and use the dependency without a dynamic try wrapper.

Written by the indexing model from the issue text.

Description

issue: bug severity: low
What version of @strapi/sdk-plugin are you using?
  • npm version: 10.8.2
  • node version: 20.17.0
  • @strapi/sdk-plugin version: 5.2.6
What's Wrong?

Using the template created by sdk-plugin for Strapi v5 some imports inside the server component don't work. Building the plugin itself seems to work without problem, however once I try to start Strapi 5.0.0 I get the following error:

Error: Could not load js config file ~/test-plugin/dist/server/index.js: Cannot destructure property 'v4' of 'require$$0__default.default' as it is undefined.
at loadJsFile (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/utils/load-config-file.js:18:13)
at Module.loadConfigFile (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/utils/load-config-file.js:37:14)
at loadPlugins (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/loaders/plugins/index.js:118:41)
at async Promise.all (index 3)
at async Module.loadApplicationContext (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/loaders/index.js:12:3)
at async Object.register (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/providers/registries.js:26:5)
at async Strapi.register (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/Strapi.js:322:7)
at async Strapi.load (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+language_s62dddslq2bxwrlfinmqz5vbcm/node_modules/@strapi/core/dist/Strapi.js:314:5)
at async Module.develop (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+langua_zxfweiqacyjrhnmmqug74nt5xi/node_modules/@strapi/strapi/dist/node/develop.js:177:28)
at async action (~/cms/node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@codemirror+langua_zxfweiqacyjrhnmmqug74nt5xi/node_modules/@strapi/strapi/dist/cli/commands/develop.js:18:5)

This started happening only from RC.18 to the stable version, instead since the first RC up to RC.17 it was working without problems.

To Reproduce
  1. Create plugin with npx @strapi/sdk-plugin@latest init test-plugin, with TypeScript disabled.
  2. Import a dependency npm install uuid
  3. Use dependency on server. For example, I changed service.js like this:
'use strict';

const {v4} = require("uuid")

const service = ({ strapi }) => ({
  getWelcomeMessage() {
    return 'Welcome to Strapi 🚀, uuid: ' + v4();
  },
});

module.exports = service;
  1. Include plugin and start Strapi npm strapi develop
  2. Strapi crashes while trying to start, with the error above.

The issue can be circumvented by requiring uuid dynamically wrapping in a try, but that shouldn't be necessary.

Expected Behaviour

Strapi should start successfully, correctly importing the dependency.

Dominant language
TypeScript
Stars
24
Forks
13
Avg merge
5d 3h
Merged PRs (30d)
5

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from strapi/sdk-plugin

All issues in strapi/sdk-plugin

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.