My app:
tender-engelbart-2ed7d8.netlify.app
Started off as a static page deployed with git (just HTML, CSS and JS), no pakage.json and no build step. No problems there.
I am attempting to add a lambda function (To fetch an OAuth token). This function has a dependency ‘node-fetch’.
To do so I restructured my project:
- moved the static files out of the root into ./client (these files do not need a build)
- added a empty functions directory (ready to contain a built function file for deploy)
- a src/functions/ with my function source
- a package.json to define a build script and dependencies (for my function)
Like so:
client/ (static files)
emoji.js
index.html
index.js
functions/
fetchToken.js (built function)
netlfiy.toml
package.json
src/functions/
fetchToken.js (function source)
My intent is to use netlify-lambda to pre-compile my function to a standalone file locally. Then deploy the files with git as before.
netlify.toml:
[build]
command = "npm run build"
publish = "client/"
functions = "functions/"
package.json:
{
"name": "fetch-token",
"version": "1.0.0",
"description": "",
"main": "functions/fetchToken.js",
"scripts": {
"build": "netlify-lambda build src/functions",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"netlify-lambda": "^1.6.3",
"node-fetch": "^2.6.0"
}
}
Running the build appears to work (no errors thrown and /function/fetchToken.js is written to disk):
Terminal output:
❯ npm run build
> fetch-token@1.0.0 build /Users/ollie/Repos/slack-status
> netlify-lambda build src/functions
netlify-lambda: Building functions
Hash: c777c9d03b51b2238e3b
Version: webpack 4.42.1
Time: 852ms
Built at: 04/26/2020 5:03:12 PM
Asset Size Chunks Chunk Names
fetchToken.js 18.6 KiB 0 [emitted] fetchToken
Entrypoint fetchToken = fetchToken.js
[0] external "stream" 42 bytes {0} [built]
[1] external "zlib" 42 bytes {0} [built]
[2] external "url" 42 bytes {0} [built]
[3] external "http" 42 bytes {0} [built]
[4] external "https" 42 bytes {0} [built]
[5] ./fetchToken.js 978 bytes {0} [built]
[6] /Users/ollie/Repos/slack-status/node_modules/node-fetch/lib/index.mjs 39.9 KiB {0} [built]
Once deployed to git, when I call the function, it complains:
{
"errorMessage": "Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task/fetchToken.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"errorType": "Runtime.ImportModuleError",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module 'node-fetch'",
"Require stack:",
"- /var/task/fetchToken.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1158:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)",
" at Module.load (internal/modules/cjs/loader.js:1002:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:901:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
" at internal/main/run_main_module.js:18:47"
]
}
Where is the gap in my understanding? Any pointers are very welcome!