node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.05k stars 682 forks source link

Frustrating warnings when used with webpack #337

Closed snario closed 2 years ago

snario commented 9 years ago
WARNING in ./~/harmony/~/formidable/lib/incoming_form.js
Critical dependencies:
1:43-50 require function is used in a way, in which dependencies cannot be statically extracted
 @ ./~/harmony/~/formidable/lib/incoming_form.js 1:43-50

WARNING in ./~/harmony/~/formidable/lib/incoming_form.js
Critical dependencies:
1:0-14 the request of a dependency is an expression
 @ ./~/harmony/~/formidable/lib/incoming_form.js 1:0-14

WARNING in ./~/harmony/~/formidable/lib/file.js
Critical dependencies:
1:43-50 require function is used in a way, in which dependencies cannot be statically extracted
 @ ./~/harmony/~/formidable/lib/file.js 1:43-50

WARNING in ./~/harmony/~/formidable/lib/file.js
Critical dependencies:
1:0-14 the request of a dependency is an expression
 @ ./~/harmony/~/formidable/lib/file.js 1:0-14

WARNING in ./~/harmony/~/formidable/lib/querystring_parser.js
Critical dependencies:
1:43-50 require function is used in a way, in which dependencies cannot be statically extracted
 @ ./~/harmony/~/formidable/lib/querystring_parser.js 1:43-50

WARNING in ./~/harmony/~/formidable/lib/querystring_parser.js
Critical dependencies:
1:0-14 the request of a dependency is an expression
 @ ./~/harmony/~/formidable/lib/querystring_parser.js 1:0-14
tdeheurles commented 9 years ago

Hi, Having same issue.

WARNING in ./~/restify/~/formidable/lib/incoming_form.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/restify/~/formidable/lib/incoming_form.js 1:43-50

WARNING in ./~/restify/~/formidable/lib/file.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/restify/~/formidable/lib/file.js 1:43-50

WARNING in ./~/restify/~/formidable/lib/json_parser.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/restify/~/formidable/lib/json_parser.js 1:43-50

WARNING in ./~/restify/~/formidable/lib/querystring_parser.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/restify/~/formidable/lib/querystring_parser.js 1:43-50

I'm importing restify that uses formidable. Compiling with webpack generate an issue :

var crypto = require('crypto');
                     ^
TypeError: undefined is not a function

Does it tells something to someone ? Running var crypto = require('crypto'); in my code does not generate that problem.

tdeheurles commented 9 years ago

For webpack user, adding :

plugins: [
     new webpack.DefinePlugin({ "global.GENTLY": false })
],

fix the issue.

quicksnap commented 8 years ago

@tdeheurles Just wanted to say thank you for this. Ran into this when packaging for Electron--was very frustrating.

quicksnap commented 8 years ago

Looking at the webpack output, this issue could most likely be fixed by changing this line: https://github.com/felixge/node-formidable/blob/master/lib/incoming_form.js#L1 to something like this:

var require = require;
if (global.GENTLY) require = GENTLY.hijack(require);

Adding var require will prevent Webpack from shadowing the actual require with an empty var require; before the if (global.GENTLY).

Would a PR like that be accepted?

Duh.. that would break everything..

EDIT Clicked "Comment" too soon.

davis commented 8 years ago

this is still an issue

gabrielstuff commented 8 years ago

yup still be, and I confirm :

plugins: [
     new webpack.DefinePlugin({ "global.GENTLY": false })
 ]

Fix it

benjie commented 7 years ago

Apologies for polluting the thread, but for anyone landing on this and still having issues afterwards who happens to be using superagent, note they have a wiki page saying what to do

[...] you can either use superagent's browser version directly (require('superagent/lib/client')), or fix your Webpack settings:

Add:

plugins.push(new webpack.DefinePlugin({ "global.GENTLY": false }));

and

node: {
  __dirname: true,
}

I also added the following, for good measure:

    alias: {
      'inherits': 'inherits/inherits_browser.js',
      'superagent': 'superagent/lib/client',
      'emitter': 'component-emitter',
    },

This cost me more time than I'd like to admit (oh look at that, it's just gone midnight), so posting here in the hopes it helps someone else since I landed on this page early on in my search...

tunnckoCore commented 7 years ago

Yea, that definitely a problem of how whole library is written. And kind of sucks. I believe we could change it and improve it for v2 release.

isaachinman commented 7 years ago

Just ran into this and lost an hour's time trying to figure it out!

joseSantacruz commented 7 years ago

I have the same issue

nazmy commented 7 years ago

Hi @benjie, I'm using superagent and still getting the error after adding the config in the webpack. I'm using webpack 2.4.1.

Here's my webpack.config snippets:

const plugins = [
    new webpack.DefinePlugin({ "global.GENTLY": false }),
    new webpack.IgnorePlugin(/\.(css|less)$/)
]

module.exports = {
    entry: handlers,
    output: {
        libraryTarget: 'commonjs',
        path: paths.appBuild,
        filename: '[name].js',
    },
    node: {
        __dirname: true,
    },
    externals: [
        'aws-sdk',
    ],
    resolve: {
        fallback: paths.nodePaths,
        extensions: ['.js', '.json', ''],
    },
    module: {
        preLoaders: preLoaders,
        loaders: loaders
    },
    plugins: plugins,
    target: 'node',
    devtool: 'source-map',
}

Warning Notification:

WARNING in ./~/formidable/lib/incoming_form.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/formidable/lib/incoming_form.js 1:43-50

WARNING in ./~/formidable/lib/file.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/formidable/lib/file.js 1:43-50

WARNING in ./~/formidable/lib/json_parser.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/formidable/lib/json_parser.js 1:43-50

WARNING in ./~/formidable/lib/querystring_parser.js
Critical dependencies:
1:43-50 require function is used in a way in which dependencies cannot be statically extracted
 @ ./~/formidable/lib/querystring_parser.js 1:43-50

Error Message:

Type Error ---------------------------------------------

     require is not a function
benjie commented 7 years ago

@nazmy Sorry, can't help other than to suggest you try adding the aliases too?

brandonmp commented 7 years ago

Been dealing w/ this for a few hours & no luck w/ any of the existing solutions. The problem for me is a bit diff't though: I'm using an electron boilerplate with preconfig'd babel/webpack, and one of the plugins somewhere is doing something strange here.

Basically, babel/webpack/whatever sees this line:

if (global.GENTLY) require = GENTLY.hijack(require);

And I guess considers require to need declaring, so it outputs it as:

var require;if (global.GENTLY) require = GENTLY.hijack(require);

This overwrites global.require & breaks everything else in the file.

Still trying to sort out which plugin is screwing it up, but unfortunately removing plugins one-by-one requires extensive fiddling w/ other things to make the project even start.

It seems like this one line is causing a lot of heartache, esp. since auth0 depends indirectly on this lib.

Does this line need to be required in exactly this way? If it's for testing, can't you set NODE_ENV='test' & wrap this?

tunnckoCore commented 7 years ago

Does this line need to be required in exactly this way? If it's for testing, can't you set NODE_ENV='test' & wrap this?

Yep, it's about testing I believe. There are many things that should be aligned to the current state of javascript, tooling and work(/dev)flow.

We already have opened the discussion, about converting tests to something like mocha. #415

russbeye commented 7 years ago

Just got bit by the same very thing as most in here. I see that I have some dependencies on formidable with my Koa stuff:

if (global.GENTLY) require = GENTLY.hijack(require);

Which was transforming into:

var require;if (global.GENTLY) require = GENTLY.hijack(require);

@tdeheurles and @gabrielstuff posted that you can fix this in your webpack config with:

plugins: [ new webpack.DefinePlugin({ "global.GENTLY": false }) ],

This indeed worked. Webpack then outputs:

if (false) require = GENTLY.hijack(require);

Since require then becomes unreachable, it never attempts to redefine require and overwrite the global require.

If this doesn't fix your require issues, I would suggest looking into your compiled webpack packages and search for var require; or something of the like. You want to really investigate whether your global require is being overwritten or not.

unicornexpat commented 7 years ago

plugins: [ new webpack.DefinePlugin({ "global.GENTLY": false }) ],

This doesn't make the warning go away. However, when i update the required dependency jsdom the warning disappear

kode8 commented 6 years ago

Remove target: 'node' from your webpack config

kedarv commented 5 years ago

Is a code fix possible instead of using webpack config rules to silence the issue? What about just deleting if (global.GENTLY) require = GENTLY.hijack(require); lines?

sverraest commented 4 years ago

Just want to say this issue = a whole day lost, especially because how it was obfuscated on Serverless + AWS Lambda.

tunnckoCore commented 4 years ago

@kedarv, we can just delete them, for now. It's a weird thing for the testing. We need rewrite of the whole testing anyway.

@sverraest Sorry about that, i've experienced that too... sadly, yup, that's the case for now. The v2 may come soon, or probably after the holidays... it just depends.

You all can support us - financially or by contributing (I'll push contributing.md soon) to the tests codebase after #531 is merged. We are only 2 devs pushing a lot of updates recently.

mikemaccana commented 4 years ago

Apparently this also affects rollup (I don't use webpack but my bug #538 was marked a duplicate of this)

tunnckoCore commented 4 years ago

Yea. Kind of normal with this codebase which for testing purposes is hijacking globals... and the tests are awful. But I will work on this until the end of month hopefully.

kedarv commented 4 years ago

For what it's worth, you can fork this library and remove the offending lines and force your project to use your fork until this is fixed in upstream

tunnckoCore commented 4 years ago

@kedarv PRs are welcome too. #415

danielo515 commented 4 years ago

Since which version is this fixed?

tunnckoCore commented 4 years ago

@danielo515, none yet. It's only on master. You can do

npm install node-formidable/node-formidable#master
danielo515 commented 4 years ago

Sadly I can not, this library is an indirect dependency. What I ended doing is not using the library at all. I was lucky and I was possible this time

kedarv commented 4 years ago

Why not release a patch version with the fix?

tunnckoCore commented 4 years ago

@kedarv because we can't. We did merged a ton of stuff and there's possible breaking changes.

Once we release v2, the release flow will be a lot smoother and easier.

tunnckoCore commented 4 years ago

@kedarv @joseSantacruz @danielo515 @mikemaccana @sverraest @ashgaliyev @dev101 @domaslasauskas @cc9226b77c @edorivai @frol @foxxtrot @ryb73 @catamphetamine @tdeheurles

Please try npm install formidable@canary - preview of v2. The API is almost the same, check the docs or open new issue if there's some problem.

Thanks.

patrykkrawczyk commented 4 years ago

I've created a repository with node-auth0 already bundled for anyone to use in their own project to avoid any issues with formidable and similar errors. https://github.com/patrykkrawczyk/node-auth0-bundle

Just get the node-auth0-bundle.js file from the dist directory, place it in your project and reference it with import/require statements as such:

import { ManagementClient } from './libs/node-auth0-bundle'
import { AuthenticationClient } from './libs/node-auth0-bundle'
tunnckoCore commented 4 years ago

Okay. But it's a lot better to just switch to the canary v2. There are (almost*) no breaking changes.

* except if you are adding options to the instance, which is a very bad practice anyway

winstondu commented 4 years ago

@tunnckoCore , it appears your commit to add hexoid revived a similar problem.

For all folks reading, 2.0.0-canary.20200226.1 was the last stable version.

chouch0u commented 3 years ago

"formidable": "^2.0.0-canary.20200504.1"

App threw an error during load
TypeError: hexoid is not a function

Is not a function again. Still not works.

GrosSacASac commented 3 years ago

What is the solution ?

tunnckoCore commented 3 years ago

How this is even possible?! No new version on our side, nothing touched, and plus we have pinned version in deps list...

@chouch0u try installing hexoid@1.0.0 in your app alongside formidable.

Some strange things are happening lately.

chouch0u commented 3 years ago

I tried build a clean new project only with express and formidable, it works. But when I build a electron project by Vue/Cli and Vue-electon try require formidable on main.js (or background.js ) it shows up this error:

App threw an error during load TypeError: hexoid is not a function

Must be something wrong with webpack or vue or electrons build system.

chouch0u commented 3 years ago

I tried build a clean new project only with express and formidable, it works. But when I build a electron project by Vue/Cli and Vue-electron try require formidable on main.js (or background.js ) it shows up this error:

App threw an error during load TypeError: hexoid is not a function

Must be something wrong with webpack or vue or electrons build system.

And I find the solution, Add formidable to externals on vue.config.js under the electronBuilder option. Anyone try to use formidable at vue-electron project should try this.

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      // List native deps here if they don't work
      externals: ['formidable'],
    }
  }
}
tunnckoCore commented 3 years ago

Yep, Webpack has some issue with us and can't bundle Formidable :rofl:

Cool that you find some solution! :)

ttqftech commented 3 years ago

Thanks to @chouch0u 's solution. For those who use Koa-body in a vue/cli electron apps, you can also try this:

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      // List native deps here if they don't work
      externals: ['formidable', 'koa-body'],
    }
  }
}
ilijapuaca commented 3 years ago

Is there an update on this issue and v2 in general? We've been using "formidable": "2.0.0-canary.20200226.1" as a resolution for a long time now due to this issue, and we now have a problem where one of the libs does not work with the pinned version so we are somewhat stuck figuring out a way forward 😐

GrosSacASac commented 3 years ago

@ilijapuaca Can you try again with version 3 ? It uses import/export syntax so should be better with webpack.

weaksou commented 2 years ago

for next js, the config file is different "next.config.js" you can use the following config:

module.exports = {
  webpack: (config, { isServer, webpack}) => {
    if (!isServer) {
      config.resolve.fallback = {
        fs: false, //here add the packages names and set them to false
      };
    }
    config.plugins.push(new webpack.DefinePlugin({ "global.GENTLY": false }));

    return config;
  },

};

You can add packages that you don't want to include in the client, so you don't get the error:

Module not found: Can't resolve 'fs'

tunnckoCore commented 2 years ago

@weaksou switch to v2 or v3.

weaksou commented 2 years ago

how I'm supposed to do this? I'm new to formidable 😄, thanks.

tunnckoCore commented 2 years ago

@weaksou check the VERSION_NOTES file. In short, installing formidable as usual now installs the latest v2. The v3 is on v3 dist-tag npm install formidable@v3. The npm dist-tag will change soon too but that doesn't matter that much.

tunnckoCore commented 2 years ago

Too old. Closing.

ahoys commented 2 years ago

For anyone else who is trying to investigate TypeError: hexoid is not a function and found out that this is the only thread in the entire Internet about the issue: downgrade your superagent (or similar library that uses formidable).

I went from 7 to 6 and that fixed the issue. Looking at the changelog, there's a formidable update mentioned.

jjmerri commented 2 years ago

For anyone else who is trying to investigate TypeError: hexoid is not a function and found out that this is the only thread in the entire Internet about the issue: downgrade your superagent (or similar library that uses formidable).

I went from 7 to 6 and that fixed the issue. Looking at the changelog, there's a formidable update mentioned.

I had to downgrade soap to version 0.43.0 to get this error to go away

dhruvit-r commented 2 years ago

I found another solution which doesn't require you to downgrade, but this only works if you're working with webpack. Add this to your webpack config file:

const { NormalModuleReplacementPlugin } = require('webpack');

module.exports = {
  ...
  plugins: [
    ...
    new NormalModuleReplacementPlugin(/^hexoid$/, require.resolve('hexoid/dist/index.js')),
  ]
}