Comments (11)
const koaBody = require('koa-body').default;
use this is fine
from koa-body.
@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
- 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.
- 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)
- 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';
orconst 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.
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.
@lbesson but this was also not possible before since it never exported an esm module or am i wrong?
from koa-body.
@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.
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.
published + [email protected]
from koa-body.
@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.
@damianobarbati try
import { koaBody } from 'koa-body';
from koa-body.
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.
@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)
- Not able to parse multipart form data using new FormData HOT 1
- Cannot read properties of undefined (reading 'body') HOT 2
- [fix] Update changelog HOT 1
- With "hashalgorithm: MD5" set, how to get the MD5 value of a file in "onfilebegin"? HOT 1
- [fix] application/xml body can not get from ctx.request.body HOT 2
- Incorrect parameter comment
- [feat] New major version release HOT 2
- Versi 6.x : Cannot destructure property 'XXX' of 'ctx.request.body' as it is undefined. HOT 8
- require('koa-body/unparsed.js') throws MODULE_NOT_FOUND error
- formidable 2.0.1 has some wrong HOT 4
- 内部使用的co-body库的json校验出现造成代码错误
- Multiplart option does not work with @koa/router? HOT 2
- [fix] Version 5.0.0 is missing some breaking changes on the CHANGELOG.md
- Json reviver func option
- [fix] A TypeScript error occurs when tsc is used to build a project HOT 2
- ctx.req.on("close", fn) koa hook is never called, when defined after `app.use(bodyParser)` HOT 1
- [fix] Webpack WARNING
- [fix] Types are incorrect with Typescript & ESM
- [fix] raw body can't give Symbol when content type is `multipart/form-data`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from koa-body.