Question

CLI serverless function deployment and running issue

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;

Submit an answer
ο»Ώ

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!

Sign In or Sign Up to Answer

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.

Bobby Iliev
Site Moderator
Site Moderator badge
β€’ June 16, 2024

Hi there,

I think that you are missing a build.sh file in your lib 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 a build.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:

https://docs.digitalocean.com/products/functions/reference/build-process/#the-build-sequence

Let me know how it goes!

- Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow β€” whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.