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

Function invocation failed: TypeError: n is not a function #64

Closed
TylerBarnes opened this issue Oct 12, 2018 · 13 comments
Closed

Function invocation failed: TypeError: n is not a function #64

TylerBarnes opened this issue Oct 12, 2018 · 13 comments

Comments

@TylerBarnes
Copy link

Hi there,
I'm attempting to use a lambda function to use the node wpapi library
https://github.com/WP-API/node-wpapi

For some reason when I try to instantiate a new instance of their WPAPI class I get the error Function invocation failed: TypeError: n is not a function

I don't believe this is a problem with netlify-lambda but I'm wondering if someone could give me some guidance on debugging this. Is there something I need to add to my config to make it run? Or is there some way for me to figure out why n is not a function? The error comes from the minified code so it's hard to understand what's happening

@TylerBarnes
Copy link
Author

Ok, I played around with this some more and actually if I run serve and then just copy paste my JS into the js file that's being served, it works fine. It seems like this might be a problem with the way netlify-lambda is minifying my script

@i8ramin
Copy link

i8ramin commented Oct 14, 2018

I'm so getting the same error, but due to other reasons. For me it happens when I require the auth0-node Management Client code.

Is there a way to turn off the js minification so we can debug these issues better?

@i8ramin
Copy link

i8ramin commented Oct 14, 2018

FYI, I was able to disable the uglification of the functions locally by passing in my own webpack config like so:

Create a webpack overrides file (mine lives in ./config/webpack.functions.js)

module.exports = {
  mode: "development"
}

Start the local server by passing in my local config

netlify-lambda -c ./config/webpack.functions.js serve src/functions

I can see the non-uglified version of the code now. For me, the error is:

Error during invocation:  TypeError: require is not a function
    at Object.../../node_modules/formidable/lib/incoming_form.js (/Users/ramin/Work/wework/labs/labs-ui/functions/pr-merge.js:41728:14)

And the corresponding line of code:

var crypto = require('crypto');

Looking through the rest of the generated file, it seems that some parts of the code are not being processed by babel, so there are regular require calls, whereas the rest of the code is using __webpack_require__.

Gotta look into the auth0-node files and its dependencies to see why this is happening.

@i8ramin
Copy link

i8ramin commented Oct 14, 2018

A bit more context on my specific issue. Noticed that require is being re-defined right above the line where it errors out.

/***/ "../../node_modules/formidable/lib/incoming_form.js":
/*!******************************************************************************************!*\
  !*** /Users/ramin/Work/wework/labs/labs-ui/node_modules/formidable/lib/incoming_form.js ***!
  \******************************************************************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

var require;if (global.GENTLY) require = GENTLY.hijack(__webpack_require__("../../node_modules/formidable/lib sync recursive"));

var crypto = require('crypto');

UPDATE

These are littered all over the node-formadible codebase. It is trying to "hijack" require, but the way it is written, babel is assigning it to a local var first.

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

https://github.com/felixge/node-formidable/blob/master/lib/incoming_form.js#L1

UPDATE 2

Found this great thread that has a workaround:
node-formidable/formidable#337 (comment)

my new webpack.functions.js override config looks like this now:

const webpack = require('webpack')

module.exports = {
  mode: "development",
  plugins: [
    new webpack.DefinePlugin({ "global.GENTLY": false })
  ],
}

@TylerBarnes
Copy link
Author

Awesome, I think this will help me troubleshoot as well! Thanks for sharing

@swyxio
Copy link
Contributor

swyxio commented Oct 17, 2018

hey both,

sorry for the delayed response and thanks for sharing. if theres anything that you can contribute upstream to babel too, that would probably be good as minification shouldnt ever cause bugs like this :( but i am glad you figured it out.

@sekoyo
Copy link

sekoyo commented Apr 5, 2019

Thanks this is happening with faunadb too and global.GENTLY fixed it. I wonder if they really broke their modules for the sake of unit test mocks or if it is something to do with this webpack config?

@swyxio
Copy link
Contributor

swyxio commented Apr 8, 2019

@dominictobias we are releasing something this week which may help with this. looking forward to see if it solves your issue.

@connor11528
Copy link

connor11528 commented Apr 25, 2019

I'm getting the same error as above. When I fire up my lambda functions to listen locally I get a warning:

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

Then when I triggered my function get the same error as OP

Error during invocation:  TypeError: n is not a function

I tried adding the webpack plugin like @i8ramin mentioned. I'm using Gatsby so added to gatsby-node.js file like so:

exports.onCreateWebpackConfig = ({ stage, actions, plugins }) => {
  actions.setWebpackConfig({
    plugins: [
      plugins.define({
        'global.GENTLY': false
      })
    ]
  })
}

But it did not help. Still getting both of the above errors. Is there a better way to add the above webpack config into Gatsby or fix these errors?

@TylerBarnes
Copy link
Author

@connor11528 check out Netlify Dev!

https://www.youtube.com/watch?v=RL_gtVZ_79Q

As far as I can tell it either makes this repo obsolete or much less necessary.

@connor11528
Copy link

@TylerBarnes oh gnar will have to look into it. Thanks!

@henrilhos
Copy link

@connor11528 do you solved that? I'm with the same problem here and I'm can't figure out how to solve this...

@swyxio
Copy link
Contributor

swyxio commented Jul 22, 2019

@castilh0s give Netlify Dev a try. thats the solution i suggested further up. gonna lock this for now, pls open new issue with repro details if you need to pursue further

@netlify netlify locked as resolved and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants