question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js API routes (and pages) should support reading files

See original GitHub issue

Feature 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:closed
  • Created 4 years ago
  • Reactions:228
  • Comments:146 (25 by maintainers)

github_iconTop GitHub Comments

84reactions
jkjustjoshingcommented, Oct 30, 2019

Workaround I’m using:

# next.config.js
module.exports = {
    serverRuntimeConfig: {
        PROJECT_ROOT: __dirname
    }
}

and in the location you need the path

import fs from 'fs'
import path from 'path'
import getConfig from 'next/config'
const { serverRuntimeConfig } = getConfig()

fs.readFile(path.join(serverRuntimeConfig.PROJECT_ROOT, './path/to/file.json'))

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).

33reactions
borispoehlandcommented, Jul 13, 2020

@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')

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found