Skip to content

Instantly share code, notes, and snippets.

@zachgoll
Last active October 20, 2022 11:54
Show Gist options
  • Save zachgoll/9c606934ffcbbf9077116044fc9d2fc7 to your computer and use it in GitHub Desktop.
Save zachgoll/9c606934ffcbbf9077116044fc9d2fc7 to your computer and use it in GitHub Desktop.
Module not found: can't resolve 'fs' error
/** THIS FILE THROWS AN ERROR! */
import type { GetServerSideProps } from "next";
import { getFileExistence, formatResult } from './file-utils.ts'
type Props = {
doesFileExist: boolean;
};
export const getServerSideProps: GetServerSideProps = async () => {
return {
props: {
doesFileExist: getFileExistence('/some-file')
},
};
};
const ExamplePage = ({ doesFileExist }: Props) => {
return <div>File exists?: {formatResult(doesFileExist)}</div>;
};
export default ExamplePage;
import fs from 'fs'
export function getFileExistence(filepath: string) {
return fs.existsSync(filepath)
}
export function formatResult(fileExistsResult: boolean) {
return fileExistsResult ? 'Yes, file exists' : 'No, file does not exist'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment