SvelteKit and Postgres implementation solution - node.js

Has anyone successfully deployed a SvelteKit app using npm pg to Netlify/Vercel/Cloudflare? My local dev implementation works just fine with how I have it set up ( db.ts file with a query function in lib/server, and then using endpoint actions and the load function in +page.server.ts files)
My build errors are as follows:
node_modules/pg-connection-string/index.js:3:18: ERROR: Could not resolve "url" node_modules/pg-connection-string/index.js:4:17: ERROR: Could not resolve "fs" node_modules/pg-pool/index.js:2:29: ERROR: Could not resolve "events" node_modules/pg-protocol/dist/parser.js:9:41: ERROR: Could not resolve "assert" node_modules/pg/lib/client.js:3:27: ERROR: Could not resolve "events"
And many of the above display a previous log message with something along the lines of:
✘ [ERROR] Could not resolve "buffer"
node_modules/safe-buffer/index.js:3:21:
3 │ var buffer = require('buffer')
╵ ~~~~~~~~
The package "buffer" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
I've been trying to get it to just build and it seems like it isn't able to use the pg package because it isn't a true node server environment. For each adapter it attempts to build with (except the node adapter) it refuses to build anything to do with the pg npm package. I could be wrong about the why, but my question about the how remains.
My hope is to avoid something like Prisma (which hasn't been working for me either) and I am trying to do this as "intended" meaning that I want to use SvelteKit as both the front end and the true backend. So an additional express server or the like is not the solution I'm looking for.
EDIT: I have also successfully deployed the app to Azure using the node adapter, but pg AND Postgres.js both do not work.

Related

hardhat dependency not picked up by heroku/vercel

I built a Minting API using hardhat. I want to host it on heroku/vercel.
However I get the below error on deployment.
Error: Cannot find module 'hardhat'
I am using node.js, express.js
I have the below require.
const hre = require("hardhat");
My code works perfectly fine on localhost.
Do check if the 'hardhat' is under devDep or dep. It should be under dep in package.json.

Using require within an NPM package

I've written a custom NPM package that will spin up a mocked Apollo GraphQL server for me with some custom settings.
In my /bin folder I have a file, server.js which is responsible for spinning up the server.
In package.json I've set up my command like this:
...
"bin": {
"mock-server": "./src/server.js"
},
...
So when I run the command mock-server from the parent project it will execute the server.js file.
All good so far, but the problem is that once I start trying to require dependencies I run into this error:
$ use-env mock-server
/Users/dev/projects/share-vde-frontend/node_modules/.bin/mock-server: line 1: syntax error near unexpected token `('
/Users/dev/projects/share-vde-frontend/node_modules/.bin/mock-server: line 1: `const { ApolloServer } = require("apollo-server");'
error Command failed with exit code 2.
My knowledge of writing npm modules is just based on what I've seen in packages I've been using in the past, so maybe I'm missing something key. Do I need some special measures when requiring imports? Or do I need to build and transpile the code? I'm using ES6 syntax, but I feel confident that anyone using this package will be on modern Node.js versions (the package is private and only to be used within our organisation).
Based on the comment from #jonrsharpe above, I solved it by added shabang to the start of the server.js file:
#!/usr/bin/env node
const { ApolloServer } = require("apollo-server");
...

'heroku local' cannot find module 'load-foreman-procfile'

I'm trying to run Heroku applications locally via 'heroku local,' but a missing module error is occurring in the Heroku CLI. Is this a Heroku bug that just needs to be reported or is there something wrong with how things have been setup on my machine (mac)? Any suggestions are appreciated.
In order to ensure my application isn't the problem, I've been debugging this issue with the node application Heroku provides in its getting started guides. Meaning, it already has a Procfile that can be run using default settings.
Things I've tried:
Re-install node modules for both application and CLI
Re-install the Heroku CLI
Here's the error:
node-js-getting-started [master] :> heroku local
Error: Cannot find module '../../load-foreman-procfile'
at Object.<anonymous> (/usr/local/Cellar/heroku/7.26.1/libexec/node_modules/#heroku-cli/plugin-local/lib/commands/local/index.js:5:18)
The file for the route above requires a 'load-foreman-procfile' like so:
const Procfile = require('../../load-foreman-procfile');
That require path doesn't lead to a to file by that name. In fact, this is the only reference to 'load-foreman-procfile' I can find in '/usr/local/Cellar/heroku/7.26.1'.
Rather than the error code above, I would expect heroku local to yield a running local server started via my Procfile.
I got the same error running heroku-cli v7.26.0. I switched to their edge channel (currently v7.26.2) and heroku local worked for me after that.
like #sophon mentioned - updating from v7.26.0 to v7.26.2 solved this for me. heroku update on mac did the trick.

Azure Error: SSL Error: CERT_UNTRUSTED when using mongoose

Have a locally running node.js app using mongoose to do crud with mlab's mongodb. No problems
I post my code to github, and then tell Azure to host my app as a webapp, which I have done many times without mongoose.
As best I can tell, Azure does an npm install on my package.json file on my behalf as it installs my app into one of their web server hosts.
If I comment out these lines in my users.js route:
//var mongoose = require('mongoose');
//var ObjectID = require('mongodb').ObjectID;
all is well, Azure runs my app.
If I do not comment them out, I get this error written in the Azure log:
npm http GET https://registry.npmjs.org/mongoose
npm ERR! Error: SSL Error: CERT_UNTRUSTED
In an effort to fix this in my package.json file, I have:
[1] forced azure to use this version of node
"engines": {
"node": "0.8.x"
},
[2] tried to force to a current version
"mongoose": "^5.3.11",
[3] tried to force negotiator to this version as GitHub was complaining about a security issue with negotiator
"negotiator": ">=0.6.1",
I would REALLY like to continue to have Azure run my web apps using GitHub, and not get into the Azure command line stuff to install my bits, so I don't have much control of the installation. There is something about Mongoose that is bad.
thanks
One thing you could try is updating to the newer certificates' definitions.
This version of config-defs.js might do the trick (usually located under /deps/npm/lib/utils).
You might also need to update that file with an additional certificate as described here
Hope it helps!
Neil Lunn fixed it, thanks. I was having problems running v10.x.x of node, and tried dropping back to V8.x.x to get around a breaking change in v10, and I screwed up the name of V8. "node": "0.8.x" does not get V8, 8.10.0 does and all is well.

Cannot find module 'swagger_params_parser'

I have a Swagger NodeJS project which was created using Swagger
Swagger uses swagger-connect 0.1.0 out of the box but I've now upgraded to swagger-connect 0.7.0 using the manual instructions which included adding the following to config/default.yml:
_swagger_params_parser: # <= Add this definition
name: swagger_params_parser
jsonOptions: {}
urlencodedOptions:
extended: false
multerOptions:
inMemory: true
textOptions:
type: "*/*"
and under section 'swagger_controllers:' I've added :
- swagger_params_parser
This works gloriously on my local machine but when I push to the server it throws the error:
Error initializing middleware
May 22 13:25:27 myserver "myapp": Error: Cannot find module '/var/www/myapp/node_modules/swagger-connect/node_modules/swagger-node-runner/node_modules/bagpipes/lib/fittings/swagger_params_parser'
I'm starting the server via:
node app.js
So it looks like a potential dependency bleed on my local machine but I've destroyed (rm -Rf) 'node_modules' and re-run npm install and it still works fine on my local machine but breaks on the server.
Any ideas why swagger-params-parser is missing?
Also, another weird thing is that swagger-params-parser doesn't exist locally in directory '/var/www/myapp/node_modules/swagger-connect/node_modules/swagger-node-runner/node_modules/bagpipes/lib/fittings/swagger_params_parser' and yet locally it works!?
The issue turned out to be a stray node_modules directory on the server (we normally use node_modules_production).
Two things I've learned from this:
1.) node_modules is used instead of node_module_production if it exists. My RPM was not overwriting the directory and hence left an old copy of node_modules which was incorrectly packaged up at some point. The swagger app was loading modules from node_modules and not node_modules_production
2.) swagger_params_parser is indeed missing, even on the working version of the code so it looks like this error:
Error initializing middleware
May 22 13:25:27 myserver "myapp": Error: Cannot find module '/var/www/myapp/node_modules/swagger-connect/node_modules/swagger-node-runner/node_modules/bagpipes/lib/fittings/swagger_params_parser
is misleading, as that module is never present, at least in version 0.7.0 of swagger-connect

Resources