Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

javascript - Cannot resolve 'fs' on static generate project

I want to serve static file in my Next.js app and it is throwing me an error each time it wants to use import fs from 'fs';

I'm supposed not obligated to yarn add fs in order to use fs, why is it giving me this?

My next.config.js is pretty much empty:


const nextTranslate = require('next-translate');

module.exports = {
  ...nextTranslate(),
};

My current dynamic post page is:

/** @format */
import fs from 'fs';

export default function Doc() {
  return <div>Enter</div>;
}

export const getStaticPaths = async () => {
  return {
    paths: [],
    fallback: false,
  };
};

Still I get this error:

enter image description here

I even tried to start a new project and it fails too once I try to use fs. Does it needs to be installed globally in my computer? I have node (v14.15.1) installed for sure.

question from:https://stackoverflow.com/questions/65846964/cannot-resolve-fs-on-static-generate-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

fs is a module that is built into Node.js and depends on Node.js APIs.

It cannot run in a browser. So it cannot run in a typical React component.

You can use it in a server-side only module in Next.js but if you want to use it in a component then you'll probably need to replace it with an HTTP module like Axios and write a server-side endpoint to request your data from.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...