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

error: import_formidable.default) is not a function #305

Closed
2 tasks
cnpap opened this issue Sep 9, 2023 · 1 comment
Closed
2 tasks

error: import_formidable.default) is not a function #305

cnpap opened this issue Sep 9, 2023 · 1 comment
Labels
bug Something isn't working outdated pending triage

Comments

@cnpap
Copy link

cnpap commented Sep 9, 2023

Problem

#303
I mentioned HMR in the mentioned issue. When I perform a restart, I will use require('some.[mjs | cjs]'). If this location is an mjs file, there is a certain probability of encountering an error. This is because I am referencing an ESM file within a CJS module. We have a simple reproduction case.

index.cjs

require('./form.mjs');

form.mjs

// This is a demo provided by Formidable.

import http from 'node:http';
import formidable, { errors as formidableErrors } from 'formidable';

const server = http.createServer(async (req, res) => {
  if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {
    // parse a file upload
    const form = formidable({});
    let fields;
    let files;
    try {
      [fields, files] = await form.parse(req);
    } catch (err) {
      // example to check for a very specific error
      if (err.code === formidableErrors.maxFieldsExceeded) {
      }
      console.error(err);
      res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
      res.end(String(err));
      return;
    }
    res.writeHead(200, { 'Content-Type': 'application/json' });
    console.log(fields, files);
    res.end(JSON.stringify({ fields, files }, null, 2));
    return;
  }

  // show a file upload form
  res.writeHead(200, { 'Content-Type': 'text/html' });
  res.end(`
    <h2>With Node.js <code>"http"</code> module</h2>
    <form action="/api/upload" enctype="multipart/form-data" method="post">
      <div>Text field title: <input type="text" name="title" /></div>
      <input type="submit" value="submit" />
    </form>
  `);
});

server.listen(8080, () => {
  console.log('Server listening on http://localhost:8080/ ...');
});

If I now run tsx run.cjs, I will receive an error.

const form = formidable({});
             ^

TypeError: (0 , import_formidable.default) is not a function
at Server. (d:\code\mono\devview\demo.mjs:7:18)
at Server.emit (node:events:513:28)
at Server.emit (node:domain:489:12)
at parserOnIncoming (node:_http_server:1091:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:119:17)

Node.js v18.16.0

image

Expected behavior

Changing form.mjs to form.ts allowed the code to execute smoothly. I believe this may be an issue with a certain loader file.

Minimal reproduction URL

https://stackblitz.com/edit/node-xbupi5?file=package.json,form.mjs

Version

^3.12.1

Node.js version

v18.16.0

Package manager

npm

Operating system

Windows

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@cnpap cnpap added bug Something isn't working pending triage labels Sep 9, 2023
@privatenumber
Copy link
Owner

Duplicate of #67

BTW, in a minimal reproduction, if your bug happens in an import statement & function call, please remove the server code as it's not necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working outdated pending triage
Projects
None yet
Development

No branches or pull requests

2 participants