Can't resolve 'fs' but in an API route???

Any idea why this error is occuring? I thought all api routes are node compatible...
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/langchain/dist/document_loaders/fs/pdf.js
./node_modules/langchain/document_loaders/fs/pdf.js
./src/app/api/chat/route.ts
./node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=%2Fhome%2Fphilip%2Fpersonal%2Faiwirk%2Fsrc%2Fapp%2Fapi%2Fchat%2Froute.ts&page=%2Fapi%2Fchat%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGY2hhdCUyRnJvdXRlJnBhZ2U9JTJGYXBpJTJGY2hhdCUyRnJvdXRlJmFwcFBhdGhzPSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmFwaSUyRmNoYXQlMkZyb3V0ZS50cyZhcHBEaXI9JTJGaG9tZSUyRnBoaWxpcCUyRnBlcnNvbmFsJTJGYWl3aXJrJTJGc3JjJTJGYXBwJnBhZ2VFeHRlbnNpb25zPXRzeCZwYWdlRXh0ZW5zaW9ucz10cyZwYWdlRXh0ZW5zaW9ucz1qc3gmcGFnZUV4dGVuc2lvbnM9anMmcm9vdERpcj0lMkZob21lJTJGcGhpbGlwJTJGcGVyc29uYWwlMkZhaXdpcmsmaXNEZXY9dHJ1ZSZ0c2NvbmZpZ1BhdGg9dHNjb25maWcuanNvbiZiYXNlUGF0aD0mYXNzZXRQcmVmaXg9Jm5leHRDb25maWdPdXRwdXQ9JnByZWZlcnJlZFJlZ2lvbj0mbWlkZGxld2FyZUNvbmZpZz1lMzAlM0Qh&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!
Module not found: Can't resolve 'fs'

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./node_modules/langchain/dist/document_loaders/fs/pdf.js
./node_modules/langchain/document_loaders/fs/pdf.js
./src/app/api/chat/route.ts
./node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=%2Fhome%2Fphilip%2Fpersonal%2Faiwirk%2Fsrc%2Fapp%2Fapi%2Fchat%2Froute.ts&page=%2Fapi%2Fchat%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGY2hhdCUyRnJvdXRlJnBhZ2U9JTJGYXBpJTJGY2hhdCUyRnJvdXRlJmFwcFBhdGhzPSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmFwaSUyRmNoYXQlMkZyb3V0ZS50cyZhcHBEaXI9JTJGaG9tZSUyRnBoaWxpcCUyRnBlcnNvbmFsJTJGYWl3aXJrJTJGc3JjJTJGYXBwJnBhZ2VFeHRlbnNpb25zPXRzeCZwYWdlRXh0ZW5zaW9ucz10cyZwYWdlRXh0ZW5zaW9ucz1qc3gmcGFnZUV4dGVuc2lvbnM9anMmcm9vdERpcj0lMkZob21lJTJGcGhpbGlwJTJGcGVyc29uYWwlMkZhaXdpcmsmaXNEZXY9dHJ1ZSZ0c2NvbmZpZ1BhdGg9dHNjb25maWcuanNvbiZiYXNlUGF0aD0mYXNzZXRQcmVmaXg9Jm5leHRDb25maWdPdXRwdXQ9JnByZWZlcnJlZFJlZ2lvbj0mbWlkZGxld2FyZUNvbmZpZz1lMzAlM0Qh&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!
"dependencies": {
"@types/node": "20.3.2",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6",
"autoprefixer": "10.4.14",
"eslint": "8.43.0",
"eslint-config-next": "13.4.7",
"langchain": "^0.0.99",
"next": "13.4.7",
"pdf-parse": "^1.1.1",
"postcss": "8.4.24",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.3.2",
"typescript": "5.1.6",
"zlib": "^1.0.5"
}
"dependencies": {
"@types/node": "20.3.2",
"@types/react": "18.2.14",
"@types/react-dom": "18.2.6",
"autoprefixer": "10.4.14",
"eslint": "8.43.0",
"eslint-config-next": "13.4.7",
"langchain": "^0.0.99",
"next": "13.4.7",
"pdf-parse": "^1.1.1",
"postcss": "8.4.24",
"react": "18.2.0",
"react-dom": "18.2.0",
"tailwindcss": "3.3.2",
"typescript": "5.1.6",
"zlib": "^1.0.5"
}
Thanks a lot
7 Replies
mferino
mferino•12mo ago
Stack Overflow
Module not found: Can't resolve 'fs' in Next.js application
Unable to identify what's happening in my next.js app. As fs is a default file system module of nodejs. It is giving the error of module not found.
philip.2000
philip.2000•12mo ago
I mean, I only use fs in the API route. Do I really need to change the next config? Feels a bit weird since app router is pretty server-first, isn't it? I think I found the problem
mferino
mferino•12mo ago
Ahh nice what is it?
philip.2000
philip.2000•12mo ago
Seems like the nextRuntime cannot be edge but has to be nodejs.
mferino
mferino•12mo ago
oic, good find!
philip.2000
philip.2000•12mo ago
Thanks for the help
mferino
mferino•12mo ago
yw! glad you found an answer 🙂