【问题标题】:Imports from public folder Nextjs in typescript give error: Cannot find module从打字稿中的公共文件夹 Nextjs 导入会出现错误:找不到模块
【发布时间】:2021-11-16 03:32:40
【问题描述】:

我正在尝试使用常规导入和 Nextjs 中使用 typescript 的自定义绝对路径来导入文件。 由于某种原因,我不断收到无法找到模块的错误消息,但是当我单击它时,我的 IDE 能够链接该文件。此外,当它是一个常规的 javascript 文件而不是 typescript 时,它可以正常工作并导入文件。

现在我使用 //@ts-ignore 并且它可以工作,但我想知道是否有更好的方法来解决这个错误。

我的 tsconfig:

    {
  "compilerOptions": {
    "target": "es2015",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/components/*"],
      "#/*": ["public/assets/images/*"],
    }
  },
  "include": [
    "public/**/*",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

【问题讨论】:

  • 尝试从 tsconfig 中删除 baseUrlpaths

标签: javascript reactjs typescript ecmascript-6 next.js


【解决方案1】:

尝试更改导入路径。

如果您有 baseUrl“.”,则路径将为 ...from '/public/...'。而不是...from './public/...'

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2019-01-22
  • 1970-01-01
  • 2020-03-02
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多