netlify / netlify-faunadb-example

Using FaunaDB with netlify functions
https://www.netlify.com/blog/2018/07/09/building-serverless-crud-apps-with-netlify-functions--faunadb/
386 stars 121 forks source link

Error: Cannot find module 'faunadb' #3

Open AndrewZamora opened 5 years ago

AndrewZamora commented 5 years ago

Thank you for the tutorial.

I got stuck when I ran npm run bootstrap. This error showed up in the terminal:

> node ./scripts/bootstrap-fauna-database.js

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'faunadb'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\Andrew\Documents\webDev\netlify-db-example\scripts\bootstrap-fauna-database.js:3:17)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! netlify-db-example@0.1.0 bootstrap: `node ./scripts/bootstrap-fauna-database.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the netlify-db-example@0.1.0 bootstrap script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Is there something I overlooked in the tutorial?

swyxio commented 5 years ago

did you run npm install? is there a faunadb folder in your node_modules?

AndrewZamora commented 5 years ago

@sw-yx Thank you for your reply. I didn't clone the repository. I just followed the tutorial. I have not npm installed faunadb. For the tutorial am I suppose to clone and run the repo locally first? tutorial_question

swyxio commented 5 years ago

ohh i see. yes, scroll up and start from "Setup & Run Locally"

AndrewZamora commented 5 years ago

@sw-yx Okay, thank you for your time!

tinymachine commented 5 years ago

I really appreciate your putting this tutorial together @DavidWells and @sw-yx, but, FWIW, I'm finding it confusing as well. Starting at the Tutorial header in the readme, it looks like it's walking you step by step through the setup process (and assuming you have not already cloned the repo). I think this mostly works, except it should also include a step to run npm install faunadb --save before running npm run bootstrap.

swyxio commented 5 years ago

ok. added

tinymachine commented 5 years ago

Thanks, @sw-yx; I noticed, though, that the tutorial already includes a separate step where you install faunadb (substep 4 under "3. Create a function > Setting up functions for local development").

A couple of additional concerns:

  1. Again under "3. Create a function > Setting up functions for local development", where it says "Your package.json should look like...", that doesn't match the current package.json in this repo: there's no proxy field in the actual pkg, but there is in the readme version. I'm confused about how the repo proxies the locally served functions, and it's weird to have this disconnect.

  2. In step 4 of the tutorial ("4. Connect the function to the frontend app"), it's not indicated where you should put that front-end code (though there is a reference to the /src folder.)

  3. When I clone the repo, install, and run, the app seems to be working, but I'm seeing warnings in my console when I do CRUD operations:

Request from ::ffff:127.0.0.1: POST /todos-create
Function `todo-create` invoked { title: 'Complete tutorial', completed: false }
success { ref: Ref(Class("todos"), "232907278267712002"),
  ts: 1558376539460000,
  data: { title: 'Complete tutorial', completed: false } }
Response with status 200 in 358 ms.
(node:46618) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'statusCode' of undefined
    at callback ([...]/netlify-faunadb-example/node_modules/netlify-lambda/lib/serve.js:35:42)
    at [...]/netlify-faunadb-example/node_modules/netlify-lambda/lib/serve.js:67:7
    at [...]/netlify-faunadb-example/functions-build/todos-create.js:20:2972
    at T ([...]/netlify-faunadb-example/functions-build/todos-create.js:20:3006)
    at A ([...]/netlify-faunadb-example/functions-build/todos-create.js:20:2827)
    at m ([...]/netlify-faunadb-example/functions-build/todos-create.js:20:728)
    at process._tickCallback (internal/process/next_tick.js:61:11)
(node:46618) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)

I'm running node v10.14.1. FWIW, the version I deployed directly to Netlify does not show these same warnings in the Functions logs, e.g.:

3:55:26 PM: todos-create invoked
3:55:26 PM: Function `todo-create` invoked { title: 'Check if there are errors in direct-to-Netlify version',
  completed: false }
3:55:27 PM: success { ref: Ref(Class("todos"), "232924462000898562"),
  ts: 1558392927140000,
  data: 
   { title: 'Check if there are errors in direct-to-Netlify version',
     completed: false } }
  1. Even though I got the cloned repo installed and working, and the deployed-directly-to-Netlify ("TLDR") version working, the Fauna dashboard is still showing the "Create your first database" message, and there doesn't appear to be any way for me to access the created db through Fauna's admin interface. The data is being stored to some db in Fauna, because my created to-dos are persisting across sessions, etc. Not sure if this is a bug with Fauna?

    Screen Shot 2019-05-20 at 3 25 40 PM

I am grateful for the effort put forth in writing this tutorial, but for me at least there's a lot that's preventing this from being as easy to follow as I think was intended.

swyxio commented 5 years ago

re: 4 there is no bug, you just need to claim your database created using the secrets given in your environment variables (you can run netlify dev:exec echo $FAUNADB_SERVER_SECRET or something like that) creating via addon just sets you up as an anonymous user for now, we don’t have a database claims process yet with fauna but we definitely need to (cc @davidwells). in the meantime you can contact fauna support to do it.

re the rest, these are probably tutorial updates we need to make. i am going to reopen this issue to keep it in mind.

tinymachine commented 5 years ago

Thanks so much for replying so quickly, @sw-yx, and for explaining the claiming issue. (As you probably know, the readme doesn't mention this, and actually it specifically says that you should see the db created in your FaunaDB account.)

I look forward to the updated version of this tutorial -- thanks again for your help.

tinymachine commented 5 years ago

Posting this here in case it's helpful: I contacted Fauna support and here's what they said about claiming the bootstrapped DB created in this tutorial:

...the database that you create in netlify-faunadb is under netlify tenancy. Since you have key, you can access it through any driver or fauna shell.

Here are the steps

  1. Install Fauna Shell - https://docs.fauna.com/fauna/current/quickstart

  2. Update your key - Follow step 8 in https://docs.fauna.com/fauna/current/quickstart-github

Please note that You would still not be able to see these under Fauna Dashboard.

So, according to this support person, even after you claim the db, there's no way to have it appear in the dashboard:

Unfortunately at this point there is no way to [add it to the dashboard]. However, we have it on out product road map to integrate all these and have a seamless user experience.

tinymachine commented 5 years ago

Again, in case it's helpful to others, I was able to connect the todo app to a Fauna database that can be viewed from Fauna's dashboard.

To do so, I followed these steps:

  1. Create a new database in the FaunaDB dashboard by clicking the "New Database" button.
  2. Somewhat counterintuitively, return to the Console Home and click "Manage Keys" (instead of accessing the "Keys" section from within the database view, which only lets you generate keys for child databases).
  3. Click "New Child DB Key", then create a new key for your new database.
  4. Copy the generated secret key, then use that key when Netlify prompts you for it after clicking the Deploy to Netlify button.

Now when you create to-dos in the deployed app, you can view them from your database view in the Fauna dashboard.

swyxio commented 5 years ago

we do need to make this claiming process easier. working on it with Fauna.

tinymachine commented 5 years ago

Hi @sw-yx -- for what it's worth, for my needs, manually creating the db in the Fauna console is totally fine; the programmatic bootstrapping process isn't necessary for me. Out of curiosity, what is the use-case for programmatic bootstrapping? Is the idea that an app could set up separate databases for each registered user or something? (That seems unlikely...) Thanks for any details you can provide, and thanks again for sharing this repo!

swyxio commented 5 years ago

the idea is simply to do as much as possible with one command! :) so for example in netlify functions:create we let you scaffold out a function that interacts with a faunadb backend, AND set up a fauna instance for you all in one go. https://github.com/netlify/netlify-dev-plugin/tree/master/src/functions-templates/js/fauna-graphql

tinymachine commented 5 years ago

Thanks for explaining! (I was missing that bootstrapping includes actually creating the indexes and schema, etc. -- not just programmatically creating the db itself. I'm new to all this!)