listParam isn't loaded successfully in the emulator
Related issues
https://github.com/firebase/firebase-functions/issues/1523
[REQUIRED] Version info
node:
v22.8.0
firebase-functions:
5.1.1
firebase-tools:
13.16.0
firebase-admin:
12.4.0
[REQUIRED] Test case
import { beforeUserCreated } from "firebase-functions/v2/identity";
import { myList } from './params.js';
export const beforecreated = beforeUserCreated((event) => {
console.log('My list', myList.value());
});
In params.js
export const myList = defineList('MY_LIST', { default: [] });
In env.local
MY_LIST=something,somethingElse
[REQUIRED] Steps to reproduce
Run a function that uses a listParam, in this case I try to create a user so the beforeUserCreated blocking function is invoked.
[REQUIRED] Expected behavior
console log shows "My list [something, somethingElse]"
[REQUIRED] Actual behavior
> {"severity":"ERROR","message":"Unhandled error SyntaxError: Unexpected token 's', \"something,somethingElse\" is not valid JSON\n at JSON.parse (<anonymous>)\n at ListParam.runtimeValue (/Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/params/types.js:378:26)\n at ListParam.value (/Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/params/types.js:39:21)\n at file:///Users/tzappia/workspace/hrvst/functions/lib/triggers/users.js:50:49\n at wrappedHandler (/Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/v2/providers/identity.js:58:39)\n at /Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/common/providers/identity.js:458:28\n at /Users/tzappia/workspace/hrvst/functions/node_modules/firebase-functions/lib/common/onInit.js:33:16\n at process.processTicksAndRejections (node:internal/process/task_queues:105:5)\n at async runFunction (/Users/tzappia/.nvm/versions/node/v22.8.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:506:9)\n at async runHTTPS (/Users/tzappia/.nvm/versions/node/v22.8.0/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:531:5)"}
Were you able to successfully deploy your functions?
I was able to successfully deploy and the listParam seems to work fine in a production environment. Is the emulator parsing the .env file differently in the emulator vs. production? Seems to be a similar behaviour in the linked bug report that was closed.
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Hello @tzappia. Could you try wrapping the value in [] and trying again?. So MY_LIST=something,somethingElse will become MY_LIST=[something,somethingElse]
@exaby73 that works in the emulator, but I'm not willing to try it in production (don't want to break my live deployment!) As noted in the related issues, I expect it to fail in production.
I did some testing.
For the emulator to work with defineList the .env file must have:
- Entire list wrapped in brackets.
- Each word wrapped in double quotes. Single quotes do not work.
- Separated by a comma. Using a space between each list item has does not effect the outcome.
["oranges and pears", "apples"](with a space between list items) and["oranges and pears","apples"](without a space between list items) both work the same.
Like this:
EXAMPLE_LIST=["oranges and pears", "apples"]
But this style does not work in production.
It creates one list item of ["oranges and pears", and another list item of "apples"] which is obviously wrong. It seems to be splitting at the , and including the brackets and quotes and the spaces between each list item.
To make it work in production we need this in the .env file:
EXAMPLE_LIST=oranges and pears,apples
But as the original issue has raised, this format does not work with the emulator.
Any updates on this? I'm still plagued by this problem and have to maintain two env variables and switch back and forth when using emulator vs prod
Hi @tzappia
Thanks for reporting this issue!
I've reproduced this and will be investigating this further.
Although not ideal, a temporary workaround could be implemented.
-
Use the format of
["A", "B", "C"] -
Introduce this code
let parsedList: any[] = []
if (ENV === "production") {
const rawList = myList.value().toString();
const trimmed = rawList.trim();
const inner = trimmed.slice(1, -1);
parsedList = inner
.split(",")
.map(item => item.trim())
.filter(item => item.length > 0);
} else {
parsedList = myList.value();
}