Skip to content

Beginner - Next JS. 'fs' module not found issue. #12124

Discussion options

You must be logged in to vote

Welcome! fs is a library exclusive to Node, so you can't use it directly in the browser. But if you import it at the top of the file and use it in a function that only runs in the server, then Next will remove this import from the client bundle, so this code is valid:

import fs from 'fs';

export default function Home(props) {
  return (
    <div className="container">
      ...
    </div>
  );
}

// getStaticProps only runs in the Node side, so it is safe
// to use libraries from Node here
export async function getStaticProps() {
  const fileNames = fs.readdirSync(postsDirectory);

  ...
}

In the tutorial these libraries are imported in another file, but a bit later they are utilized ins…

Replies: 13 comments 25 replies

Comment options

You must be logged in to vote
6 replies
@trav3711
Comment options

@LauraCole1900
Comment options

@jde
Comment options

@netrolite
Comment options

@netrolite
Comment options

Answer selected by Timer
Comment options

You must be logged in to vote
3 replies
@rafaelalmeidatk
Comment options

@theogravity
Comment options

@eddrichjanzzen
Comment options

Comment options

You must be logged in to vote
3 replies
@rafaelalmeidatk
Comment options

@Morantron
Comment options

@beshur
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
7 replies
@Coder-Jedi
Comment options

@tracyhenry
Comment options

@johnbarhorst
Comment options

@Cerity06
Comment options

@DanB-Web
Comment options

Comment options

You must be logged in to vote
2 replies
@yddc902
Comment options

@orcololo
Comment options

Comment options

You must be logged in to vote
3 replies
@Ajay-056
Comment options

@bonesoul
Comment options

@matthill8286
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@cwtuan
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet