GithubHelp home page GithubHelp logo

Comments (9)

DavidWells avatar DavidWells commented on July 20, 2024 1

Ohhh right right right.

Because you are including another module from npm, we need to ship that function with it's dependancy.

You do need to bundle everything together with netlify-lambda and import statements

Or you can use the zip based function approach. Here is a demo of using zip based functions https://github.com/DavidWells/function-zips

from functions.netlify.com.

allygator avatar allygator commented on July 20, 2024

I am also having this issue. I cant get require or import to work.

from functions.netlify.com.

swyxio avatar swyxio commented on July 20, 2024

cant see the linked file, its gone now.

sample code would help.

from functions.netlify.com.

allygator avatar allygator commented on July 20, 2024

My current function is very simple.

import fetch from "node-fetch";

const endpoint = 'https://us.battle.net/oauth/token?grant_type=client_credentials&client_id='+process.env.CLIENTID+'&client_secret='+process.env.SECRET;

exports.handler = async (event, context, callback) => {
  return callback(null, {
    statusCode: 200,
    body: endpoint
    })
};

I have "node-fetch": "^2.2.0", in my dependencies in package.json, but I have also tried it in devdependencies like is used in the examples.

my exact error message is:

{
errorMessage: "Unexpected token import",
errorType: "SyntaxError",
stackTrace: [
"createScript (vm.js:80:10)",
"Object.runInThisContext (vm.js:139:10)",
"Module._compile (module.js:616:28)",
"Object.Module._extensions..js (module.js:663:10)",
"Module.load (module.js:565:32)",
"tryModuleLoad (module.js:505:12)",
"Function.Module._load (module.js:497:3)",
"Module.require (module.js:596:17)",
"require (internal/module.js:11:18)"
]
}

from functions.netlify.com.

DavidWells avatar DavidWells commented on July 20, 2024

Hey @allygator do you have another .babelrc file somewhere in your project?

I've seen project babel files conflict with the default provided in netlify-lambda

Follow up question: are you using netlify-lambda to build the functions?

from functions.netlify.com.

allygator avatar allygator commented on July 20, 2024

@DavidWells I do not have another .babelrc file in my project, and Im not using netlify-lambda. I couldnt get it working with my project, or a fork of this repository.

from functions.netlify.com.

DavidWells avatar DavidWells commented on July 20, 2024

Aha theres the problem.

You will need to bundle with webpack or https://github.com/netlify/netlify-lambda to use import statements.

Here is an example of how that works: https://github.com/netlify/netlify-faunadb-example/blob/master/package.json#L17

Alternatively, you can convert your import statement to:

const fetch = require("node-fetch");

^ this will work without any other steps (or netlify-lambda build process)

from functions.netlify.com.

allygator avatar allygator commented on July 20, 2024

I have also tried that, but I get a different error (not related to this original issue though)

{
errorMessage: "Cannot find module 'node-fetch'",
errorType: "Error",
stackTrace: [
"Function.Module._load (module.js:474:25)",
"Module.require (module.js:596:17)",
"require (internal/module.js:11:18)",
"Object.<anonymous> (/var/task/token.js:1:77)",
"Module._compile (module.js:652:30)",
"Object.Module._extensions..js (module.js:663:10)",
"Module.load (module.js:565:32)",
"tryModuleLoad (module.js:505:12)",
"Function.Module._load (module.js:497:3)"
]
}

from functions.netlify.com.

DavidWells avatar DavidWells commented on July 20, 2024

Also create react app lambda is a good example of using netlify-lambda https://github.com/netlify/create-react-app-lambda/blob/master/package.json#L16

from functions.netlify.com.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.