Next.js API routes (and pages) should support reading files
See original GitHub issueFeature request
Is your feature request related to a problem? Please describe.
It’s currently not possible to read files from API routes or pages.
Describe the solution you’d like
I want to be able to call fs.readFile
with a __dirname
path and have it “just work”.
This should work in Development and Production mode.
Describe alternatives you’ve considered
This may need to integrate with @zeit/webpack-asset-relocator-loader
in some capacity. This plugin handles these types of requires.
However, it’s not a necessity. I’d be OK with something that only works with __dirname
and __filename
(no relative or cwd-based paths).
Additional context
Example:
// pages/api/test.js
import fs from 'fs'
import path from 'path'
export default (req, res) => {
const fileContent = fs.readFileSync(
path.join(__dirname, '..', '..', 'package.json'),
'utf8'
)
// ...
}
Note: I know you can cheat the above example ☝️ with
require
, but that’s not the point. 😄
Issue Analytics
- State:
- Created 4 years ago
- Reactions:228
- Comments:146 (25 by maintainers)
Top Results From Across the Web
Next.js API routes (and pages) should support reading files
Here's how to read nextJS project files files from your Serverless API routes, both on Vercel in production and locally in Development.
Read more >Next.js API routes: How to read files from directory ... - Medium
Page Code. Let's first intialize our code for our page: // pages/index.js import useSWR from 'swr'; export ...
Read more >How to Load Data from a File in Next.js – Vercel Docs
Store your json files in a private folder inside your application; Construct an API endpoint to load these files using the file system;...
Read more >API Routes: Introduction - Next.js
API routes provide a solution to build your API with Next.js. Any file inside the folder pages/api is mapped to /api/* and will...
Read more >How to read markdown files from Next.js API routes - Ironeko
Next.js' API routes are handy, but they're limited. Here's a quick guide on how to read markdown files in a Next.js API route....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Workaround I’m using:
and in the location you need the path
I know this doesn’t solve the need to reference files with paths relative to the current file, but this solves my very related use case (reading image files from a
/public/images
folder).@bengrunfeld yes, your solution only works locally.
I had a similar problem lately (wanted to read a file in a API route) and the solution was easier than expected.
Try
path.resolve('./public/ts-data.csv')