最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

node.js - Enabling imports between two bundled TS packages in a monorepo - Stack Overflow

matteradmin2PV0评论

I have a monorepo with a /frontend and /backend dir. I have some shared types and enums in backend that I import into frontend. I've set up paths in my tsconfig to correctly alias this, and I reference it:

// frontend tsconfig.json
{
  "compilerOptions": {
    "rootDir": "./",
    "target": "ES2020",
    "useDefineForClassFields": true,
    "lib": ["ES2020", "DOM", "DOM.Iterable"],
    "module": "ESNext",
    "skipLibCheck": true,

    /* Bundler mode */
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true,

    "paths": {
      "@/*": ["./src/*"],
      "@backend/*": ["../backend/*"],
      // "@shared/*": ["../shared/*"],
      "$/*": ["./react-maptiler/*"],
      "react-maptiler": ["./react-maptiler/index.ts"]
    },
    "typeRoots": ["./src/types"]
  },
  "exclude": ["node_modules", "dist"],
  "include": ["src", "serviceWorker/generateSW.js", "serviceWorker/sw.js"],
  "references": [
    { "path": "./tsconfig.node.json" },
    { "path": "../backend/" },
    { "path": "../shared/" }
  ]
}

Then I import into my frontend like this:

import { isUser } from '@backend/models/account'

But when I run tsc I get a bunch of these errors:

src/state/globalState.ts:7:8 - error TS6305: Output file 'C:/Users/user/Desktop/monorepoName/backend/models/account.d.ts' has not been built from source file 'C:/Users/user/Desktop/monorepoName/backend/models/account.ts'.    

7 } from '@backend/models/account'

My backend tsconfig also uses moduleResolution: bundler` with vite-node. Is there any way I can import into frontend from backend, since backend doesn't have a build step? This is the backend's tsconfig.json:

{
  "compilerOptions": {
    
/* "incremental": true, */
    "composite": true,
    "target": "ESNext",
    "experimentalDecorators": true,
    
/* This is essential for DBOS to work: */
    "emitDecoratorMetadata": true,
    "module": "ESNext",
    "moduleResolution": "bundler",
    
// "declaration": true,
    
// "declarationMap": true,
    "sourceMap": true,
    
// "outDir": "./dist",
    "newLine": "lf",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "paths": {
      "@/*": ["./*"] 
//"@shared/*": ["../shared/*"]
    },
    "baseUrl": ".",
    "resolveJsonModule": true,
    "typeRoots": ["@types"]
  },
  "include": [
    "api",
    "auth",
    "drizzle",
    "models",
    "utils",
    "types",
    "test/test.ts",
    "auth/firebase.ts",
    "zipcodes",
    "services",
    "config",
    "lib",
    "test",
    "@types",
    "shared"
  ],
  "exclude": ["dist"]
  
// "references": [{ "path": "../shared/" }]
}

I used to have a shared package that compiled and that worked well, but I'm currently unable to deploy my backend without having all code located within one package, so I'd like a workable solution in the present that allows me to import into frontend.

Post a comment

comment list (0)

  1. No comments so far