Next.js 模块未找到:无法解析“tls”
我正在尝试将 instagram feed 添加到我的 next.js 应用程序中,因此我安装了 instagram-web-api,但是当我从“instagram-web-api”导入 Instagram
时,它给了我很多东西错误(无法解析“tls”、无法解析“fs”等)。
我可以通过 npm install & 使用其他库。导入,但我不确定为什么“instagram-web-api”给我所有这些错误。
因此,我在互联网上搜索解决方案并看到反馈,将以下代码添加到 next.config.js 中。
module.exports = { webpack5: true,
webpack: (config) => {
config.resolve.fallback = { tls: false };
return config;
},
};
因此,我用此代码替换了 module.exports = nextConfig;,删除并再次重新安装了库,然后它给了我另一堆错误(无法解析“流”,无法解析“ buffer'、无法解析“process”、无法解析“cypto”等)。
我对 webpack 没有太多了解,我习惯于只使用 next.js 的默认设置,所以我不确定发生了什么:(
I am trying to add instagram feed to my next.js app, so I installed instagram-web-api, but when I do import Instagram from "instagram-web-api"
, it gives me a lot of errors(Can't resolve 'tls', Can't resolve 'fs', etc).
I can use other library just fine with npm install & import, but I am not sure why "instagram-web-api" giving me all this errors.
So I searched on internet for solution and saw feedback to add the below code to the next.config.js.
module.exports = { webpack5: true,
webpack: (config) => {
config.resolve.fallback = { tls: false };
return config;
},
};
So, I replaced module.exports = nextConfig;
with this code, deleted and reinstalled the library again, then it gives me another bunch of errors (Can't resolve 'stream', Can't resolve 'buffer', Can't resolve 'process', Can't resolve 'cypto', etc).
I don't have a lot of knowledge on webpack, I'm used to using just the default setting of next.js, so I am not sure what's going on :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Instagram-web-api 是一个 Node js 模块,您正在尝试在前端使用它。您应该将所有 instagram-web-api 代码移至 getStaticProps,如下所示
Instagram-web-api is a node js module and you are trying to use it on the frontend. You should move all your instagram-web-api code to the getStaticProps like this
The error "Module not found: Error: Can't resolve 'tls'" occurs because there has been a breaking change in Webpack version 5.
To solve the error, set the browser.tls property to false in your package.json file.
tls is a Node.js core module and should most likely not be bundled with your client-side code.
By setting tls to false, we use an empty module instead of including a polyfill for the tls module.
If the error persists, try to install the net module.
If the error persists, try to edit your webpack.config.js file.
就我而言,如果您使用 Next.js 应用程序目录和服务器操作,并且在文件顶部忘记了“使用服务器”,则会出现此错误。
就我而言,显示
module not found tls
和can't parse perf_hooks
,我希望它可以帮助别人。In my case, if you are using Next.js app directory and server actions, this errors appear if on top of the file you have forgotten the
"use server"
.In my case show
module not found tls
andcan't resolve perf_hooks
, I hope it can helps somebody.