https://serverless-stack.com/ logo
#help
Title
# help
s

Scott

05/03/2022, 1:08 AM
Has anyone had recent luck with getting typeORM implemented with SST? I've tried previously mentioned solutions for externalModules/nodeModules for pg-native but cannot seem to by-pass the error when running sst build/seed deploy:
Copy code
Error: There was a problem transpiling the Lambda handler: > node_modules/pg/lib/native/client.js:4:21: error: Could not resolve "pg-native" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    4 │ var Native = require('pg-native')
Note - The above works locally with sst start I've also tried an alternative solution which involves having a mocked pg-native library (which just returns an empty module). This actually works with sst build & seed deploy, BUT then sst start runs into issues running typeORM locally. See attached error. I'm currently at a stand still of either typeORM works locally OR online, but have been unable to find a solution that works for both.
y

Yohann B

05/03/2022, 7:10 AM
Are you sure you added :
bundle :{
Copy code
externalModules: ['pg-native']
}
to your lambda function configuration ?
s

Scott

05/03/2022, 7:15 AM
@Yohann B, thanks for the reply, I've tried that in multiple places, but out of interest. What would be the most correct location for it? Api stack level (ApiStack.ts)
Copy code
this.api = new Api(this, 'Api', {
defaultAuthorizationType: ApiAuthorizationType.AWS_IAM,
defaultFunctionProps: {
    environment: {
        // PRODUCTS_BUCKET: productsBucket.bucketName,
        // LISTINGS_BUCKET: listingsBucket.bucketName,
        DATABASE: DATABASE,
        CLUSTER_ARN: database.clusterArn,
        SECRET_ARN: database.secretArn,
        REGION: scope.region,
    },
    permissions: [database],
    bundle: {
        externalModules: ['pg-native'],
    },
},
Function Level (ApiStack.ts)
Copy code
'GET /public': {
    function: {
        handler: 'src/public.main',
    },
    authorizationType: ApiAuthorizationType.NONE,
    bundle: {
        externalModules: ['pg-native'],
    },
},
Main Application (index.ts)
Copy code
app.setDefaultFunctionProps({
    runtime: 'nodejs14.x',
    bundle: {
        externalModules: ['pg-native']
    }
});
y

Yohann B

05/03/2022, 7:51 AM
Something seems off with your config, maybe you can install pg-native locally so it will be included for now and won't create the problem
f

Frank

05/03/2022, 3:53 PM
Thanks for chiming in @Yohann B!
@Scott can you put together a sample repo (ie. 1 function) that reproduces this error?
And I will give it a try on my end.
s

Scott

05/04/2022, 12:05 AM
Will see if I can put that together today, cheers @Frank
@Frank I created a new repo here - https://github.com/Scott-lasertrade/typeorm-poc It builds+runs fine with sst build/seed, but replicates the error I get above or rather there is no error unless you turn on uncaught exceptions with breakpoints. So generally it just hangs, let me know how you go.
@Frank, did you have any luck/trouble with the repo above or does it work on your end?
t

thdxr

05/11/2022, 3:19 AM
I'll be looking into this tomorrow @Scott
s

Scott

05/11/2022, 5:57 AM
Cheers @thdxr, had to make a bit of a judgement call, have started on conversion to kysely so may not be as relevant now - pending whether I can get it all working as it's quite a big migration.