将私人仓库导入下一个项目时出错

Error importing private repo into next project

我正在尝试导入私有 github 存储库以在 React/Next 项目中使用它,但是它会抛出一个错误,指出当前没有配置加载器,我不认为实际上是我在此项目上导入其他 .TS 文件的问题,而不是直接从存储库导入,所以我怀疑导入时遗漏了一些东西。

存储库非常简单,只有一个 READMEComms.ts 文件。

这是我 package.json 上的回购协议:

"my-library": "git+https://ghp_mYpErsOnaLAcCeSSToKenOnGithUb1234567:x-oauth-basic@github.com/My-Repo/my-library.git"

以下是我将其导入我的项目的方式:

import * as Comms from "my-library"

错误:

./node_modules/my-library/Comms.ts
Module parse failed: Unexpected token (21:50)
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 const FetchTokenIdsFromWallet=async(account: string, collectionAddr: string, chain: string)=>{
|   
|   const requestString = 'https://deep-index.moralis.io/api/v2/' + account + '/nft/' + collectionAddr + '?chain=' + chain + '&format=decimal'

原来是loader的问题。勾选here看如何添加loader,基本上是用

添加loader
yarn add ts-loader --dev

然后将 webpack 添加到您的 next.config.js:

module.exports = {
  reactStrictMode: true,
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.module.rules.push({
      test: /\.(ts)x?$/, // Just `tsx?` file only
      use: [
        // options.defaultLoaders.babel, I don't think it's necessary to have this loader too
        {
          loader: "ts-loader",
          options: {
            transpileOnly: true,
            experimentalWatchApi: true,
            onlyCompileBundledFiles: true,
          },
        },
      ],
    });
    return config
  }
}

如果在此之后您收到类似 _your_module__WEBPACK_IMPORTED_MODULE_2__.default.yourFunc is not a function 的错误,请检查您是否以正确的方式导出函数,例如 。如果您以正确的方式导出它们,请尝试 运行 yarn upgrade

相关文档:

webpack

custom webpack for next.js