英文:
Module not found error with axios and follow-redirects in Next.js
问题
I'm using Next.js version 13 with axios to make API requests in my application. When using getStaticProps() to fetch data from an API, I encountered a "Module not found" error related to the follow-redirects package. I have already installed axios, but the error persists. Here's the relevant code snippet:
export async function getStaticProps() {
await axios.get('https://my-api-url.com/getlist')
.then(response => {
setData(response.data);
setLoading(false);
})
.catch(error => {
console.error('Error fetching data:', error);
setLoading(false);
});
const data = await response.json();
console.log(data);
}
I get the following error:
Module not found: Can't resolve 'debug' in 'E:\resnxt\node_modules\follow-redirects'
Did you mean './debug'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, E:\resnxt).
If changing the source code is not an option, there is also a resolve option called 'preferRelative' which tries to resolve these kinds of requests in the current directory too.
Import trace for requested module:
./node_modules/follow-redirects/debug.js
./node_modules/follow-redirects/index.js
./node_modules/axios/dist/node/axios.cjs
./src/app/api/HomePageData.js
./src/app/page.js
I have already attempted reinstalling the follow-redirects package using the --save flag, but it didn't resolve the issue.
Any help or suggestions on resolving this error would be greatly appreciated. Thank you!
英文:
I'm using Next.js version 13 with axios to make API requests in my application. When using getStaticProps() to fetch data from an API, I encountered a "Module not found" error related to the follow-redirects package. I have already installed axios, but the error persists. Here's the relevant code snippet:
export async function getStaticProps() {
await axios.get('https://my-api-url.com/getlist')
.then(response => {
setData(response.data);
setLoading(false);
})
.catch(error => {
console.error('Error fetching data:', error);
setLoading(false);
});
const data = await response.json();
console.log(data);
}
I get the following error
Module not found: Can't resolve 'debug' in 'E:\resnxt\node_modules\follow-redirects'
Did you mean './debug'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules, E:\resnxt).
If changing the source code is not an option, there is also a resolve option called 'preferRelative' which tries to resolve these kinds of requests in the current directory too.
Import trace for requested module:
./node_modules/follow-redirects/debug.js
./node_modules/follow-redirects/index.js
./node_modules/axios/dist/node/axios.cjs
./src/app/api/HomePageData.js
./src/app/page.js
I have already attempted reinstalling the follow-redirects package using the --save flag, but it didn't resolve the issue.
Any help or suggestions on resolving this error would be greatly appreciated. Thank you!
答案1
得分: 0
根据堆栈跟踪,您正在使用 Next 13 应用程序路由器(app
目录),但您正在尝试通过 getStaticProps
获取数据,而这在 app
目录下不受支持。您可能需要查看迁移文档 - https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#static-site-generation-getstaticprops
英文:
Based on the stack trace, you are using Next 13 App Router (app
directory), but you are trying to fetch data via getStaticProps
that isn't supported under the app
directory. You may want to look through the migration docs - https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration#static-site-generation-getstaticprops
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论