How do I work with "internal packages" in a node server application? #4509
-
The docs introduce the internal packages concept using examples for Next.js and Vite but as far as I know, that doesn't cover the case for server apps. I know that for development nodemon/ts-node can be used to run the code that imports untranspiled packages and it just works, but how do I build my server app for production? Is there a way to build/transpile the app with its dependencies? Using I know there are things like ncc, but I'm not sure if that will be the best approach. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 6 replies
-
What did you end up doing? I'm having difficulty using Firebase Functions as one of my monorepo apps because it uploads and builds everything in the cloud. The cloud build process tries to download the internal monorepo packages from the npm registry but since they are internal packages it can't find them and the cloud build fails. There are various solutions for bundling the internal packages before uploading the Firebase Functions code to Firebase, but most of them are messy or have drawbacks. Hoping you found something else! Am checking out ncc now because I never knew about that one. |
Beta Was this translation helpful? Give feedback.
-
"internal packages" are a great way to reuse code Today I lost an entire day looking at ways for "internal packages" to just work in a compiled TypeScript NodeJS app. I ended up installing tsup Here are the changes I made to my package.json: {
"name": "server",
"version": "1.0.0",
"scripts": {
"dev": "tsx watch ./src/index.ts",
- "build": "tsc",
+ "build": "tsc --noEmit && tsup ./src/index.ts",
"start": "node ./dist/index.js",
},
"dependencies": {
"express": "4.18.2",
"@shared/utils": "*", // internal package
//...
},
"devDependencies": {
"typescript": "5.2.2",
+ "tsup": "8.0.1",
// ...
}
} |
Beta Was this translation helpful? Give feedback.
-
I think the same. |
Beta Was this translation helpful? Give feedback.
-
@predrag-codetribe I ended up with this solution for Firebase. Maybe it will help you also? But I think maybe I missed the mark and we could be talking about seperate issues here. |
Beta Was this translation helpful? Give feedback.
-
Thanks I tried that, didn't work for me.
I think I am gonna quit from the "internal packages" for the Node side, Here below is what I do on the Node side that works for me: tsconfig.json {
"compilerOptions": {
"module": "CommonJS",
"target": "ES2021",
"esModuleInterop": true,
"outDir": "./dist",
"noEmit": false,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"resolveJsonModule": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
+ "baseUrl": ".",
+ "paths": {
+ "@shared/utils": [ // required for code sharing
+ "../shared/utils/src/*"
+ ]
}
},
"include": [
"./*/**/*",
+ "../shared/utils" // required for code sharing
] package.json {
"name": "server",
"version": "1.0.0",
"scripts": {
+ "dev": "tsx watch ./src/index.ts",
- "build": "tsc",
+ "build": "tsc && tsc-alias",
- "start": "node ./dist/src/index.js"
+ "start": "node ./dist/server/src/index.js"
},
"dependencies": {
// ...
- "@shared/utils": "*"
},
"devDependencies": {
+ "tsc-alias": "^1.8.8",
"tsx": "4.1.4",
"typescript": "5.2.2"
}
} I will still continue looking at this topic. |
Beta Was this translation helpful? Give feedback.
-
I spent a couple of days trying out different solutions, including those mentioned here, but I wasn't 100% happy with them. Some build tools required more changes than others, and I found the added complexity and overhead quite annoying. I even considered just running I've finally found a solution that will work well for everyone here and 100% maintains the "internal package" perks and local development experience. You need to define a The package will publish an RUN --mount=type=bind,source=.git,target=.git \
turbo run build -F "${WORKSPACE}"... \
&& turbo run publish:apply -F "${WORKSPACE}"... The "version": "1.0.0",
- "main": "./src/index.ts",
+ "main": "./dist/index.js",
"types": "./src/index.ts",
"scripts": {
"build": "tsup src/index.ts",
@@ -16,9 +16,6 @@
- "publishConfig": {
- "main": "./dist/index.js"
- }, |
Beta Was this translation helpful? Give feedback.
-
To address this challenge, I implemented a solution using tsup. Unlike the default behavior, which compiles code into a single file while excluding dependencies and peer dependencies, tsup's configuration can be adjusted to include specific dependencies, like internal packages. "scripts": {
"build": "tsc --noEmit && tsup",
"start": "node dist/server.js"
},
"tsup": {
"entry": ["src/server.ts"],
"noExternal": ["@internal-package-to-include"]
}, This setup ensures that tsup compiles the code, including the specified internal package, into the build output, effectively addressing the issue with internal packages in a Node server application. |
Beta Was this translation helpful? Give feedback.
-
We've shipped some documentation updates that describe this in detail: https://turbo.build/repo/docs/core-concepts/internal-packages |
Beta Was this translation helpful? Give feedback.
-
my solution.
|
Beta Was this translation helpful? Give feedback.
-
one alternative is to add not ideal but this would avoid the mess. |
Beta Was this translation helpful? Give feedback.
We've shipped some documentation updates that describe this in detail: https://turbo.build/repo/docs/core-concepts/internal-packages