I’ve upgraded to `1.0.0-beta.23` and I’m getting t...
# sst
j
I’ve upgraded to
1.0.0-beta.23
and I’m getting this error… although I’m not 100% sure this is an upgrade issue, I’m still in the very early, walking skeleton phase of dev so lots of things are changing…
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')
      ╵                      ~~~~~~~~~~~
    at Object.bundle (/Users/jamlen/dev/src/github.com/sst-demo/node_modules/@serverless-stack/core/dist/runtime/handler/node.js:184:23)
    at Object.bundle (/Users/jamlen/dev/src/github.com/sst-demo/node_modules/@serverless-stack/core/dist/runtime/handler/handler.js:19:16)
    at new Function (/Users/jamlen/dev/src/github.com/sst-demosst-demo/node_modules/@serverless-stack/resources/src/Function.ts:725:39)
    at RDS.createMigrationsFunction (/Users/jamlen/dev/src/github.com/sst-demo/node_modules/@serverless-stack/resources/src/RDS.ts:371:16)
    at new RDS (/Users/jamlen/dev/src/github.com/sst-demo/node_modules/@serverless-stack/resources/src/RDS.ts:187:36)
    at new CustomerProfileStack (/Users/jamlen/dev/src/github.com/sst-demo/src/platforms/customerProfile/index.ts:16:25)
    at Object.main (/Users/jamlen/dev/src/github.com/sst-demo/stacks/index.ts:23:5)
    at Object.<anonymous> (/Users/jamlen/dev/src/github.com/sst-demo/.build/run.js:92:16)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
s
Was this build working previously? As I know pg-native is not a normal dependency, see thread here: https://serverless-stack.slack.com/archives/C01JG3B20RY/p1624651544051700
j
I can’t say for certain it was working previously… I built a separate demo to see how to use
typeorm
but that is using v0.67 (I think) and I didn’t have to define that in there.
In this repo I’m splitting things up with lerna and workspaces, so trying to figure out where I would need to put this, since not all stacks use typeorm and therefore I don’t want it at that top level.
I’ll post a tree of the repo so you can see what I mean
Copy code
.
├── README.md
├── cdk.context.json
├── lerna.json
├── package.json
├── src
│   ├── packages
│   │   └── clients
│   │       ├── notificationConsumer.ts
│   │       ├── index.ts
│   │       ├── jest.config.js
│   │       ├── package.json
│   │       ├── test
│   │       └── tsconfig.json
│   └── platforms
│       ├── customerProfile
│       │   ├── data
│       │   │   ├── index.ts
│       │   │   └── migration
│       │   ├── entities
│       │   │   ├── Customer.ts
│       │   │   └── index.ts
│       │   ├── index.ts
│       │   ├── jest.config.js
│       │   ├── package.json
│       │   ├── scripts
│       │   ├── services
│       │   │   ├── api
│       │   │   │   └── customer.ts
│       │   │   └── events
│       │   │       └── userCreated.ts
│       │   ├── test
│       │   └── tsconfig.json
│       ├── fulfilment
│       ├── order
│       └── user
│           ├── index.ts
│           ├── jest.config.js
│           ├── package.json
│           ├── scripts
│           ├── services
│           │   ├── auth
│           │   │   └── cognitoTriggers.ts
│           │   └── events
│           ├── test
│           └── tsconfig.json
├── sst.json
├── stacks
│   └── index.ts
├── tsconfig.json
└── yarn.lock
Where the user platform doesn’t access the DB, it just takes the cognito signup and publishes events
s
Hmm not sure on a great solution, you could try the solution above for only the routes that require pg-native(typeorm) to see if it's causing the problem. e.g. 'GET /public': { function: 'src/platforms/customerProfile/services/api/custom.handler', bundle: { nodeModules: ['pg-native'] } },
j
Yep tried that but still didn’t work and gave same error message.
s
Ahh dang, sorry I couldn't be of more help.
j
No worries thanks for you help and quick responses.
@Frank @thdxr any ideas?
f
@jamlen a slight correction to the snippet Scott shared:
Copy code
'GET /public': {
    function: {
      handler: "...",
      bundle: {
        nodeModules: ['pg-native']
      }
    }
  }
},
j
Thanks @Frank I’ve tried a few variations of that but still end up with the same error.
Copy code
this.api = new sst.Api(this, "CustomerApi", {
            defaults: {
                function: {
                    bundle: { nodeModules: ['typeorm', 'pg', 'pg-native'] },
                    srcPath: `${props.srcPath}/customerProfile`,
                    environment: {
                        DATABASE: 'customerProfile',
                        CLUSTER_ARN: cluster.clusterArn,
                        SECRET_ARN: cluster.secretArn,
                    },
                    permissions: [cluster, props.bus],
                },
                authorizer: "iam",
            },
            routes: {
                "GET    /customer": `services/api/customer.get`,
                "POST   /customer": `services/api/customer.post`,
                "GET    /customer/{id}": `services/api/customer.findOne`,
                "PUT    /customer/{id}": `services/api/customer.put`,
                "PATCH  /customer/{id}": `services/api/customer.patch`,
                "DELETE /customer/{id}": `services/api/customer.remove`,
            }
        })
s
This technically gets past that error for me but I can't seem to get any requests that involve typeorm to setup the connection properly.
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,
        },
        permissions: [database],
        // bundle: {
        //     nodeModules: ['typeorm'],
        // },
    },
    routes: {
        'GET /run/migrations': {
            function: {
                handler: 'src/run/migrations.main',
                bundle: {
                    nodeModules: ['pg-native'],
                },
            },
            authorizationType: ApiAuthorizationType.NONE,
        },
    },
});
j
Is that using v1.0.0-beta.23?
s
Ahh no, I'm still on v0.69.6 sorry
f
@Scott thanks for chiming in
@jamlen if you run
sst build
, and go into
.build/cdk.out
, you should see a bunch of
asset.*
directories.
If you go into one of them, do u see
node_modules
folders in them?
j
So I currently have three stacks, the first is just establishing an eventbus to share, the 2nd stack creates the Auth and the 3rd has the first of my APIs. when I run
yarn sst build
I only get one
asset.*
directory and it is for the stack with the auth (and trigger function).
I do not get any asset folder for the 3rd stack and therefore no
node_modules
however, if I add a dependency for the Auth and add that bundle line (I used
uuid
as an example) then in there I do get a
node_modules
folder… still fails with the same error for
pg-native
though.
Copy code
Using stage: jamlen
Preparing your SST app
Synthesizing CDK
Building function services/auth/cognitoTriggers.postAuth
Building function index.handler

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')
I also don’t see where its getting that
index.handler
from… I was expecting it to say
building function services/api/customer.get
f
Can u try running
sst build
again and send me the
.build/sst-debug.log
?
j
sent… my
node_modules
at the top level does have the package, so I don’t think its having issues installing the package
f
Do you have
pg-native
in ur package.json?
s
Hmm interesting, disregard my previous success actually I still get this error when running sst build, but not when running sst start
j
No I do not have
pg-native
in package.json… I think it will be a peer dependency for
pg
which is in my package.json. If I try to
yarn add pg-native
I get this:
Copy code
error /Users/jamlen/dev/src/github.com/sst-demo/node_modules/libpq: Command failed.
Exit code: 1
Command: node-gyp rebuild
Arguments:
Directory: /Users/jamlen/dev/src/github.com/sst-demo/node_modules/libpq
Output:
gyp info it worked if it ends with ok
gyp info using node-gyp@7.1.2
gyp info using node@14.19.0 | darwin | x64
gyp info find Python using Python version 3.9.7 found at "/usr/local/opt/python@3.9/bin/python3.9"
gyp info spawn /usr/local/opt/python@3.9/bin/python3.9
gyp info spawn args [
gyp info spawn args   '/Users/jamlen/dev/src/github.cosst-demo/node_modules/@npmcli/run-script/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/jamlen/dev/src/github.com/sst-demo/node_modules/libpq/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/jamlen/dev/src/github.com/sst-demo/node_modules/@npmcli/run-script/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/jamlen/Library/Caches/node-gyp/14.19.0/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/jamlen/Library/Caches/node-gyp/14.19.0',
gyp info spawn args   '-Dnode_gyp_dir=/Users/jamlen/dev/src/github.com/sst-demo/node_modules/@npmcli/run-script/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/jamlen/Library/Caches/node-gyp/14.19.0/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/jamlen/dev/src/github.com/sst-demo/node_modules/libpq',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
/bin/sh: pg_config: command not found
gyp: Call to 'pg_config --libdir' returned exit status 127 while in binding.gyp. while trying to load binding.gyp
gyp ERR! configure error
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/Users/jamlen/dev/src/github.com/tfgm/bus-reform-ticketing/node_modules/@npmcli/run-script/node_modules/node-gyp/lib/configure.js:351:16)
gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:282:12)
gyp ERR! System Darwin 21.4.0
gyp ERR! command "/Users/jamlen/.nvm/versions/node/v14.19.0/bin/node" "/Users/jamlen/dev/src/github.com/sst-demo/node_modules/.bin/node-gyp" "rebuild"
gyp ERR! cwd /Users/jamlen/dev/src/github.com/sst-demo/node_modules/libpq
gyp ERR! node -v v14.19.0
gyp ERR! node-gyp -v v7.1.2
gyp ERR! not ok
which led me to this GH issue which was closed as fixed in 2014! https://github.com/brianc/node-postgres/issues/684
I don’t have postgres installed on my machine - because I wouldn’t need it with an SST local dev env anyway.
Its looking like I’ll have to
brew install postgres
just so I can have the tools this thing needs… that’s a pain-in-the-arse and something I totally don’t need!
s
I see you added a checkmark @jamlen, did you manage to solve the problem? What was the resolution?
I found a weird 'solution', but it achieves the complete opposite. I created a mock pg-native as an empty package and use that as a dependency. This allows for proper builds and seems to run fine for both sst build + on seed, but now I run into issues when running sst start.
j
I had to brew install pg to get it working… I'd much rather I didn't have to though.