Adding packages
Learn how to add packages to your Turborepo workspace.
Advanced topic
This is an advanced topic - you should only follow these instructions if you are sure you want to add a new package to your TurboStarter application instead of adding a folder to your application in apps/web
or modify existing packages under packages
. You don't need to do this to add a new screen or component to your application.
To add a new package to your TurboStarter application, you need to follow these steps:
Generate a new package
First, enter the command below to create a new package in your TurboStarter application:
Turborepo will ask you to enter the name of the package you want to create. Enter the name of the package you want to create and press enter.
If you don't want to add dependencies to your package, you can skip this step by pressing enter.
The command will have generated a new package under packages named @turbostarter/<package-name>
. If you named it example
, the package will be named @turbostarter/example
.
Export a module from your package
By default, the package exports a single module using the index.ts
file. You can add more exports by creating new files in the package directory and exporting them from the index.ts
file or creating export files in the package directory and adding them to the exports
field in the package.json
file.
From index.ts
file
The easiest way to export a module from a package is to create a new file in the package directory and export it from the index.ts
file.
Then, export the module from the index.ts
file.
From exports
field in package.json
This can be very useful for tree-shaking. Assuming you have a file named module.ts
in the package directory, you can export it by adding it to the exports
field in the package.json
file.
When to do this?
- when exporting two modules that don't share dependencies to ensure better tree-shaking. For example, if your exports contains both client and server modules.
- for better organization of your package
For example, create two exports client
and server
in the package directory and add them to the exports
field in the package.json
file.
- The
client
module can be imported usingimport { client } from '@turbostarter/example/client'
- The
server
module can be imported usingimport { server } from '@turbostarter/example/server'
Use the package in your application
You can now use the package in your application by importing it using the package name:
Et voilà! You have successfully added a new package to your TurboStarter application. 🎉
Last updated on