Morning guys! I'm using a new library `DigestFetc...
# help
r
Morning guys! I'm using a new library
DigestFetch
and it requires
node-fetch
When I do a build, ESBuild gives me the error
Copy code
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?
Copy code
bundle:
    packager: npm
    forceInclude:
      - node-fetch
    externals:
      - node-fetch
t
what is the full error?
r
Copy code
Error: 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.
t
this package is bundled wrong, for some reason they list
node-fetch
as a dev dep when it's needed. You should be able to fix this problem by adding node-fetch yourself
r
Ok, I'll try this thanks!
Didn't work sadly, now I'm getting this error.
Copy code
f351736a-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.
Nevermind, I found it.
Had to force the node-fetch to an older version.
Hi again @thdxr I added a dependency to node-fetch in my package.json, when building, and running locally everything works fine. But when I push my code, not too sure what happens. Seed build everything but I have this error when I try to access the service
Copy code
{
  "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.
t
Did you try adding it to the bundle.nodeModules
r
Something like so?
Copy code
bundle: {
          externalModules: ['node-fetch'],
          nodeModules: ['node-fetch'],
        },
t
yeah can remove it from external though
r
I'll give it a go
t
I'd suggest not using this source library if possible, it's package pretty poorly
r
Yeah, I'll try to see if I can switch it for something else
Adding it to nodeModules fixed it indeed, thanks!