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

hexoid is not a function error upon plugin registration on server start #366

Closed
2 tasks done
SaundersB opened this issue Jul 4, 2022 · 8 comments · Fixed by #441
Closed
2 tasks done

hexoid is not a function error upon plugin registration on server start #366

SaundersB opened this issue Jul 4, 2022 · 8 comments · Fixed by #441

Comments

@SaundersB
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.2.0

Plugin version

7.1.0

Node.js version

16.15.1

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

12.4

Description

I'm seeing an error with the hexoid library when I register this plugin with my fastify API:

/Users/bsaunders/src/awards/jnrcorp-apis/dist/apps/claims-api/webpack:/jnrcorp-apis/node_modules/@fastify/multipart/index.js:210
  const toID = hexoid()
^
TypeError: hexoid is not a function
    at fastifyMultipart (/Users/bsaunders/src/awards/jnrcorp-apis/dist/apps/claims-api/webpack:/jnrcorp-apis/node_modules/@fastify/multipart/index.js:210:1)
    at Plugin.exec (/Users/bsaunders/src/awards/jnrcorp-apis/dist/apps/claims-api/webpack:/jnrcorp-apis/node_modules/avvio/plugin.js:131:1)
    at Boot.loadPlugin (/Users/bsaunders/src/awards/jnrcorp-apis/dist/apps/claims-api/webpack:/jnrcorp-apis/node_modules/avvio/plugin.js:273:1)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Is there a reason this plugin wouldn't see one of the libraries fastify-multipart declared in the fastify-multipart package.json?

I've made sure I didn't do anything silly like forget to npm i in my root project.

My usage inside a plugin of my own:

import FastifyMultipart from '@fastify/multipart';

const fastifyApi = async (
  server,
  opts
) => {
...
server.register(FastifyMultipart);
...
};

export default fp(fastifyApi);

which I then register when I run the server with out plugins:

const server = Fastify({
    logger: config.fastify,
});
server.register(startServer, config);
try {
    await server.listen(config.server);
  } catch (err) {
    server.log.error(err);
    process.exit(1);
  }

Steps to Reproduce

  1. create a new fastify API (v4)
  2. register the fastify-multipart plugin
  3. start the server
  4. see the error above

Expected Behavior

the API should start without issue and then allow me to accept multipart form content-type. right now my API only accepts application/json as expected via fastify. however, since the API doesn't start I can't use plugin.

@mcollina
Copy link
Member

mcollina commented Jul 5, 2022

I have non idea. My hunch would be to look into the webpack config or stop using webpack altogether.

@mcollina mcollina closed this as completed Jul 5, 2022
@mcollina
Copy link
Member

mcollina commented Jul 5, 2022

This is 100% not a problem of this module.

@simon-tannai
Copy link

@SaundersB I have the same issue with webpack and a former version of fastify-multipart. If you didn't found any solution, you have way to solve it:

  1. According to this issue, it seems to be able to fix it by use a Webpack Module. I tried it but it was not working for me (NX environment). If you figure it out with this solution, please comment the thread
  2. You can define @fastify/multipart as external dependency. In my NX environments, it looks like this:
"externalDependencies": [
  "fastify-multipart"
]

I suppose in yours it'll be something like:

module.exports = {
  //...
  externals: '@fastify/multipart',
};

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 2, 2023

@mcollina

Can I integrate my perf optimized hexoid jit into fastify-multipart?
lukeed/hexoid#2

@gsimko
Copy link

gsimko commented May 16, 2023

@mcollina I think the problem is that the way hexoid is imported in this module is incompatible with ES6. The default export of that module can be accessed by require('hexoid').default, and that works fine with webpack/babel.

"according to the ES6 module spec, a default export is no different than a named export with the name default. It is just syntactic sugar which can be statically resolved at compile time"
https://stackoverflow.com/questions/33704714/cant-require-default-export-value-in-babel-6-x

@gsimko
Copy link

gsimko commented May 16, 2023

For anyone looking for a quick hacky solution without fixing this module, it's possible from webpack to patch the bad code by adding the following rule:

{
        test: /@fastify\/multipart/,
        loader: 'string-replace-loader',
        options: {
          search: /const toID = hexoid/,
          replace: 'const toID = hexoid.default',
        }
}

@mcollina mcollina reopened this May 17, 2023
@mcollina
Copy link
Member

@Uzlopak go ahead with integrating your lib

@Uzlopak
Copy link
Contributor

Uzlopak commented May 17, 2023

@mcollina

You have luck, I prepared yesterday something after seeing this issue ;)

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.

5 participants