Comments (9)
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.
I am also having this issue. I cant get require or import to work.
from functions.netlify.com.
cant see the linked file, its gone now.
sample code would help.
from functions.netlify.com.
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.
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.
@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.
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.
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.
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)
- [Proposal] Function example for Go MongoDB CRUD that can be locally tested
- fdfdf
- Title
- [Proposal] New functions example
- [security] Enforce Branch Protections
- Function tutorial: Building a Video Chat App with Vuejs and Golang
- questions
- Function example:
- Function tutorial:
- netlify config
- web
- auth
- Function tutorial:
- Update legacy explorers.netlify.com link urls found on functions.netlify.com HOT 1
- @jasti99 we've closed this issue as it was lacking a proper description and a referenced issue. Furthermore it seems that your changelog is los in the void of rebase/merge issues.
- Update the main website page
- Vhk
- Add side nav link to Serverless Functions Jamstack Explorers course HOT 2
- Double opt-in newsletters subscriptions with Netlify + Airtable + Vero
- mich
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from functions.netlify.com.