Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using ES import issue with monorepo workspace packages #20294

Open
vks-dbb opened this issue May 10, 2024 · 1 comment
Open

Using ES import issue with monorepo workspace packages #20294

vks-dbb opened this issue May 10, 2024 · 1 comment
Assignees
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: pending reproduction Waiting for free time to reproduce the issue, or more information

Comments

@vks-dbb
Copy link

vks-dbb commented May 10, 2024

Bug report

Required System information

20.1.3 4.24.1
  • Node.js version: 18.18.2
  • NPM version: 9.8.1
  • Strapi version: 4.24.1
  • Database: sqlite
  • Operating system: macos/linux(ubuntu 20)
  • Is your project Javascript or Typescript: typescript

Describe the bug

A clear and concise description of what the bug is.

I encountered the problem of not being able to import files from workspace packages (turbo monorepo with using workspaces), in my case it was the @some/common package, complaining that this is not supported in the ts file.

strapi:build: cache bypass, force executing 9df377beb679af49
strapi:build: [WARN] [@strapi/strapi]: The NODE_ENV is not set to production. This may result in unexpected behavior.
strapi:build: ⠋ Compiling TS src/api/healthcheck/controllers/healthcheck.ts:4:38 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@some/common")' call instead.
@dbbs/strapi:build: To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field "type": "module" to '/apps/strapi/package.json'.
strapi:build:
strapi:build: import { SettingServiceClient } from '@some/common'

Steps to reproduce the behavior

  1. Add monorepo package (included in workspaces) to strapi dependencies
  2. Try to import something from this package for example in strapi api's routes
  3. Try to run strapi app

Expected behavior

Want some help how to use monorepo packages in strapi, need solution to pass compilation step and problems with imports,
the main problem that the strapi expecting importing module building with CommonJS, but we don't needed

Screenshots

If applicable, add screenshots to help explain your problem.

Code snippets

strapi:build: cache bypass, force executing 9df377beb679af49
strapi:build: [WARN] [@strapi/strapi]: The NODE_ENV is not set to production. This may result in unexpected behavior.
strapi:build: ⠋ Compiling TS src/api/healthcheck/controllers/healthcheck.ts:4:38 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("@some/common")' call instead.
@dbbs/strapi:build: To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field "type": "module" to '/apps/strapi/package.json'.
strapi:build:
strapi:build: import { SettingServiceClient } from '@some/common'

Additional context

my tsconfig configuration:

{
   "extends": [
     "@strapi/typescript-utils/tsconfigs/server"
   ],
   "compilerOptions": {
     "rootDir": ".",
     "module": "NodeNext",
     "target": "ESNext",
     "moduleResolution": "NodeNext",
     "outDir": "dist",
     "noImplicitAny": false,
     "allowJs": true,
     "composite": false,
     "declaration": false
   },
   "include": [
     "./",
     "./**/*.ts",
     "./**/*.mts",
     "./**/*.js",
     "src/**/*.json"
   ],
   "exclude": [
     "node_modules/",
     "build/",
     "dist/",
     ".cache/",
     ".tmp/",
     "src/admin/",
     "**/*.test.*",
     "src/plugins/**"
   ]
}

@joshuaellis joshuaellis added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: pending reproduction Waiting for free time to reproduce the issue, or more information source: typescript Source is related to TypeScript (typings, tooling, ...) labels May 14, 2024
@antokhio
Copy link

Hi, you can look how it's done here https://github.com/antokhio/turbo-strapi-next-rtk.

But yes you need to bundle your ‘common` package to CJS and ESM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: pending reproduction Waiting for free time to reproduce the issue, or more information
Projects
Status: To be reviewed (Open)
Status: To review
Development

No branches or pull requests

4 participants