netlify / blobs

A TypeScript client for Netlify Blobs
https://docs.netlify.com/blobs/overview
MIT License
16 stars 5 forks source link

Using `getStore` outside a function only works in dev, not in production #175

Open rhtenhove opened 6 months ago

rhtenhove commented 6 months ago

Describe the bug

When using for example:

const blobStore = getStore("foo");

Outside a function, netlify dev is fine with it and will store and retrieve stuff as expected. However when using this in production (or a deploy preview), the following error is thrown.

{
  "errorType": "MissingBlobsEnvironmentError",
  "errorMessage": "The environment has not been configured to use Netlify Blobs. To use it manually, supply the following properties when creating a store: siteID, token",
  "trace": [
    "MissingBlobsEnvironmentError: The environment has not been configured to use Netlify Blobs. To use it manually, supply the following properties when creating a store: siteID, token",
    "    at getClientOptions (file:///var/task/node_modules/@netlify/blobs/dist/chunk-SGXOM2EY.js:298:11)",
    "    at getStore (file:///var/task/node_modules/@netlify/blobs/dist/main.js:294:27)",
    "    at file:///var/task/netlify/functions/my-cool-function.mjs:156:17",
    "    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)",
    "    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)",
    "    at async _tryAwaitImport (file:///var/runtime/index.mjs:1008:16)",
    "    at async _tryRequire (file:///var/runtime/index.mjs:1057:86)",
    "    at async _loadUserApp (file:///var/runtime/index.mjs:1081:16)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
    "    at async start (file:///var/runtime/index.mjs:1282:23)"
  ]
}

To fix this, getStore needs to happen within a function called by a request.

Configuration

 System:
    OS: Linux 5.15 Ubuntu 23.10 23.10 (Mantic Minotaur)
    CPU: (20) x64 13th Gen Intel(R) Core(TM) i7-13700H
    Memory: 3.70 GB / 7.62 GB
    Container: Yes
    Shell: 3.6.1 - /usr/bin/fish
  Binaries:
    Node: 20.13.1 - /usr/local/bin/node
    npm: 10.5.2 - /usr/local/bin/npm
hrishikesh-k commented 4 months ago

For internal tracking, this was first reported: https://linear.app/netlify/issue/FRP-985/ and has been fixed as of: https://linear.app/netlify/issue/FRP-959/.

The only difference is that, there getStore was being called during build time and here, based on the stack trace, this looks to be happening within a Function. I don't think the fix was for that part.

What's the use-case here? Any particular reason to call getStore outside of the default export?

hrishikesh-k commented 4 months ago

I came here due to the report: https://answers.netlify.com/t/107958/11