在Next.js应用中导入共享代码
我想通过重复使用类型,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运行dev
从Next-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 (likenext-app
) - create the below
external.ts
file in the root of the monorepo (on the same level as thenext-app
) - run
npm run dev
from thenext-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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用实验
externalDir
功能: https:// github .com/vercel/next.js/pull/22867 。我已经检查了至少在您描述的情况下起作用。
也就是说,我们还有一个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.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.