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

Could this support ESM when I set module 'ESNext' at tsconfig.json #164

Open
stevenhobs opened this issue Jan 1, 2023 · 2 comments
Open

Comments

@stevenhobs
Copy link

我编译程序的js文件为 mjs, SwaggerRouter 的mapDir函数操作会报错,因为 require 语句
.pnpm\[email protected]\node_modules\koa-swagger-decorator\dist\utils.js:46
const obj = require(filepath);
^

Error [ERR_REQUIRE_ESM]: require() of ES Module .......\router\pub.router.js from E:\project\mtd-koa\node_modules.pnpm\[email protected]\node_modules\koa-swagger-decorator\dist\utils.js not supported.

@ggomaeng
Copy link

ggomaeng commented Apr 4, 2023

+1

@regis-underdog
Copy link

I am also facing this issue with Vitest. It appears to be an issue in the lines referencing a require statement.

src/main.js

import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import cors from '@koa/cors';
import logger from 'koa-logger';
import errorHandler from 'koa-better-error-handler';
import koa404Handler from 'koa-404-handler';

import { router } from '@/controller/index';

const app = new Koa();

app.context.onerror = errorHandler();
app.context.api = true;

app
  .use(logger())
  .use(router.allowedMethods())
  .use(cors())
  .use(bodyParser())
  .use(koa404Handler)
  .use(router.routes());

export { app };

And src/controller/index.ts

import Router from '@koa/router';
import { SwaggerRouter, request, summary, query, path, body, tags } from 'koa-swagger-decorator';

const router = new SwaggerRouter();
const testTag = tags(['test']);

// swagger docs avaliable at http://localhost:3000/swagger-html
router.swagger({
  title: 'API Server',
  description: 'API DOC',
  version: '1.0.0',
});

export class HealthChecker {
  @request('get', '/readyz')
  @summary('Check if webserver is ready to receive traffic')
  @query({
    verbose: {
      type: 'boolean',
      required: false,
      default: true,
      description: 'Show full output',
    },
  })
  static async readyz(ctx) {
    ctx.body = { message: 'hello world' };
  }
}

router.map(HealthChecker, { doValidation: false });

export { router };

This code above works when running the Koa server with tsx.

However, with Vitest file src/test/controller/index.test.ts

import { vi, expect, test } from 'vitest';
import request from 'supertest';

// This does not work, see https://github.com/Cody2333/koa-swagger-decorator/issues/164
import { app } from 'src/main';

test('readyz probe works', async () => {
  const response = await request(app.callback()).get('/readyz');
  expect(response.status).toBe(200);
  expect(response.text).toBe('Hello World');
});

Returns the following errors

 FAIL  src/test/controller/index.test.ts [ src/test/controller/index.test.ts ]
SyntaxError: Cannot use import statement outside a module
 ❯ loadModule node_modules/.pnpm/[email protected]_@[email protected]/node_modules/koa-swagger-decorator/lib/utils.ts:43:15
 ❯ loadClass node_modules/.pnpm/[email protected]_@[email protected]/node_modules/koa-swagger-decorator/lib/utils.ts:51:15
 ❯ node_modules/.pnpm/[email protected]_@[email protected]/node_modules/koa-swagger-decorator/lib/utils.ts:60:22

Module src/controller/v1/index.ts:1 seems to be an ES Module but shipped in a CommonJS package. To fix this issue, change the file extension to .mjs or add "type": "module" in your package.json.

Please note, that type: module is correctly set in my package.json file. It is actually complaining about the node_modules directory.

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

No branches or pull requests

3 participants