"Unexpected token import" in lambda
I'm trying to write me a lambda that uses node-fetch similar to the hello_fetch example, but I'm gettign an unexpected token error.

It works when I deploy a copy of this repository and then change the existing lambdas, but when I try to do it in my own site (with the exact same code) it gives me this error. Help would be appreciated
File in question: name_from_id.js
I am also having this issue. I cant get require or import to work.
cant see the linked file, its gone now.
sample code would help.
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)"
]
}
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?
@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.
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)
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)"
]
}
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
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