langchainjs: Cannot find name 'this'
getting the following error when compiling in node 18 environment on my express server with ts: node_modules/@langchain/core/dist/language_models/chat_models.d.ts(64,129): error TS2304: Cannot find name ‘this’. node_modules/@langchain/core/dist/language_models/llms.d.ts(66,122): error TS2304: Cannot find name ‘this’.
updated from “langchain”: “^0.0.72” due to davinci deprecation
here is my package.json:
{
"name": "docker_web_app",
"description": "A web app to serve apis",
"type": "module",
"scripts": {
"testingRun": "node src/server.ts",
"clean": "rimraf dist/*",
"lint": "eslint . --ext .ts --fix",
"build": "npm-run-all clean lint tsc",
"tsc": "tsc",
"start": "node ./dist/server.js",
"dev:start": "npm-run-all build start",
"dev": "nodemon --watch,ejs --exec npm run dev:start",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@aws-sdk/client-cloudfront": "^3.363.0",
"@aws-sdk/client-s3": "^3.362.0",
"@aws-sdk/s3-request-presigner": "^3.362.0",
"@babel/cli": "^7.23.0",
"@babel/node": "^7.22.19",
"@babel/preset-typescript": "^7.23.2",
"@dropbox/sign": "^1.1.3",
"@pdf-lib/fontkit": "^1.1.1",
"@react-pdf/renderer": "^3.1.14",
"@sendgrid/mail": "^7.7.0",
"@types/node": "^18.11.3",
"@types/react": "^18.2.36",
"@types/react-dom": "^18.2.14",
"@types/validator": "^13.7.16",
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.40.1",
"@typescript-eslint/parser": "^5.40.1",
"axios": "^1.1.3",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.0",
"config": "^3.3.7",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dateformat": "^5.0.3",
"dayjs": "^1.11.9",
"dayjs-plugin-utc": "^0.1.2",
"docxtemplater": "^3.31.3",
"dotenv": "^16.0.1",
"ejs": "^3.1.8",
"eslint": "^8.26.0",
"exec": "^0.2.1",
"express": "^4.18.2",
"express-validator": "^7.0.1",
"jsonwebtoken": "^8.5.1",
"langchain": "^0.1.3",
"multer": "^1.4.5-lts.1",
"mysql2": "^2.3.3",
"npm-run-all": "^4.1.5",
"numeral": "^2.0.6",
"pdf-lib": "^1.17.1",
"react": "^18.2.0",
"react-pdf": "^7.5.1",
"reflect-metadata": "^0.1.13",
"sequelize": "^6.31.1",
"sequelize-typescript": "^2.1.5",
"shelljs": "^0.8.5",
"tmp": "^0.2.1",
"twilio": "^3.84.0",
"typeorm": "^0.3.16",
"typescript": "^5.0.4",
"ws": "^8.13.0"
},
"devDependencies": {
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@babel/preset-react": "^7.22.15",
"@types/express": "^4.17.20",
"@types/express-serve-static-core": "^4.17.37",
"@types/multer": "^1.4.7",
"@types/stream-to-promise": "^2.2.4",
"@types/tmp": "^0.2.3",
"babel-loader": "^9.1.3",
"install": "^0.13.0",
"nodemon": "^2.0.19",
"npm": "^10.2.0",
"ts-node": "^10.9.1",
"webpack": "^5.89.0",
"webpack-cli": "^5.1.4"
}
}
and tsconfig
{
"compilerOptions": {
"module": "ES2022",
"esModuleInterop": true,
"target": "ES2022",
"jsx": "react",
"lib": [
"ES2022",
"DOM"
],
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"strict": false,
"noImplicitReturns": false,
"strictPropertyInitialization": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowJs": true,
"noUnusedLocals": true,
"sourceMap": true,
"outDir": "dist",
"rootDir": "src",
},
"include": [
"**/*.ts",
"**/*.tsx",
],
"exclude": [
"node_modules",
"dist",
"**/*.js"
]
}
About this issue
- Original URL
- State: closed
- Created 5 months ago
- Comments: 30
🤖
Hello @bryantpa! Just wanted to jump in and say a quick hello. I’m Dosu, a friendly bot here to assist you with any bugs, questions, or contributions while you wait for a human maintainer. Looking forward to helping you out!
The error you’re encountering, “Cannot find name ‘this’”, typically occurs when the
thiskeyword is used in a context where it’s not defined. In TypeScript,thisis not bound in arrow functions and always represents the object that defined the arrow function.The error is likely happening because
thisis being accessed inside an arrow function where it’s not defined. To resolve this issue, you could convert the arrow function to a regular function and use thebindmethod to bindthisto the appropriate context. Here’s an example:In this code,
traverseis converted to a regular function andthisis bound to the appropriate context using thebindmethod. Now,thisinsidetraversewill refer to the correct context, and you should no longer get the “Cannot find name ‘this’” error.However, this is a general solution and might not apply directly to your case as I don’t have visibility into the exact code causing the issue. If the error is coming from within the
langchainpackage, it might be an issue with the package itself. In that case, you might want to reach out to the maintainers of the package or check if there’s an updated version that resolves this issue.Also, I noticed that you’re using TypeScript version 5.0.4 in your project. According to the LangChain.js compatibility information, LangChain.js is compatible with TypeScript versions less than 5.2.0. So, your TypeScript version should be compatible with LangChain.js.
If the issue persists, could you please provide more information about the context in which the error occurs? Specifically, the part of your code where you’re using the
langchainpackage would be helpful. This will allow me to provide a more accurate solution.Sources
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.
Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
If you want to continue the conversation, start your reply with @dosu-bot.
Bumping to latest langchain@0.1.31 and @langchain/openai@0.0.26 should fix the most recent iteration of this - apologies for the delay!
I love you. It works for me.
@williamwinkler I was able to get around it by adding the following to my tsconf.json>compilerOptions:
"skipLibCheck": true,This has been creeping in since v 0.0.94 depending on what modules you use. Looks like core broke about 3 months ago with 0.0.204.
Heres my entire build file for the example (this deploys and launches on firebase functions):
Maybe try setting
skipLibCheckin your tsconfig?https://www.typescriptlang.org/tsconfig#skipLibCheck