Hello community π
Iβm new to the DO and right now interested particularly in scheduled functions
to automate my processes.
Using this documentation https://docs.digitalocean.com/products/functions/how-to/create-functions/ I created serverless function within typescript
template.
My main issue While I was working within 1 file function by testing different triggers and deployments - everything was fine. When I add additional folders and helper functions - it stops running.
Function logs
{
"namespace": "fn-a0450dcf-a2f0-4ce8-ae58-63b3e2fd2f6c",
"name": "autopet",
"version": "0.0.36",
"publish": false,
"annotations": [
{
"key": "path",
"value": "fn-a0450dcf-a2f0-4ce8-ae58-63b3e2fd2f6c/fireball/autopet"
},
{
"key": "waitTime",
"value": 47
},
{
"key": "uuid",
"value": "b0ec5d8b-de9f-44eb-b666-542f51b3b179"
},
{
"key": "entry",
"value": "lib/autopet.js"
},
{
"key": "user_id",
"value": "16702566"
},
{
"key": "gbs",
"value": 0
},
{
"key": "kind",
"value": "nodejs:14"
},
{
"key": "timeout",
"value": false
},
{
"key": "limits",
"value": {
"logs": 16,
"memory": 256,
"timeout": 3000
}
},
{
"key": "initTime",
"value": 2409
}
],
"subject": "b0ec5d8b-de9f-44eb-b666-542f51b3b179",
"activationId": "b5d27fa6e0854d89927fa6e085ed89e4",
"end": "1717980782028",
"start": "1717980782028",
"duration": 0,
"response": {
"result": {
"error": "The function did not initialize properly."
},
"size": 53,
"status": "developer error",
"success": false
},
"logs": [
"2024-06-10T00:53:04.391904024Z stdout: Function entrypoint 'lib/autopet.js' is not a function."
],
"statusCode": null
}
Structure
packages/fireball/autopet/
βββ src/
β βββ autopet.ts
β βββ shared/
β βββ constants.ts
β βββ index.ts
βββ lib/
β βββ autopet.js
β βββ shared/
β βββ constants.js
β βββ index.js
βββ package.json
βββ tsconfig.json
project.yml
packages:
- name: fireball
functions:
- name: autopet
binary: false
main: 'lib/autopet.js'
runtime: nodejs:default
web: true
webSecure: false
tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"module": "commonjs",
"outDir": "lib",
"rootDir": "src",
"target": "es2019",
"types": ["node"],
"resolveJsonModule": true,
"skipLibCheck": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
package.json
{
"main": "lib/autopet.js",
"devDependencies": {
"@types/node": "^20.14.2",
"dotenv": "^16.4.5",
"prettier": "^3.3.1",
"typescript": "5.1"
},
"scripts": {
"build": "tsc -b"
},
"dependencies": {
"axios": "^1.7.2",
"discord.js": "^14.15.3",
"ethers": "^6.13.0"
},
"engines": {
"node": ">=16.0.0"
}
}
autopet.ts
import { POLYGON_RPC } from './shared';
export function main(_args_: {}): {} {
let name: _string_ = _args_['name'] || 'stranger';
let greeting: _string_ = `Hello ${name}, here's rpc url: ${POLYGON_RPC}`;
console.log(greeting);
return { body: greeting };
}
lib/autopet.js
"use strict";
_Object_.defineProperty(_exports_, "__esModule", { value: true });
_exports_.main = void 0;
const shared_1 = require("./shared");
function main(_args_) {
let name = _args_['name'] || 'stranger';
let greeting = `Hello ${name}, here's rpc url: ${shared_1.POLYGON_RPC}`;
console.log(greeting);
return { body: greeting };
}
_exports_.main = main;
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Hi there,
I think that you are missing a
build.sh
file in yourlib
directory.The contents of the
lib
folder and the contents of each functionβs directory affect what is included in each deployed function and how that function is built.Here is also an example project that might help:
https://github.com/digitalocean/sample-functions-todo/tree/main
The structure is more or less just the
build.sh
file and your library.Here is also another example on how this is implemented:
https://github.com/digitalocean/functions-deployer/tree/main/e2e/use-lib/lib
The above is from an end-to-end test and includes a simple use of the
lib
directory and abuild.sh
file.If your serverless function repository is public, feel free to share it here and I will be happy to take a further look at why the builds might be failing.
Here is also a link to the official documentation on how the build sequence works:
Let me know how it goes!
- Bobby