在Next.js应用中导入共享代码

发布于 2025-02-12 12:08:38 字数 1489 浏览 1 评论 0原文

我想通过重复使用类型,DTO和其他同构应用程序来利用MonorePo从同一单声道存储库中的后端(NEST.JS)服务中使用。在我的情况下,下一个。我正在重新出口nest-app/libs/types/src/index.ts中的一堆文件,然后在我的next.js应用中导入它。但是,当我这样做时,Next.js抱怨未能编译错误

重现错误的最简单方法是

  • 为monorepo创建一个任意文件夹,例如test> test-import < /code>
  • 导航到monorepo文件夹(cd test-import
  • 运行npx create-next-app@最新-Typescript并选择一个名称对于项目(例如next-app),
  • 在MonorePo的根部创建下面的external.ts文件(与next> Next-app <在同一级别上/code>)
  • 运行npm运行devNext-app文件夹

monorepo结构:

external.ts
next-app
  (other files omitted)
  pages
    index.tsx

external.ts

export type ExternalType = string;

export const me = "ME";

next-app/pages/index.tsx是导入external.ts的内容

import { me } from "../../external";

// boilerplate code omitted
<h1 className={styles.title}>
  Welcome {me} to <a href="https://nextjs.org">Next.js!</a>
</h1>

../external.ts
Module parse failed: Unexpected token (1: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 ExternalType = string;
| 
| export const me = "ME";

I want to leverage monorepo by reusing types, DTOs, and other isomorphic app stuff from the backend (Nest.js) services within the same mono repo. In my case the next.js app and the nest.js app (which is a nest.js monorepo on its own) are on the same level in the root dir. I'm re-exporting a bunch of files in the nest-app/libs/types/src/index.ts and then import it in my next.js app. But when I do so, next.js complains with Failed to compile error

The easiest way to reproduce the error is to do the following

  • create an arbitrary folder for the monorepo, like test-import
  • navigate to the monorepo folder (cd test-import)
  • run npx create-next-app@latest --typescript and choose a name for the project (like next-app)
  • create the below external.ts file in the root of the monorepo (on the same level as the next-app)
  • run npm run dev from the next-app folder

Monorepo structure:

external.ts
next-app
  (other files omitted)
  pages
    index.tsx

external.ts

export type ExternalType = string;

export const me = "ME";

next-app/pages/index.tsx is importing content of external.ts

import { me } from "../../external";

// boilerplate code omitted
<h1 className={styles.title}>
  Welcome {me} to <a href="https://nextjs.org">Next.js!</a>
</h1>

The following error is thrown:

../external.ts
Module parse failed: Unexpected token (1: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 ExternalType = string;
| 
| export const me = "ME";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-02-19 12:08:38

您需要使用实验externalDir功能: https:// github .com/vercel/next.js/pull/22867

// next.config.js
const nextConfig = {
  ...
  experimental: {
    externalDir: true
  }
}

我已经检查了至少在您描述的情况下起作用。


也就是说,我们还有一个Nest+Next MonorePo(在我们的情况下,共享代码都超出了Next和Nest-并且要使它运行,我们必须求助于symint链接(ln -s -s)巢文件夹中的共享代码。如果您决定拥有一个具有共享代码的文件夹,该文件夹都存在于下一个和Nest Codebase之外,则可能也需要尝试。

IIRC有一种使它在没有符号链接的情况下使其正常工作的方法,但是对于该巢穴,必须管理整个MonorePo,包括非nest subprojects。

You need to use the experimental externalDir feature: https://github.com/vercel/next.js/pull/22867.

// next.config.js
const nextConfig = {
  ...
  experimental: {
    externalDir: true
  }
}

I have checked that it works at least in the case you have described.


This said, we also have a Nest+Next monorepo — although in our case the shared code is outside of both Next and Nest — and to get it working we had to resort to symlinking (ln -s) the shared code into the Nest folder. If you ever decide to have a folder with shared code that exists outside both the Next and Nest codebases, you might want to try this as well.

IIRC there is a way to get it working without a symlink but for that Nest has to manage the whole monorepo, including non-Nest subprojects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文