Giter Club home page Giter Club logo

Comments (11)

sschen86 avatar sschen86 commented on May 27, 2024 1

const koaBody = require('koa-body').default;
use this is fine

from koa-body.

TheDadi avatar TheDadi commented on May 27, 2024

@risu-p As @sschen86 mentioned correctly [email protected] requires you to add the .default in your require statement. @MarkHerhold There are multiple ways of solving this issue

  1. Get rid of the default export and make it a named export e.g.
  • const { createKoaBodyMiddleware } = require('koa-body');
  • import { createKoaBodyMiddleware } from 'koa-body';
    which is compatible with both commonJS and module syntax.
  1. Leave it as it is now and update the README.md to match the new require statement. (an example of an other module that did this -> conventional-changelog/commitlint#2425)
  2. Leave the default export as is and add an additional named export for syntactic sugaring, which then allows to either
  • const { createKoaBodyMiddleware } = require('koa-body');
  • import { createKoaBodyMiddleware } from 'koa-body';
    or
  • const createKoaBodyMiddleware = require('koa-body').default;
  • import createKoaBodyMiddleware from 'koa-body';

You decide 🦺

This is only a problem when using the module with commonJS require not for import / export

from koa-body.

lbesson avatar lbesson commented on May 27, 2024

It is currently also not possible to use koaBody in a typescript esm project.

If you import koaBody from 'koa-body'; then at runtime you'll get a TypeError: koaBody is not a function but you cannot use koaBody.default since default is not typed

from koa-body.

TheDadi avatar TheDadi commented on May 27, 2024

@lbesson but this was also not possible before since it never exported an esm module or am i wrong?

from koa-body.

lbesson avatar lbesson commented on May 27, 2024

@TheDadi I'm still looking at it. But I can confirm that this was working with versions 5.0.0 (and previous ones) with my setup (typescript + esm), and not anymore since 6.0.0 (see for example c2corg/c2c_images#83).
It can normally import both commonjs or esm modules without issues.

I'll investigate whenever I have time, and report whatever I find.
In any case, I think accessing createKoaBodyMiddleware with a named import would solve my issue.

from koa-body.

MarkHerhold avatar MarkHerhold commented on May 27, 2024

I think we can just add a named export and change the docs to reflect that as the preferred usage pattern. I'm on vacation at the moment but can review a PR

from koa-body.

MarkHerhold avatar MarkHerhold commented on May 27, 2024

published + [email protected]

from koa-body.

damianobarbati avatar damianobarbati commented on May 27, 2024

@MarkHerhold is this the way now (ESM)?

import body from 'koa-body';

const app = new koa();
app.use(body.default());

Because is a little bit sucky 😢

from koa-body.

MarkHerhold avatar MarkHerhold commented on May 27, 2024

@damianobarbati try

import { koaBody } from 'koa-body';

from koa-body.

saschanaz avatar saschanaz commented on May 27, 2024

Something is weird here, if you try await import("koa-body") in the console:

> await import("koa-body")
[Module: null prototype] {
  HttpMethodEnum: {
    POST: 'POST',
    GET: 'GET',
    PUT: 'PUT',
    PATCH: 'PATCH',
    DELETE: 'DELETE',
    HEAD: 'HEAD'
  },
  KoaBodyMiddlewareOptionsSchema: ZodObject {
    spa: [Function: bound safeParseAsync] AsyncFunction,
    superRefine: [Function: bound _refinement],
    _def: {
      shape: [Function: shape],
      unknownKeys: 'strip',
      catchall: [ZodNever],
      typeName: 'ZodObject'
    },
    parse: [Function: bound parse],
    safeParse: [Function: bound safeParse],
    parseAsync: [Function: bound parseAsync] AsyncFunction,
    safeParseAsync: [Function: bound safeParseAsync] AsyncFunction,
    refine: [Function: bound refine],
    refinement: [Function: bound refinement],
    optional: [Function: bound optional],
    nullable: [Function: bound nullable],
    nullish: [Function: bound nullish],
    array: [Function: bound array],
    promise: [Function: bound promise],
    or: [Function: bound or],
    and: [Function: bound and],
    transform: [Function: bound transform],
    default: [Function: bound default],
    describe: [Function: bound describe],
    isNullable: [Function: bound isNullable],
    isOptional: [Function: bound isOptional],
    _cached: null,
    nonstrict: [Function: passthrough],
    augment: [Function (anonymous)],
    extend: [Function (anonymous)]
  },
  __esModule: true,
  default: {
    koaBody: [Function: koaBody],
    HttpMethodEnum: [Getter],
    KoaBodyMiddlewareOptionsSchema: [Getter],
    default: [Function: koaBody]
  },
  koaBody: [Function: koaBody]
}

default should be koaBody but somehow the default is the module object again.

from koa-body.

PatrickBauer avatar PatrickBauer commented on May 27, 2024

@saschanaz
I was really confused by this too when I tried to understand what is happening. As someone who wants to understand the things they use, I tried to find out what happens.

The NodeJS ESM loader supports importing CommonJS Modules (like koa-body). While importing it takes all named exports and puts them into a default export. This happens here: https://github.com/nodejs/node/blob/ecde9d9640e9b590a153c78b7dacc2739337f2a8/lib/internal/modules/esm/translators.js#L256

Its kind of a compatibility layer. What koa-body is doing right now (exporting "default" inside a CJS file) is not supported like they expected and will produce the error that was encountered here.

More information on why this is happening:
nodejs/node#48899
nodejs/node#33416
nodejs/node#35249

Bundlers take care of this as far as I understand, but the NodeJS default behavior for importing CJS into EMS is different.

from koa-body.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.