如何修复“您可能需要适当的加载程序来处理此文件类型,当前没有配置加载程序来处理此文件。”

Kir*_*hka 10 javascript typescript reactjs next.js

我使用 next.js。当导出第三方包中的文件index.ts 中的类型时,会发生错误。

Module parse failed: Unexpected token (23:7)
    You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
    > export type { Validate } from './src/nullable'
Run Code Online (Sandbox Code Playgroud)

Sma*_*der 12

在package.json中,假设添加以下依赖项

\n

"@yourproject/your-project": "文件:../your-project",

\n

然后在 next.config 中添加 transpilePackages: ['@yourproject/your-project'],。\xc2\xa0这允许加载器直接处理 .ts 文件。

\n
transpilePackages: ['@yourproject/your-project'],\nwebpack: (config) => {\n       config.resolve.extensionAlias = {\n      ".js": [".ts", ".tsx", ".js", ".jsx"],\n      ".mjs": [".mts", ".mjs"],\n      ".cjs": [".cts", ".cjs"],\n    };\n    return config;\n  },\n
Run Code Online (Sandbox Code Playgroud)\n


Dar*_*ert 5

当您的配置少于您想要使用的配置时,就会出现此问题。

就您而言,您正在尝试在 NextJS 项目中使用 Typescript。不幸的是,您的 Webpack 配置没有 Typescript 文件加载器。

有两种方法可以解决这个问题: