Tre*_*cos 9 workspace typescript monorepo nestjs yarn-workspaces
我正在尝试创建一个包含 NestJs API、React 应用程序以及它们之间共享的许多其他较小项目的 monorepo
\nmonorepo 设置了纱线工作区
\n使用 Vite 的 React 应用程序可以完美地导入和编译常见项目,但 NestJs api 永远不会编译,通常会给出错误SyntaxError: Unexpected token \'export\'。
我制作了一个最小的存储库,其中仅包含一个基本的 Nest 项目和一个公共文件夹,以尝试使用以下结构进行单个函数导入:
\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 common # Sample common project\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src \n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 test.ts # The file called from the api\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package.json \n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 api # NestJs API\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig \n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src \n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package.json \n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package.json # Yarn workspace setup\nRun Code Online (Sandbox Code Playgroud)\n主要的package.json:
\n{\n "name": "mono",\n "workspaces": [\n "common",\n "api"\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n常见的package.json
\n{\n "name": "@test/common",\n "version": "1.0.0",\n "main": "index.ts"\n}\nRun Code Online (Sandbox Code Playgroud)\n常见的 tsconfig
\n{\n "compilerOptions": {\n "module": "CommonJS",\n "allowSyntheticDefaultImports": true,\n "baseUrl": ".",\n "esModuleInterop": true,\n "composite": true,\n "sourceMap": true,\n "declaration": true,\n "declarationMap": true\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n及其复杂的测试函数在src/test.ts中
\nexport const hi = () => \'Hello there\';\nRun Code Online (Sandbox Code Playgroud)\n这是main.ts我调用此函数的文件:
import { NestFactory } from \'@nestjs/core\';\nimport { AppModule } from \'./app.module\';\nimport { hi } from \'@test/common\';\n\nasync function bootstrap() {\n const app = await NestFactory.create(AppModule);\n await app.listen(3000);\n\n console.log(\'All fine\', hi());\n}\nbootstrap();\nRun Code Online (Sandbox Code Playgroud)\napi包.json
\n{\n "name": "api",\n "scripts": {\n "start": "nest start",\n ...other commands\n },\n "dependencies": {\n "@nestjs/common": "^9.0.0",\n "@nestjs/core": "^9.0.0",\n "@nestjs/platform-express": "^9.0.0",\n "@test/common": "*",\n "reflect-metadata": "^0.1.13",\n "rxjs": "^7.2.0",\n "webpack-node-externals": "^3.0.0"\n },\n "devDependencies": {\n ...nest deps\n "ts-jest": "29.0.3",\n "ts-loader": "^9.2.3",\n "ts-node": "^10.0.0",\n "tsconfig-paths": "4.1.1",\n "typescript": "^4.7.4"\n },\n "jest": {\n ...jest config\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n以及 api 的 tsconfig
\n{\n "compilerOptions": {\n "module": "CommonJS",\n "declaration": true,\n "removeComments": true,\n "emitDecoratorMetadata": true,\n "experimentalDecorators": true,\n "allowSyntheticDefaultImports": true,\n "target": "es2017",\n "sourceMap": true,\n "outDir": "./dist",\n "baseUrl": "./",\n "incremental": true,\n "skipLibCheck": true,\n "strictNullChecks": false,\n "noImplicitAny": false,\n "strictBindCallApply": false,\n "forceConsistentCasingInFileNames": false,\n },\n "include": [\n "../common",\n "./src/*"\n ],\n "references": [\n {\n "path": "../common"\n }\n ],\n "paths": {\n "@test/common/*": [\n "../common/*"\n ]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nyarn run start在Nest 项目上运行时,生成的dist文件夹似乎包含正确的文件结构:
但一次又一次导致同样的错误
\nC:\\<path>\\MonoNest\\api>yarn run start\nyarn run v1.22.19\n$ nest start\nC:\\<path>\\MonoNest\\common\\index.ts:1\nexport * from \'./src/test\';\n^^^^^^\n\nSyntaxError: Unexpected token \'export\'\n at Object.compileFunction (node:vm:360:18)\n at wrapSafe (node:internal/modules/cjs/loader:1088:15)\n at Module._compile (node:internal/modules/cjs/loader:1123:27)\n at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n at Module.load (node:internal/modules/cjs/loader:1037:32)\n at Function.Module._load (node:internal/modules/cjs/loader:878:12)\n at Module.require (node:internal/modules/cjs/loader:1061:19)\n at require (node:internal/modules/cjs/helpers:103:18)\n at Object.<anonymous> (C:\\<path>\\MonoNest\\api\\src\\main.ts:3:1)\n at Module._compile (node:internal/modules/cjs/loader:1159:14)\nerror Command failed with exit code 1.\ninfo Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.\nRun Code Online (Sandbox Code Playgroud)\n如果你想测试一下,Github 上提供了最小复制项目
\n为了清楚起见,我正在尝试制作一个“真正的”单一存储库,其中父文件夹仅包含不同项目的路径。
\n我对“官方”nestjs monorepo 文档不感兴趣,该文档使根存储库包含我在其他项目中不需要的大多数 Nestjs 包和配置
\n这是因为您尝试在非 Pure ESM 的项目中使用Pure ESM包。有时有一些解决方法,例如使用模块名称映射器(在 Jest 中)。其他时候……没有。您可以尝试使您的软件包成为纯 ESM,这会起作用......但我对该过程不够熟悉,无法提供任何帮助。
| 归档时间: |
|
| 查看次数: |
1585 次 |
| 最近记录: |