You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express TS导入Vite React共享ES6包遇导出语法错误排查

共享库在Express TS后端报错:Unexpected Token 'export' 问题记录

我尝试通过GCloud npm仓库中的共享库,在Express TypeScript后端与Vite React前端之间共享通用zod模型和JS函数。共享库在前端运行完全正常,但后端持续报错。

注:ResponseErrorModel在后端可正常工作,但PatientModel和LoginFormModel无论怎么调试都无法运行,已经花了10小时排查各种相关方案。

试过的方案及遇到的问题:

  • 若在后端package.json中添加type: module或commonjs配置,错误会消失,但全栈应用无法正常运行,出现LoginRequest.ts:4 POST http://localhost:8080/api/login 500 (Internal Server Error)错误
  • 已尝试后端package.json和tsconfig中所有ES/Common相关的配置组合,以及入口文件(index.ts与index.json)的不同设置
  • 试过export * from ./forms/LoginModel直接导入LoginFormModel,也试过改用{LoginModel}的方式导入

错误信息:"Unexpected Token 'export'"
错误截图

共享库信息

package.json

{
  "name": "@something/something-shared",
  "version": "1.0.13",
  "description": "A nice greeter",
  "main": "lib/index.js",
  "types": "lib/index.d.ts",
  "scripts": {
    "clean": "rimraf lib/*",
    "build": "tsc",
    "format": "prettier --write \"src/**/*.ts\"",
    "lint": "tslint -p tsconfig.json",
    "prepare": "npm run build",
    "prepublishOnly": "npm run lint",
    "preversion": "npm run lint",
    "postversion": "git push && git push --tags",
    "patch": "npm version patch",
    "publish": "npm publish",
    "artifact-registry-login": "npx google-artifactregistry-auth"
  },
  "keywords": [],
  "author": "someone",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^18.15.11",
    "prettier": "^2.8.7",
    "rimraf": "^4.4.1",
    "tslint": "^6.1.3",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "^5.0.2"
  },
  "files": [
    "lib/**/*"
  ],
  "dependencies": {
    "zod": "^3.21.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "declaration": true,
    "outDir": "./lib",
    "strict": true,
    "moduleResolution": "Node"
  },
  "include": ["src"],
  "exclude": ["node_modules", "**/__tests__/*"]
}

后端信息

导入后的库结构

库结构截图

package.json

{
  "name": "something-backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "dev": "concurrently \"npx tsc --watch\" \"nodemon -q dist/index.js\"",
    "build": "npx tsc",
    "start": "node dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@something/something-shared": "^1.0.13",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "kysely": "^0.23.5",
    "pg": "^8.10.0",
    "zod": "^3.21.4"
  },
  "devDependencies": {
    "@types/express": "^4.17.17",
    "@types/node": "^18.15.11",
    "@types/pg": "^8.6.6",
    "concurrently": "^8.0.1",
    "nodemon": "^2.0.22",
    "prettier": "^2.8.7",
    "typescript": "^5.0.2"
  }
}

tsconfig.json(已尝试不同target和module类型)

{
  "compilerOptions": {
    "outDir": "./dist",
    "target": "es2016",
    "module": "commonjs",
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

内容的提问来源于stack exchange,提问作者gunslingor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 13:47:02