Netlfiy functions and groq-js

Hi,

for some reason i am unable to deploy a fairly simple function to netlify… it runs on my machine :smiley: but does not work when deployed to netlify.

const groq = require('groq-js');

// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
exports.handler = async (event, context) => {
  try {
    // const subject = event.queryStringParameters.name || 'World';


    const input = '*[_type == "user"]{name}';

    // Returns an ESTree-inspired syntax tree
    const tree = groq.parse(input);

    const dataset = [
      { _type: 'user', name: 'Michael' },
      { _type: 'company', name: 'Bluth Company' },
    ];

    // Evaluate a tree against a dataset
    const value = await groq.evaluate(tree, { dataset });

    // Gather everything into one JavaScript object
    const result = await value.get();

    return {
      statusCode: 200,
      body: JSON.stringify(result),
    };
  } catch (err) {
    return { statusCode: 500, body: err.toString() };
  }
};

do you have any idea why? the following error was thrown when the function was called

{"errorMessage":"Unexpected token *","errorType":"SyntaxError","stackTrace":["SyntaxError: Unexpected token *","createScript (vm.js:80:10)","Object.runInThisContext (vm.js:139:10)","Module._compile (module.js:616:28)","Object.Module._extensions..js (module.js:663:10)","Module.load (module.js:565:32)","tryModuleLoad (module.js:505:12)","Function.Module._load (module.js:497:3)","Module.require (module.js:596:17)","require (internal/module.js:11:18)","Object.<anonymous>(/var/task/src/node_modules/groq-js/src/index.js:6:20)"]}
expected result:

[{"name":"Michael"}]

i find it rather difficult to debug netlify functions and was hoping netlify dev would at least mirror the above behaviour, which it does not…

Hi, @Kevin_Forster, and welcome to our Netlify community site.

What version of node are you using in the local development environment? You can set the node version for your Function using the environment variable AWS_LAMBDA_JS_RUNTIME as found in our documentation here.

If you set the node runtimes to the same version, does the error still occur? Also, is there package.json file in the directory for this function which contains all the dependencies it will need?

thanks for the hint… looks like functions do not care about the node version set in .nvmrc… i just set the AWS_LAMBDA_JS_RUNTIME within netlify.toml and reployed the functions… works like a charm…

thank you

1 Like