I am trying out using ESM for some Lambda NodeJS functions. I have a compiled js file (from typescript, using esnext), a symlink to the node_modules folder (see https://github.com/vibe/aws-esm-modules-layer-support) and a basic package.json file that has a name, and "type": "module"
set.
Some functions work fine, but a few that use a shared package are not loading. The package is this one, https://github.com/capaj/object-resolve-path, but I have forked it to my own private repo, and converted it to ESM.
I don't get any TS compilation errors, but when I try to invoke the function, it is failing with the following error:
2022-08-12T12:58:58.467Z undefined ERROR Uncaught Exception {"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token '{'","stack":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token '{'"," at _loadUserApp (file:///var/runtime/index.mjs:724: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"]}
I'm not getting any syntax errors show when editing the module code in VSCode.
So I must have converted the module wrong? But I can't see what is wrong.
I added an index.d.ts file with:
declare module 'object-resolve-path' {
export default function resolvePath(obj: any, path: string): any;
}
And changed module.exports
to export const ...
eg
Before
var Path = require('./path')
module.exports = function (o, path) {
if (typeof path !== 'string') {
throw new TypeError('path must be a string')
}
if (typeof o !== 'object') {
throw new TypeError('object must be passed')
}
var pathObj = Path.get(path)
if (!pathObj.valid) {
throw new Error('path is not a valid object path')
}
return pathObj.getValueFrom(o)
}
Now
import Path from './path.js';
export default resolvePath(o, path) {
if (typeof path !== 'string') {
throw new TypeError('path must be a string')
}
if (typeof o !== 'object') {
throw new TypeError('object must be passed')
}
var pathObj = Path.get(path)
if (!pathObj.valid) {
throw new Error('path is not a valid object path')
}
return pathObj.getValueFrom(o)
}
I can't figure out anyway to debug this locally, as the lambda functions are part of a mono-repo setup, and they deploy via cloudformation as various stacks.