netlify / netlify-lambda

Helps building and serving lambda functions locally and in CI environments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Function invocation failed: TypeError: n is not a function

TylerBarnes opened this issue · comments

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

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

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?

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.

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 })
  ],
}

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

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.

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?

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

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?

@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.

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

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

@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