Issues while deploying a whatsapp bot on vercel - node.js

I have created a whatsapp bot which uses openai, whatsapp-web.js and nodejs.
While deploying it on vercel, I am facing following issues:
Error: Failed to launch the browser process!
[0212/144729.058867:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported.
These are the output that I am getting on deployment:
Running build in Washington, D.C., USA (East) – iad1
Cloning github.com/hritik2002/GitaGpt-Bot (Branch: main, Commit: 17356f1)
Previous build cache not available
Cloning completed: 401.653ms
Running "vercel build"
Vercel CLI 28.15.3
Running "install" command: `npm install`...
npm WARN deprecated puppeteer#13.7.0: < 18.1.0 is no longer supported
added 121 packages, and audited 122 packages in 6s
10 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
/vercel/path0/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241
reject(new Error([
^
Error: Failed to launch the browser process!
[0212/144729.058867:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
at onClose (/vercel/path0/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:241:20)
at Interface.<anonymous> (/vercel/path0/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:231:68)
at Interface.emit (node:events:525:35)
at Interface.close (node:internal/readline/interface:536:10)
at Socket.onend (node:internal/readline/interface:262:10)
at Socket.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
Node.js v18.12.1
Error: Command "node app.js" exited with 1
My package.json file:
{
"dependencies": {
"axios": "^0.26.1",
"qrcode-terminal": "^0.12.0",
"whatsapp-web.js": "^1.18.0"
}
}
My app.js code:
const qrcode = require("qrcode-terminal");
const { Client, LocalAuth, MessageMedia } = require("whatsapp-web.js");
const axios = require("axios");
async function QueryMessage(message) {
// performs certain task:
}
const client = new Client({
authStrategy: new LocalAuth(),
});
client.initialize();
client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
client.on("authenticated", () => {
console.log("AUTHENTICATED");
});
client.on("ready", () => {
console.log("Client is ready!");
});
const sendMessage = async (chat, message) => {
await chat.sendMessage(await QueryMessage(message));
};
//Replying Messages with image from url
client.on("message", async (message) => {
const chat = await message.getChat();
if (!chat.isGroup) {
sendMessage(chat, message.body);
}
});

Related

MongoMemoryServer: Instance closed unexpectedly in supertest for express app

I am trying to use Mongo Memory Server in supertest to test my API, but I get the following error:
Starting the MongoMemoryServer Instance failed, enable debug log for more information. Error:
UnexpectedCloseError: Instance closed unexpectedly with code "1" and signal "null"
at MongoInstance.closeHandler (/home/jonathanpotter/code/express-apps/rest-api-tutorial/node_modules/mongodb-memory-server-core/src/util/MongoInstance.ts:543:52)
at ChildProcess.emit (node:events:513:28)
at ChildProcess.emit (node:domain:489:12)
at maybeClose (node:internal/child_process:1091:16)
at Socket.<anonymous> (node:internal/child_process:449:11)
at Socket.emit (node:events:513:28)
at Socket.emit (node:domain:489:12)
at Pipe.<anonymous> (node:net:313:12)
My test is as follows:
import supertest from "supertest";
import createServer from "../utils/server";
import { MongoMemoryServer } from "mongodb-memory-server";
import mongoose from "mongoose";
const app = createServer();
describe("product", () => {
beforeAll(async () => {
const mongoServer = await MongoMemoryServer.create();
await mongoose.connect(mongoServer.getUri());
});
afterAll(async () => {
await mongoose.disconnect();
await mongoose.connection.close();
});
describe("get product", () => {
describe("given the product does not exist", () => {
it("should return a 404", async () => {
const productId = "product123";
await supertest(app).get(`/api/products/${productId}`);
});
});
});
});
My testing dependencies are:
"jest": "^29.3.1",
"mongodb-memory-server": "^8.10.2",
"supertest": "^6.3.3",
"ts-jest": "^29.0.3",
"ts-node-dev": "^2.0.0",
...and I my operating system is Pop!_OS 22.04 LTS.
edit: This all works fine on my Mac, but not on my Linux machine. Does anyone know of a fix for Linux (Pop!OS)?

error jest-worker just run in cpanel (adonisjs + nextjs + reactjs)

hello i got problem when deploying adonisjs + nextjs + reactjs in cpanel. i got the log error like this
events.js:291
throw er; // Unhandled 'error' event
^
Error: spawn /opt/alt/alt-nodejs12/root/usr/bin/node EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:274:12)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'EAGAIN',
code: 'EAGAIN',
syscall: 'spawn /opt/alt/alt-nodejs12/root/usr/bin/node',
path: '/opt/alt/alt-nodejs12/root/usr/bin/node',
spawnargs: [
'/home/test123/nodevenv/repositories/testproject/12/lib/node_modules/jest-worker/build/workers/processChild.js'
]
}
my server.js
'use strict'
const { Ignitor } = require('#adonisjs/ignitor')
new Ignitor(require('#adonisjs/fold'))
.appRoot(__dirname)
.fireHttpServer()
.catch(console.error)
my next.config.js
'use strict'
const next = require('next')
const { withPlugins } = require('next-compose-plugins')
const withOffline = require('next-offline')
require('dotenv').config()
const nextWithOfflineConfig = {
dontAutoRegisterSw: false,
generateInDevMode: false,
workboxOpts: {
swDest: './next/.next/service-worker.js',
runtimeCaching: [
{
urlPattern: /^https?.*/,
handler: 'NetworkFirst',
options: {
cacheName: 'https-calls',
networkTimeoutSeconds: 15,
expiration: {
maxEntries: 150,
maxAgeSeconds: 1 * 24 * 60 * 60, // 1 day
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
debug: false,
},
}
const nextEnv = {
env: {
APP_KEY: process.env.APP_KEY,
}
}
module.exports = withPlugins([
[withOffline, { nextWithOfflineConfig }],
[nextEnv]
])
package.json
"next": "^10.0.9",
"next-compose-plugins": "^2.2.1",
"next-offline": "^5.0.3",
"next-page-transitions": "^1.0.0-beta.2",
"next-pwa": "^5.2.21",
"react": "^17.0.2",
cpanel dependencies
node 12.22.9
npm 6.14.0
i already try
rm -rf node_modules
rm package-lock.json yarn.lock
npm cache clear --force
npm install
but still not solve the issue
any solutions?
I'm going to guess your server is running CloudLinux, as this is when this error usually comes up?
It's down to the limits on the number of processes and threads you can create inside an LVE. If you edit your next.conf.js and add the experimental section:
module.exports = {
experimental: {
workerThreads: false,
cpus: 4
}
};
This disables worker threads, and sets a CPU count of 4 so it limits the processes created - you might still need to adjust this depending on the limits inside the account you're running the build from.
You can read a bit more about it here in the blog I wrote after helping a customer with the same issue: https://kdaws.com/2022/10/fixing-next-js-error-spawn-errno-11-on-cpanel-and-plesk-with-cloudlinux/
Thanks,
Karl

Next JS - How to fix Error: connect ECONNREFUSED 127.0.0.1:3003 at TCPConnectWrap.afterConnect [as oncomplete]

I am new to Next JS.
I can't build my application locally. When I enter the npm run build command I get the following errors :
...
_currentUrl: 'http://localhost:3003/data/menus.json',
[Symbol(kCapture)]: false
},
response: undefined,
isAxiosError: true,
toJSON: [Function: toJSON]
}
Error: connect ECONNREFUSED 127.0.0.1:3003
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1148:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3003,
config: {
url: 'http://localhost:3003/data/menus.json',
method: 'get',
headers: {
Accept: 'application/json, text/plain, */*',
'User-Agent': 'axios/0.21.4'
},
transformRequest: [ [Function: transformRequest] ],
transformResponse: [ [Function: transformResponse] ],
timeout: 0,
adapter: [Function: httpAdapter],
...
Et à la fin du log
...
info - Generating static pages (771/771)
> Build error occurred
Error: Export encountered errors on following paths:
/ar
/ar/classic
/ar/minimal
/ar/standard
/ar/vintage
/de
/de/classic
/de/minimal
/de/standard
/de/vintage
/en
/en/classic
/en/minimal
/en/standard
/en/vintage
/es
/es/classic
/es/minimal
/es/standard
/es/vintage
/fr
/fr/classic
/fr/minimal
/fr/standard
/fr/vintage
/he
/he/classic
/he/minimal
/he/standard
/he/vintage
/zh
/zh/classic
/zh/minimal
/zh/standard
/zh/vintage
at C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\export\index.js:487:19
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Span.traceAsyncFn (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\telemetry\trace\trace.js:60:20)
at async C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\build\index.js:833:17
at async Span.traceAsyncFn (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\telemetry\trace\trace.js:60:20)
at async C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\build\index.js:707:13
at async Span.traceAsyncFn (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\telemetry\trace\trace.js:60:20)
at async Object.build [as default] (C:\Users\PC\OneDrive\Documents\lab\samara\laravel\node_modules\next\dist\build\index.js:77:25)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: C:\Program Files\nodejs\node.exe
Arguments: C:\Users\PC\.node\corepack\yarn\1.22.15\lib\cli.js build
Directory: C:\Users\PC\OneDrive\Documents\lab\samara\laravel\shop
Output:
info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 1.
The npm run dev command works without any problem. So locally the application has no problem. But when I go to production, I want to build I get these errors above.
Here is my server.js file:
// server.js
const { createServer } = require('http');
const { parse } = require('url');
const next = require('next');
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3003;
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port });
const handle = app.getRequestHandler();
app.prepare().then(() => {
createServer(async (req, res) => {
try {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
if (pathname === '/a') {
await app.render(req, res, '/a', query);
} else if (pathname === '/b') {
await app.render(req, res, '/b', query);
} else {
await handle(req, res, parsedUrl);
}
} catch (err) {
console.error('Error occurred handling', req.url, err);
res.statusCode = 500;
res.end('internal server error');
}
}).listen(port, (err) => {
if (err) throw err;
console.log(`> Ready on http://${hostname}:${port}`);
});
});
And my package.json file
{
"name": "#samara/shop",
"version": "1.1.0",
"private": true,
"scripts": {
"dev": "next dev -p 3003",
"build": "next build",
"start": "NODE_ENV=production node server.js"
If you need another file to check let me know.
I don't know if it's the best way but in my case I run npm run dev then run npm run build and the error stops happening
then stop the dev and run npm run start
you can also add a try catch in the api call http://localhost:3003/data/menus.json
if you are calling in a getStaticProps function, you can in the catch set the revalidity to 1 second if you need to revalidate
This one is a duplicate of a:
ECONNREFUSED during 'next build'. Works fine with 'next dev'
The issue is explained quite well in this thread.
Essentially as the comment states:
You should not fetch an API route from getStaticProps...
Please check also Ciro Santilli answer, he gives more details to the issue

sp-request : trying get file details from sharepoint returns ECONNREFUSED

I have the following code that successfully uploads test.sspkg to my sharepoint app catalog. Now I'm trying to use sp-request to prove that it's actually there.
But I'm getting an ECONNREFUSED error.
Error message
This shows the error:
[14:24:15] INFO: Checking if file (test.sppkg) is checked out
[14:24:15] INFO: File checkout type: 0
[14:24:17] Published file 1482ms
[14:24:17] And we're done...
trying to retrieve: https://mytenant.sharepoint.com/sites/JJTest/_api/web/GetFileByServerRelativeUrl(#FileUrl)/$value?#FileUrl='%2Fsites%2FJJTest%2FAppCatalog%2Ftest.sppkg'
errored out
{ GotError: connect ECONNREFUSED 127.0.0.1:443
at onError (/src/myplugins/node_modules/got/dist/source/request-as-event-emitter.js:140:29)
at ClientRequest.request.on.error (/src/myplugins/node_modules/got/dist/source/request-as-event-emitter.js:157:17)
at ClientRequest.emit (events.js:203:15)
at ClientRequest.EventEmitter.emit (domain.js:448:20)
at ClientRequest.origin.emit.args (/src/myplugins/node_modules/#szmarczak/http-timer/dist/source/index.js:43:20)
at TLSSocket.socketErrorListener (_http_client.js:401:9)
at TLSSocket.emit (events.js:198:13)
at TLSSocket.EventEmitter.emit (domain.js:448:20)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1107:14) name: 'RequestError', code: 'ECONNREFUSED' }
Package
This is the node package I'm using to call the SP API:
"dependencies": {
"sp-request": "^3.0.0"
}
Code:
const sprequest = require('sp-request');
await new Promise((resolve, reject) => {
gulp
.src(folderLocation)
.pipe(
spsync({
username: uname,
password: pwd,
site: siteCatalogUrl + "/",
libraryPath: catalogName,
publish: true,
verbose: true
}))
.on("finish", () => {
var spr = sprequest.create({ username: uname, password: pwd });
//https://mytenant.sharepoint.com/sites/JJTest/_api/web/getFileByServerRelativeUrl('/sites/JJTest/AppCatalog/test.sppkg')
var filepath = `${siteCatalogUrl}/_api/web/GetFileByServerRelativeUrl(#FileUrl)/$value?#FileUrl='${encodeURIComponent("/sites/JJTest/AppCatalog/test.sppkg")}'`;
console.log('trying to retrieve: ' + filepath);
var retrieveFileUrl = filepath;
spr.get(filepath, {
encoding:null
})
.then(data => {
expect(fileContent.equals(data.body)).is.true;
resolve();
})
.catch(data => {
console.log('errored out')
console.log(data)
reject();
});
resolve();
});
});
What I've tried:
I have tried just copying parts of the output from my console directly into the browser. Specifically, this is the output i get from my debug print statement:
trying to retrieve: https://mytenant.sharepoint.com/sites/JJTest/_api/web/GetFileByServerRelativeUrl/(#FileUrl)/$value?#FileUrl='%2Fsites%2FJJTest%2FAppCatalog%2Ftest.sppkg'
errored out
Pasting this:
https://mytenant.sharepoint.com/sites/JJTest/_api/web/GetFileByServerRelativeUrl('/sites/JJTest/AppCatalog/test.sppkg')
into a browser as the URL works. It returns the details of the file to me.
Can you tell me where I've gone wrong? maybe the syntax of substituting the #FileUrl?
I am using gulp version 3.9.1. And I just did a install latest of chai and sp-request.
But tried changing the specific versions to:
"sp-request": "^2.1.3"
"gulp": "^3.9.1",
"gulp-spsync-creds": "2.3.8",
"chai": "3.5.0"
And now, it's happy! it worked!

Testing Keystone and Mongoose with Jest

I am implementing unit testing in a project with Keystone but it always throws an error at testing:
an all test suites.
/Project/node_modules/mongodb/lib/mongo_client.js:433
throw err
^
TypeError: require(...) is not a function
at /Project/node_modules/keystone/lib/core/start.js:64:44
at /Project/node_modules/async/dist/async.js:3880:24
at eachOfArrayLike (/Project/node_modules/async/dist/async.js:1069:9)
at eachOf (/Project/node_modules/async/dist/async.js:1117:5)
at _parallel (/Project/node_modules/async/dist/async.js:3879:5)
at Object.parallelLimit [as parallel] (/Project/node_modules/async/dist/async.js:3962:5)
at /Project/node_modules/keystone/lib/core/start.js:60:9
at connected (/Project/node_modules/keystone/lib/core/openDatabaseConnection.js:78:5)
at NativeConnection.<anonymous> (/Project/node_modules/keystone/lib/core/openDatabaseConnection.js:85:4)
at Object.onceWrapper (events.js:273:13)
at NativeConnection.emit (events.js:182:13)
at /Project/node_modules/mongoose/lib/connection.js:860:13
at connectCallback (/Project/node_modules/mongodb/lib/mongo_client.js:527:5)
at /Project/node_modules/mongodb/lib/mongo_client.js:430:11
at process._tickCallback (internal/process/next_tick.js:61:11)
npm ERR! Test failed. See above for more details.
I already tried using "import { init, start, mongoose } from 'keystone';" instead, and using jest async() and await. I run too "jest --clearCache". Jest version: "jest": "^24.3.1"
My .babelrc:
{
"presets": [
["#babel/preset-env",
{"targets": {
"node": "current"
}}
],
"#babel/preset-react"
]
}
test.js:
const Keystone = require('keystone');
beforeAll( done => {
Keystone.init({
'name': 'Keystone CMS',
'cookie secret': 'My_Biscuit',
});
Keystone.start();
done();
})
describe('Test Keystone CMS own features', () => {
test('Keystone connects to MongoDB', (done) => {
expect(Keystone.mongoose.connections._listening).toBeTruthy;
done();
});
});
I will then expand it to test mongoose
It seems, that it is not really possible at the moment:
https://github.com/keystonejs/keystone/issues/3564

Resources