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

@koa/multer still use 'require' instead of 'import' in 'koa.hbs', lead to ESM project run failure #1451

Closed
3 of 5 tasks
wazho opened this issue Jul 16, 2023 · 1 comment · Fixed by #1452
Closed
3 of 5 tasks

Comments

@wazho
Copy link
Contributor

wazho commented Jul 16, 2023

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Expected Behavior

The generated src/routes/index.ts still uses

const multer = require('@koa/multer');

instead of

import multer from '@koa/multer';

Current Behavior

Run the unexpected routes/index.ts, and then throw the error:

ReferenceError: require is not defined in ES module scope, you can use import instead

Possible Solution

packages/cli/src/routeGeneration/templates/koa.hbs have to update the source from

{{#if useFileUploads}}
const multer = require('@koa/multer');
const upload = multer({{{json multerOpts}}});
{{/if}}

to

{{#if useFileUploads}}
import multer from '@koa/multer';
const upload = multer({{{json multerOpts}}});
{{/if}}

Steps to Reproduce

  1. Prepare the tsoa.json partial config below:
{
  "routes": {
    "routesDir": "src/routes",
    "routesFileName": "index.ts",
    "middleware": "koa"
  }
}
  1. Use decorator @UploadedFile in method of controller.
@Route('files')
@Tags('Files')
class UploadFileController extends Controller {
  @Post('')
  async uploadFile(
    @FormField() fileName: string,
    @UploadedFile() file: multer.File
  ): Promise<any> {
    // ...
  }
}
  1. Execute tsoa to generate routes/index.ts successfully.
  2. When project is ESM project, run the app will receive error:
ReferenceError: require is not defined in ES module scope, you can use import instead

Context (Environment)

Version of the library: 5.0.0
Version of NodeJS: v18.12.0

  • Confirm you were using yarn not npm:
    • using yarn

Detailed Description

When I observe the same file (packages/cli/src/routeGeneration/templates/koa.hbs), most of requirements use import instead of require. Only useFileUploads has been ignored.

// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
{{#if canImportByAlias}}
  import { Controller, ValidationService, FieldErrors, ValidateError, TsoaRoute, HttpStatusCodeLiteral, TsoaResponse, fetchMiddlewares } from '@tsoa/runtime';
{{else}}
  import { Controller, ValidationService, FieldErrors, ValidateError, TsoaRoute, TsoaResponse, HttpStatusCodeLiteral } from '../../../src';
{{/if}}
{{#each controllers}}
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
import { {{name}} } from '{{modulePath}}';
{{/each}}
{{#if authenticationModule}}
import { koaAuthentication } from '{{authenticationModule}}';
// @ts-ignore - no great way to install types from subpackage
{{/if}}
{{#if iocModule}}
import { iocContainer } from '{{iocModule}}';
import type { IocContainer, IocContainerFactory } from '@tsoa/runtime';
{{/if}}
import type { Middleware } from 'koa';
import type * as KoaRouter from '@koa/router';
{{#if useFileUploads}}
const multer = require('@koa/multer'); // <------------------ THIS LINE
const upload = multer({{{json multerOpts}}});
{{/if}}

Breaking change?

I think this change doesn't cause breaking change.

@github-actions
Copy link

Hello there wazho 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant