Robert
06/23/2022, 2:54 PMDigestFetch
and it requires node-fetch
When I do a build, ESBuild gives me the error
ERROR: You can mark the path "node-fetch" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
I can mark it as externalModules
in the bundle to fix the build error, but then I have a runtime error that the library is not present 😕
Back in the days, when using serverless, I could do something like this. Anyone have any idea how I can do something equivalent when creating my stack?
bundle:
packager: npm
forceInclude:
- node-fetch
externals:
- node-fetch
thdxr
06/23/2022, 2:54 PMRobert
06/23/2022, 2:54 PMError: There was a problem transpiling the Lambda handler: ✘ [ERROR] Could not resolve "node-fetch"
node_modules/digest-fetch/digest-fetch-src.js:8:68:
8 │ ...ch) !== 'function' && canRequire) var fetch = require('node-fetch')
╵ ~~~~~~~~~~~~
You can mark the path "node-fetch" as external to exclude it from the bundle, which will remove this error. You can also surround this "require" call with a try/catch block to handle this failure at run-time instead of bundle-time.
at Object.bundle (file:///tmp/seed/source/node_modules/@serverless-stack/core/dist/runtime/handler/node.js:161:23)
at Module.bundle (file:///tmp/seed/source/node_modules/@serverless-stack/core/dist/runtime/handler/index.js:16:16)
at new Function (file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/Function.js:223:45)
at Function.fromDefinition (file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/Function.js:439:20)
at ApiGatewayV1Api.addRoute (file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/ApiGatewayV1Api.js:612:27)
at file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/ApiGatewayV1Api.js:116:29
at Array.forEach (<anonymous>)
at ApiGatewayV1Api.addRoutes (file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/ApiGatewayV1Api.js:114:29)
at EmptyStack.VersionStack (file:///tmp/seed/source/.build/lib/index.js:157:14)
at stack (file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/FunctionalStack.js:15:35)
at App.stack (file:///tmp/seed/source/node_modules/@serverless-stack/resources/dist/App.js:284:16)
at Module.default (file:///tmp/seed/source/.build/lib/index.js:261:7)
at file:///tmp/seed/source/.build/run.mjs:92:16
Notices refreshed
There was an error synthesizing your app.
thdxr
06/23/2022, 2:55 PMnode-fetch
as a dev dep when it's needed. You should be able to fix this problem by adding node-fetch yourselfRobert
06/23/2022, 2:55 PMRobert
06/23/2022, 3:00 PMf351736a-5770-46e1-a6bd-6250857ad1b1 ERROR Runtime.UnhandledPromiseRejection: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/robert/Documents/repos/myapi/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /home/robert/Documents/repos/myapi/node_modules/node-fetch/src/index.js from /home/robert/Documents/repos/myapi/.sst/artifacts/debug-treesoflives-fdm-data-migration-api-VersionStack-Lambda_GET_-versions/src/handlers/version.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /home/robert/Documents/repos/myapi/node_modules/node-fetch/package.json.
Robert
06/23/2022, 3:02 PMRobert
06/23/2022, 3:02 PMRobert
06/23/2022, 5:55 PM{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'node-fetch'\nRequire stack:\n- /var/task/src/handlers/version.js\n- /var/runtime/index.mjs",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'node-fetch'",
"Require stack:",
"- /var/task/src/handlers/version.js",
"- /var/runtime/index.mjs",
" at _loadUserApp (file:///var/runtime/index.mjs:726:17)",
" at async Object.module.exports.load (file:///var/runtime/index.mjs:741:21)",
" at async file:///var/runtime/index.mjs:781:15",
" at async file:///var/runtime/index.mjs:4:1"
]
}
Not sure if you'd have an idea why this happens.thdxr
06/23/2022, 5:58 PMRobert
06/23/2022, 5:59 PMbundle: {
externalModules: ['node-fetch'],
nodeModules: ['node-fetch'],
},
thdxr
06/23/2022, 6:00 PMRobert
06/23/2022, 6:00 PMthdxr
06/23/2022, 6:00 PMRobert
06/23/2022, 6:01 PMRobert
06/23/2022, 6:07 PM