Replies: 174 comments 52 replies
-
Just wanted to second that, trying to implement file uploading using API routes. I can get the file to upload but then need to be able to access it again to upload it to S3 bucket. |
Beta Was this translation helpful? Give feedback.
-
I second this! Also, being able to read directories is very important for my company's usage as we keep our data like team members and blog posts in a content directory so we're looking for a way to require all files in the directory. |
Beta Was this translation helpful? Give feedback.
-
The above PR will fix this! ☝️ 🙏 |
Beta Was this translation helpful? Give feedback.
-
How about |
Beta Was this translation helpful? Give feedback.
-
Hey @marlonmarcello, this is going to be possible. Stay tuned 😊 |
Beta Was this translation helpful? Give feedback.
-
It's this already solved? |
Beta Was this translation helpful? Give feedback.
-
Not yet, you can subscribe for #8334 |
Beta Was this translation helpful? Give feedback.
-
@huv1k Many thanks! |
Beta Was this translation helpful? Give feedback.
-
Is there a way to help this move forward more quickly? |
Beta Was this translation helpful? Give feedback.
-
Worth noting: if you're using TypeScript, you can already import a JSON file as a module directly (make sure import myJson from '../../../some/path/my.json'; The shape of the JSON object is also automatically used as its type, so autocomplete is really nice. |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
Saw in the PR this has changed a bit - any update on what the current plans are (or aren't)? Sounds like there are some strategies you don't want pursued, mind listing them + why so contributors can give this a shot? |
Beta Was this translation helpful? Give feedback.
-
This is blocking the usage of nexus with Next.js. It would be great to see this prioritized again. |
Beta Was this translation helpful? Give feedback.
-
Awesome Man. Worked for me. |
Beta Was this translation helpful? Give feedback.
-
I've been using the new e.g.: export async function unstable_getStaticProps() {
const siteData = await import("../data/pages/siteData.json");
const home = await import("../data/pages/home.json");
return {
props: { siteData, home }
};
} |
Beta Was this translation helpful? Give feedback.
-
I don't get how this is still a thing. I mean I now installed webpack What's the problem that this still is a problem? Would really love to know more. |
Beta Was this translation helpful? Give feedback.
-
The universal solution, is we store blob in S3, and read file directly from S3. |
Beta Was this translation helpful? Give feedback.
-
For anyone having file issues using the
After you've done this to make Sentry work on your API routes you'll need to wrap the handler like so:
There is a open PR to address this issue vercel/nft#322 |
Beta Was this translation helpful? Give feedback.
-
Just spent a whole day troubleshooting this very weird issue... details on this thread: https://github.com/orgs/vercel/discussions/1180 The only workaround that worked for me is (you need to install I added a type declaration for my template files in a declare module '*.mjml' {
const content: string
export default content
} Then I added a new Webpack configuration to load these files using the raw-loader plugin which just mimics a readFile (this is in next.config.js): webpack(config, context) {
config.module.rules.push(
{
test: /\.mjml$/,
use: 'raw-loader',
}
)
return config
} And now in my API file, I simply import the file so that instead of trying to use the static analysis to guess which files are used, it's just a normal Node dependency: import emailTemplate from '../../emails/default.mjml' |
Beta Was this translation helpful? Give feedback.
-
This discussion has been opened on Aug 5 2019 and today, 16th of Feb 2023 it is still an issue. It's been 3,5 years, how is this still a thing?! @$#!& |
Beta Was this translation helpful? Give feedback.
-
So, I've been at this for 3 days now and I have come across a few dirty solutions for it. What we know is that api's run as "serverless functions" and to optimize them Vercel bundles the strict minimum which is why we don't have access to files, they are simply not bundled. What seems to work most reliably is forcing Vercel to bundle the files by resolving the path to the files themselves with In my case I'm generating an image with background images and custom font that are in a resources folder in the root.
|
Beta Was this translation helpful? Give feedback.
-
This might be useful to some, who just want to get to the "root" (I might update this with more testing).
then, for example path.join(__next__base__dirname, "public") |
Beta Was this translation helpful? Give feedback.
-
Unfortunately none of the noted workarounds works for me. In my case I have a third party lib which is both reading and writing to a file system inside route handler, ofc it works absolutely fine locally... but not when deployed on Vercel, I am experiencing the same error as the rest of the bunch. |
Beta Was this translation helpful? Give feedback.
-
Can you read from your public folder? |
Beta Was this translation helpful? Give feedback.
-
Hm so I had an idea, very naive one I guess, that I can just use webpack plugin to copy file from source to build and that somehow read and write to it will be possible in api route. That does not seem to be the case 😖 |
Beta Was this translation helpful? Give feedback.
-
I got it to work by using /tmp dir. Most likely not something that would be of a use to most of you guys, but it work like we would expect it to work. I however have no idea how is this tmp being handled if it is being removed after a while. Docs here >> https://vercel.com/guides/how-can-i-use-files-in-serverless-functions |
Beta Was this translation helpful? Give feedback.
-
Perhaps this information could benefit someone. We use serverless functions and the task was to switch to an app router. After the transition, a problem appeared with fonts. We have used page routers in the past and had no problems. We also didn't have any problems with performance and dev experience at that time, but that’s another story...🥲 "next": "^14.1.0",
"pdfkit": "0.14.0", structure
const nextConfig = {
experimental: {
outputFileTracingIncludes: {
'/api/reports/export-report': ['./public/fonts/**/*'],
},
...
}
import PDFKit from 'pdfkit';
import path from 'path';
export const FONT_FAMILY = 'Helvetica';
export const FONT_FAMILY_BOLD = 'Helvetica-Bold';
const PUBLIC_PATH = './public';
const FONTS_PATH = 'fonts';
export const fonts = {
[FONT_FAMILY]: 'helvetica.woff',
[FONT_FAMILY_BOLD]: 'helvetica-bold.woff',
};
export const registerFonts = (doc: PDFKit.PDFDocument) => {
Object.entries(fonts).forEach(([name, fontFileName]) => {
doc.registerFont(name, getFontPath(fontFileName));
});
};
const resolveFontFolderPath = () => {
return path.resolve(process.cwd(), PUBLIC_PATH, FONTS_PATH);
};
export const getFontPath = (fontFileName: string) => {
const folderPath = resolveFontFolderPath();
return path.join(folderPath, fontFileName);
}; |
Beta Was this translation helpful? Give feedback.
-
I tried everything to allow my server actions ( "use server) to read a file stored in public folder without any success. |
Beta Was this translation helpful? Give feedback.
-
4 years btw |
Beta Was this translation helpful? Give feedback.
-
Just ran into this issue. So basically, vercel is caching my JSON files in a completely unrelated path, which is why all my JSON imports break upon deployment (and work inside a dev environment). The solution seems to be to create some sort of 'hack' import step that tricks the vercel compiler to bundle the json files against its will. And this code has to be duplicated across all API endpoints. It was foolish for me to assume that importing a JSON file (which has been standard esm spec for years) would be respected within vercel. My solution has been to get rid of JSON entirely within my project. I am lucky that I can afford to do this. Others seem not so lucky. Judging by the age of this problem, vercel has no intention of supporting JSON imports. Longer term, we are forced to migrate away from vercel, as this is one of many basic issues that they refuse to deal with. |
Beta Was this translation helpful? Give feedback.
-
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:
Beta Was this translation helpful? Give feedback.
All reactions