NextJS 中由 `node_modules` 的 try/require 引起的“无法解析‘编码’”警告

问题描述 投票:0回答:1

在打字稿中的 NextJS

13.4.19
应用程序中,使用
@apollo/[email protected]
会在构建步骤中导致来自 ApolloServer/node-fetch 的警告。这似乎是由
try / require
中的
node-fetch
引起的:

  • ./node_modules/node-fetch/lib/index.js
let convert;
try {
  convert = require('encoding').convert;
} catch (e) {}
  • 警告:
- warn Compiled with warnings

./node_modules/node-fetch/lib/index.js
Module not found: Can't resolve 'encoding' in 'C:<...>\apollo-server-encoding-warn\node_modules\node-fetch\lib'

Import trace for requested module:
./node_modules/node-fetch/lib/index.js
./node_modules/@apollo/server/dist/esm/plugin/schemaReporting/schemaReporter.js
./node_modules/@apollo/server/dist/esm/plugin/schemaReporting/index.js
./node_modules/@apollo/server/dist/esm/ApolloServer.js
./node_modules/@apollo/server/dist/esm/index.js
./app/api/graphql/route.ts
  • tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

手动安装

encoding
包作为删除此警告的解决方法,但这不是一个非常令人满意的解决方案,因为我不想安装使用此模式检查其存在的每个未来包。

在开发模式下构建和运行时都会发生此警告,有没有办法告诉 NextJs/typescript 在这些警告来自

node_modules
时不要报告这些警告?还是我错过了其他什么症状?

node.js typescript next.js apollo-server node-fetch
1个回答
0
投票

我最终发现这个警告实际上来自 Webpack,在浏览选项时我发现我可以通过更新

next.config.js
文件来目标抑制它,如下所示:

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    config.ignoreWarnings = [
      // https://webpack.js.org/configuration/other-options/#ignorewarnings
      {
        module: /node-fetch/,
        message: /.*Can't resolve 'encoding'.*/,
      },
    ];

    return config;
  },
};

module.exports = nextConfig;
© www.soinside.com 2019 - 2024. All rights reserved.