reactjs - Next.js 模块未找到 : Can't resolve 'tls'

标签 reactjs webpack next.js instagram instagram-api

我正在尝试将 instagram 提要添加到我的 next.js 应用程序中,所以我安装了 instagram-web-api,但是当我执行 import Instagram from "instagram-web-api" 时,它给出了我有很多错误(无法解析“tls”,无法解析“fs”等)。

我可以通过 npm install & import 使用其他库,但我不确定为什么“instagram-web-api”会给我所有这些错误。

所以我在互联网上搜索解决方案并看到反馈将以下代码添加到 next.config.js。

module.exports = { webpack5: true,
  webpack: (config) => {
    config.resolve.fallback = { tls: false };

    return config;
  },
};

所以,我用这段代码替换了 module.exports = nextConfig;,删除并再次重新安装了库,然后它又给了我一堆错误(无法解析'stream',可以'无法解析“缓冲区”、无法解析“进程”、无法解析“cypto”等)。

我对 webpack 了解不多,我习惯于使用 next.js 的默认设置,所以我不确定发生了什么:(

最佳答案

Instagram-web-api 是一个节点 js 模块,您正在尝试在前端使用它。您应该像这样将所有 instagram-web-api 代码移动到 getStaticProps

export async function getStaticProps(context) {
    const client = new Instagram({ username: 'INSTAGRAM_USERNAME', password: 'INSTAGRAM_PASSWORD' });
    await client.login();

    const response = await client.getPhotosByUsername({
        username: 'INSTAGRAM_USERNAME',
    });

    return {
        props: {
            posts: response.user.edge_owner_to_timeline_media.edges,
        }, // will be passed to the page component as props
    };
}

关于reactjs - Next.js 模块未找到 : Can't resolve 'tls' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71419087/

相关文章:

javascript - react : setting background image for a file present on the server

javascript - 在 Gatsby JS 中以编程方式创建页面时如何区分某些内容

reactjs - 出现错误 : void' is not assignable to type 'FC<IPickWinnerProps>' in react js

javascript - 意外字符 '#'

webpack - 将 XHR 添加到 Vue JS webpack

javascript - Flickity 通过 webpack 返回不同的结果

javascript - Next.js - 使用 CSS 模块时无法应用动态类名

reactjs - 如何解决 Nextjs 上未找到 next-auth/client 模块的问题

javascript - 在下一个 js react 项目中添加 favicon

reactjs - react-datepicker 无法在加载时设置默认值