https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • j

    jorjik

    05/18/2023, 6:58 PM
    when I run
    wrangler dev
    it setup the server and function are working, the syntax are different for the functions - for workers and for page functions?
  • s

    Skye

    05/18/2023, 6:59 PM
    Yes, I'd recommend you read https://developers.cloudflare.com/pages/platform/functions
  • s

    Skye

    05/18/2023, 7:00 PM
    If you don't want to change your syntax, you can instead put a
    _worker.js
    file in _site and use the same syntax as you're using now
  • j

    jorjik

    05/18/2023, 7:07 PM
    Thank you @Skye
  • h

    hanpolo

    05/20/2023, 12:09 AM
    I'm trying to
    import { randomBytes } from 'crypto'
    in my local pages dev. My command line is:
    Copy code
    npx wrangler pages dev \
      --compatibility-flags="nodejs_compat" --experimental-local \
      --kv=SOMETHING --kv=TEST --kv=CONFIG \
      --do TEST_DO=TestDO@test-do-dev \
      --d1=TEST_DB --persist \
      -- npm start
    However, the build fails with message:
    Copy code
    The package "crypto" wasn't found on the file system but is built into node.
      Add the "nodejs_compat" compatibility flag to your Pages project to enable Node.js compatibility.
    Any ideas what I'm doing wrong?
  • w

    Walshy | Pages

    05/20/2023, 12:17 AM
  • w

    Walshy | Pages

    05/20/2023, 12:17 AM
    crypto isn't supported yet
  • h

    hanpolo

    05/20/2023, 12:18 AM
    Yep .. thanks .. just figured that out. New to crypto. Can I simply npm install it? I seem to get errors if I try to do so.
  • s

    Skye

    05/20/2023, 12:19 AM
    You'd probably want something like
  • w

    Walshy | Pages

    05/20/2023, 12:20 AM
    ^ Web Crypto likely has equivalent APIs
  • s

    Skye

    05/20/2023, 12:20 AM
    No npm or node modules needed
  • h

    hanpolo

    05/20/2023, 12:21 AM
    pardon the silly question, but is Web Crypto included in Node.js that is used in Pages Functions?
  • h

    hanpolo

    05/20/2023, 12:21 AM
    I guess it's built into Javascript?
  • w

    Walshy | Pages

    05/20/2023, 12:25 AM
    Web Crypto is a web standard that is in the v8 runtime (and other js engines)
  • w

    Walshy | Pages

    05/20/2023, 12:25 AM
    Functions/Workers is built on v8 and web standards
  • w

    Walshy | Pages

    05/20/2023, 12:25 AM
    It isn't running Node.js (hence the new node compat - and why it's so quick)
  • h

    hanpolo

    05/20/2023, 12:26 AM
    Thanks much .. I see that it's integrated. I guess I still need --nodejs-compat to use Buffer.
  • w

    Walshy | Pages

    05/20/2023, 12:27 AM
    yep, Buffer will require node compat
  • h

    hanpolo

    05/20/2023, 12:31 AM
    Thanks much everyone .. much appreciated.
  • m

    modichutiyathaaurhai

    05/20/2023, 9:59 AM
    how to debug pages functions, local deploy doesn't work
  • i

    Isaac McFadyen | YYZ01

    05/20/2023, 1:00 PM
    Can you try
    wrangler pages dev
    ?
  • i

    Isaac McFadyen | YYZ01

    05/20/2023, 1:01 PM
    And then pass in your folder of assets.
  • i

    Isaac McFadyen | YYZ01

    05/20/2023, 1:01 PM
    wrangler pages dev ./build
    or whatever your folder is called
  • i

    izdi

    05/21/2023, 6:32 AM
    Hi everyone, I'm using next.js app to build frontend, and
    functions
    to build backend, however I stuck with lack of knowledge on how to enable
    npm
    modules. I see that workers has this capabilities using wrangler cli out of the box, is there any guide/example for functions? Thanks 🗿
  • s

    Skye

    05/21/2023, 9:35 AM
    They should work exactly the same for functions, just install it normally, and import them in your code
  • i

    izdi

    05/21/2023, 8:10 PM
    Sorry, I framed it wrong, the problem is when I deploy to Cloudflare Pages the build fails with an error
    Copy code
    22:05:01.912    ./workers/src/index.ts:29:47
    22:05:01.912    Type error: Cannot find name 'ExecutionContext'.
    22:05:01.912    
    22:05:01.912      27 | 
    22:05:01.912      28 | export default {
    22:05:01.912    > 29 |     async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<any> {
    22:05:01.912         |                                                  ^
    22:05:01.913      30 |         if (env.router === undefined) {
    22:05:01.913      31 |             env.router = buildRouter(env);
    22:05:01.913      32 |         }
    In this example it is related to workers, but prior to that I had the same issues with
    functions
    in the root folder of the next.js project.
  • i

    izdi

    05/21/2023, 8:26 PM
    nevermind, I excluded workers folder at tsconfig
  • n

    nora

    05/21/2023, 8:26 PM
    shouldn’t you be compiling
  • n

    nora

    05/21/2023, 8:26 PM
    oh that is the compilation
  • i

    izdi

    05/21/2023, 8:37 PM
    ok, it helped to exclude workers from the build but I reverted code for my
    functions/api/inquiry.js
    endpoint to use Notion SDK
    import { Client } from "@notionhq/client"
    , so when I added the external package I got now another failed build with this statement
    Copy code
    22:30:18.149    Found Functions directory at /functions. Uploading.
    22:30:18.882    ✨ Compiled Worker successfully
    22:30:18.943    Validating asset output directory
    22:30:19.550    Deploying your site to Cloudflare's global network...
    22:30:22.711    Uploading... (72/79)
    22:30:23.178    Uploading... (74/79)
    22:30:23.454    Uploading... (76/79)
    22:30:23.846    Uploading... (79/79)
    22:30:23.846    ✨ Success! Uploaded 7 files (72 already uploaded) (1.62 sec)
    22:30:23.846    
    22:30:24.343    ✨ Upload complete!
    22:30:25.957    Success: Assets published!
    22:30:26.821    Error: Failed to publish your Function. Got error: Uncaught ReferenceError: global is not defined
      at functionsWorker-0.19086037171855352.js:477:49 in ../node_modules/node-fetch/browser.js
      at functionsWorker-0.19086037171855352.js:11:50 in __require
      at functionsWorker-0.19086037171855352.js:584:24 in ../node_modules/@notionhq/client/build/src/Client.js
      at functionsWorker-0.19086037171855352.js:11:50 in __require
      at functionsWorker-0.19086037171855352.js:932:20 in ../node_modules/@notionhq/client/build/src/index.js
      at functionsWorker-0.19086037171855352.js:11:50 in __require
      at functionsWorker-0.19086037171855352.js:1128:29 in api/inquiry.js
      at functionsWorker-0.19086037171855352.js:8:56 in __init
      at functionsWorker-0.19086037171855352.js:1136:5 in ../../../../tmp/functionsRoutes-0.015377362220493573.mjs
      at functionsWorker-0.19086037171855352.js:8:56 in __init
    Are there some specifics I'm missing on how to import packages in functions or setup with next.js?
1...387388389...392Latest