I am having trouble getting my slack bolt app with typescript to run locally, I get this…
- User
I am having trouble getting my slack bolt app with typescript to run locally, I get this error:
Error: Cannot find module '../../../utils/constants' Require stack:
- /Users/brandondickson/workspace/slack-bot-desk-genie/dist/modals/addAdmin.js
- /Users/brandondickson/workspace/slack-bot-desk-genie/dist/modals/index.js
- /Users/brandondickson/workspace/slack-bot-desk-genie/dist/index.js at Module._resolveFilename (node:internal/modules/cjs/loader:939:15) at Module._load (node:internal/modules/cjs/loader:780:27) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (/Users/brandondickson/workspace/slack-bot-desk-genie/dist/modals/addAdmin.js:7:18) at Module._compile (node:internal/modules/cjs/loader:1105:14) at Module._extensions..js (node:internal/modules/cjs/loader:1159:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Module._load (node:internal/modules/cjs/loader:827:12) at Module.require (node:internal/modules/cjs/loader:1005:19) { code: 'MODULE_NOT_FOUND', requireStack: [ '/Users/brandondickson/workspace/slack-bot-desk-genie/dist/modals/addAdmin.js', '/Users/brandondickson/workspace/slack-bot-desk-genie/dist/modals/index.js', '/Users/brandondickson/workspace/slack-bot-desk-genie/dist/index.js' ] }
This is my tsconfig:
{ "compilerOptions": { "target": "es2018", "module": "commonjs", "moduleResolution": "node", "esModuleInterop": true, "resolveJsonModule": true, "allowSyntheticDefaultImports": true, "strict": true, "allowJs": true, "sourceMap": true, "outDir": "dist", "baseUrl": "./src", "paths": { "@/": [""] } }, "include": ["src//*", "prisma//*"] }
This is my package.json:
{ "name": "slack-bot-desk-genie", "version": "1.0.0", "description": "xDesign desk booking slack bot", "homepage": "https://bitbucket.org/xdesign365/slack-bot-desk-genie", "main": "app.js", "prisma": { "seed": "ts-node prisma/seed.ts" }, "scripts": { "start:app": "node ./dist/index.js", "start:dev": "ts-node ./src/app.ts", "start:db": "docker compose up", "start:ngrok": "./ngrok http 3000", "start:studio": "npx prisma studio", "db:seed": "npx prisma db seed", "lint:verify": "eslint . --ext .ts && echo All files pass linting.", "lint:fix": "eslint --fix . --ext .ts && echo All files pass linting.", "prettier:verify": "prettier --config --check ./.prettierrc.js "/*{.ts,.json,.js}"", "prettier:fix": "prettier --config --write ./.prettierrc.js "/*{.ts,.json,.js}"", "prepare": "husky install", "build": "tsc" }, "license": "ISC", "dependencies": { "@prisma/client": "4.14.0", "@slack/bolt": "3.13.1", "aws-lambda": "1.0.7", "date-fns": "2.30.0", "dotenv": "16.0.3", "slack-block-builder": "2.7.2" }, "devDependencies": { "@types/aws-lambda": "8.10.115", "@types/node": "20.1.2", "@typescript-eslint/eslint-plugin": "5.59.5", "@typescript-eslint/parser": "5.59.5", "eslint": "8.40.0", "eslint-config-prettier": "8.8.0", "husky": "8.0.3", "prettier": "2.8.8", "prettier-plugin-organize-imports": "3.2.2", "prettier-plugin-prisma": "4.13.0", "prisma": "4.14.0", "serverless": "3.30.1", "serverless-dotenv-plugin": "6.0.0", "serverless-functions-base-path": "1.0.33", "serverless-offline": "12.0.4", "serverless-offline-scheduler": "0.5.0", "serverless-plugin-typescript": "2.1.4", "serverless-tscpaths": "0.0.8", "ts-node": "10.9.1", "typescript": "5.0.4" } }
- Assistant
The error you're experiencing is because the compiled JavaScript code in the
distfolder is unable to find the required module '../../../utils/constants'. This issue can occur due to two potential reasons:- The path to the
constantsmodule in your TypeScript code is incorrect. - The compiled JavaScript code in the
distfolder is not in sync with your TypeScript source code.
First, make sure that the path to the
constantsmodule is correct in your TypeScript code. In the file that imports theconstantsmodule, you should have:import { CONSTANTS } from '@/utils/constants';Now, let's check if the compiled JavaScript code is in sync with the TypeScript source code. Perform the following steps:
-
Run to compile the TypeScript code and generate the latest JavaScript code in the folder.
- The path to the
- User
- Assistant
It appears that the issue is related to the path alias not being resolved correctly in the compiled JS code. The compiled JS code still contains the
@/...alias, which does not get resolved during runtime.Here's what you can do to fix the issue:
- Install
tsconfig-pathsandts-nodeas dependencies:
npm install --save-dev tsconfig-paths ts-node- Modify the
start:appscript in yourpackage.jsonfile to includets-nodeandtsconfig-paths:
"scripts": { "start:app": "ts-node -r tsconfig-paths/register ./src/app.ts", ... }This will allow the
ts-nodecommand to compile and run your TypeScript code with the correct path resolution based on yourtsconfig.jsonfile during runtime.Run
npm run buildto ensure that your compiled code in thedistfolder has the correct path resolution.Finally, try running the app using
npm run start:app. This should resolve the issue with the paths not being properly resolved in your app during runtime. - Install
- User
- Assistant
The error you're encountering is because of the
socketModebeing set totruein your App configuration. If you want to use Socket Mode, you will need to provide a valid app token. Make sure that you have theSLACK_APP_TOKENenvironment variable correctly set in your.envfile.If you do not want to use Socket Mode, set
socketModetofalsein the App configuration. Here's the modified configuration:const app = new App({ token: process.env.SLACK_BOT_TOKEN, signingSecret: process.env.SLACK_SIGNING_SECRET, socketMode: false, appToken: process.env.SLACK_APP_TOKEN, port: 3000, });After making the changes, be sure to restart your application. If the error persists, please double-check your
.envfile and make sure the environment variableSLACK_APP_TOKENis set correctly. - User
I am trying to setup stripe local webhooks with nestjs but I am getting 404 errors and nothing is logging in the condole even though I'm getting activity in the slack cli terminal
This is my stripe-webhook-controller.ts:
import { Controller, Post, Req, Res } from '@nestjs/common'; import { Stripe } from 'stripe'; import { Request, Response } from 'express';
@Controller('webhook') export class StripeWebhookController { @Post() async handleStripeWebhook( @Req() req: Request, @Res() res: Response, ): Promise { const stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { apiVersion: '2022-11-15', });
- Assistant
The 404 error suggests that the route for your webhook is not registered or not reachable. To help you identify the issue, please ensure the following:
-
Make sure your NestJS application is running and listening on the correct port (in your case
3001). -
Verify that the
StripeWebhookControlleris imported and registered in yourAppModuleor another relevant module in your NestJS application.
For example, in your
app.module.ts:import { Module } from '@nestjs/common'; import { StripeWebhookController } from './stripe-webhook-controller'; @Module({ controllers: [StripeWebhookController], }) export class AppModule {}If you have a separate module for handling Stripe-related logic, make sure the
StripeWebhookControlleris registered in that module.- Make sure that there is no middleware or other request handlers interfering with the
/webhookendpoint.
If you've verified these points and are still encountering the error, please provide some additional information about your NestJS application setup, such as the file (with the code) and any other relevant module files.
-
- User
That was it! I'm now getting this error [Nest] 77197 - 05/17/2023, 8:16:48 PM ERROR [ExceptionsHandler] Webhook payload must be provided as a string or a Buffer (https://nodejs.org/api/buffer.html) instance representing the raw request body.Payload was provided as a parsed JavaScript object instead. Signature verification is impossible without access to the original signed material. Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing
- Assistant
The error message indicates that you are passing a parsed JavaScript object as the payload, while Stripe's
constructEventmethod expects the raw request body as a string or a Buffer. By default, NestJS automatically parses the request body to JSON in your Controllers.You need to access the raw body to construct the webhook event and handle Stripe's signature verification. You can achieve this by applying the
rawflag to the bodyParser middleware in yourmain.tsfile in order to get the raw request body.Here's an example of the code to update in your
main.tsfile:import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import * as express from 'express'; async function bootstrap() { const app = await NestFactory.create(AppModule); // Update default bodyParser middleware to include the raw body app.use(express.json({ verify: (req, _, buf) => (req['rawBody'] = buf) })); await app.listen(3001); } bootstrap(); - User
Best way to generate a unique booking reference in nest js
- Assistant
To generate a unique booking reference in NestJS, you can use the
uuidlibrary.uuidis a universally unique identifier package that will help you create booking references in various formats that are very difficult to predict or collide accidentally.First, install the
uuidpackage:npm install --save uuidNow, import the
v4function from theuuidpackage in your NestJS service or controller file where you want to generate the unique booking reference:import { v4 as uuidv4 } from 'uuid';Next, create a helper function to generate the booking reference:
generateBookingReference(): string { return `booking-${uuidv4()}`; // You can replace 'booking' with your custom prefix. }You can now call the
generateBookingReferencefunction whenever you need to create a unique booking reference.