Problem with faunadb

So I am installing FaunaDB. I have this function:
‘use strict’;
const faunadb = require(‘faunadb’);

/* configure faunaDB Client with our secret */
const q = faunadb.query;
const client = new faunadb.Client({
  secret: 'SECRET KEY'
});

/* create a user in FaunaDB that can connect from the browser */
function createUser(userid, name, email) {
  return client.query(
    q.Create(q.Class('users'), {
      data: {
        id: userid,
        name: name,
        email: email
      }
    })
  );
}

exports.handler = function(event, context, callback) {
  createUser(event.queryStringParameters.userid,event.queryStringParameters.name,event.queryStringParameters.email)
    .then(key =>
      callback(null, {
        statusCode: 200,
        body: 'Done'
      })
    )
    .catch(e => {
      console.error(e);
      callback(null, {
        statusCode: 500,
        body: JSON.stringify({
          error: e
        })
      });
    });
}

And I have this package.json:
{
“name”: “login-curso”,
“version”: “1”,
“dependencies”: {
“faunadb”: “^2.5.2”
}
}

However, every time I run my function I get:

{“errorType”:“Runtime.ImportModuleError”,“errorMessage”:“Error: Cannot find module ‘faunadb’\nRequire stack:\n- /var/task/register.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js”,“trace”:[“Runtime.ImportModuleError: Error: Cannot find module ‘faunadb’”,“Require stack:”,"- /var/task/register.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object. (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1156:30)"," at Object.Module._extensions…js (internal/modules/cjs/loader.js:1176:10)"," at Module.load (internal/modules/cjs/loader.js:1000:32)"," at Function.Module._load (internal/modules/cjs/loader.js:899:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)"," at internal/main/run_main_module.js:18:47"]}

FYI, I’m having the same problem:

Question: are you sure you even need the package.json? I can’t find a definitive answer even to that question. I simply added the function in a /functions directory of my site, which does not have a package.json, and it gets deployed and “works” - aside from the import problem - without it. So, it’s unclear to me whether it’s even necessary or not. Surely we’re not supposed to check in node_modules at least I’m assuming, but without something looking for packages.json (which definitely isn’t needed just for a plain HTML site) then it won’t matter, and I can’t find anything that seems to be looking for it (and then executing an npm install as part of the build process, obviously).

FYI, answered my own question: Problem with FaunaDB function- import failure

In short: yeah, package.json needs to be there, in conjunction with a build command to run npm install.

A bit more detail for you to hopefully solve the problem too…

Here’s my full package.json file:

{
  "name": "test",
  "version": "1.0.0",
  "dependencies": {
    "faunadb": "^2.13.1"
  },
  "scripts": {
    "build": "npm install"
  },
  "devDependencies": {
  }
}

Then, in Deploy Settings->Continuous Deployment->Build Settings for your site (in the Netlify app console), I added a build command “npm run build”. That did the trick.

1 Like