Package Creation in Nx Workspace
Create packages in Nx workspace with proper structure and configuration.
Process
- Create directory structure - Set up package folders
- Configure package.json - Define package metadata and dependencies
- Configure project.json - Set up Nx project configuration
- Configure TypeScript - Set up tsconfig files
- Add to workspace - Update tsconfig.base.json with path alias
- Create source files - Add index.ts and lib/ structure
Package Structure
packages/[category]/[package-name]/
├── package.json
├── project.json
├── tsconfig.json
├── tsconfig.lib.json
├── tsconfig.spec.json
├── jest.config.ts
├── README.md
└── src/
├── index.ts
└── lib/
└── [implementation files]
Categories:
api/- API packagesdomains/- Domain-specific packages
Configuration Files
package.json:
{
"name": "@category/package-name",
"version": "1.0.0",
"type": "commonjs",
"main": "./src/index.ts",
"types": "./src/index.ts"
}
project.json:
{
"name": "category-package-name",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/category/package-name/src",
"projectType": "library",
"tags": ["scope:category", "type:library"],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"options": {
"outputPath": "dist/packages/category/package-name",
"main": "packages/category/package-name/src/index.ts",
"tsConfig": "packages/category/package-name/tsconfig.lib.json"
}
}
}
}
tsconfig.json:
{
"extends": "../../../tsconfig.base.json",
"references": [{ "path": "./tsconfig.lib.json" }, { "path": "./tsconfig.spec.json" }]
}
Workspace Integration
Add to tsconfig.base.json (workspace root):
{
"compilerOptions": {
"paths": {
"@category/package-name": ["packages/category/package-name/src/index.ts"]
}
}
}
Source Files
src/index.ts:
export * from "./lib";
src/lib/index.ts:
export const exampleFunction = () => {
return "Hello from package";
};
Examples
Example 1: Create API package
# Directory structure
packages/api/my-api/
├── package.json
├── project.json
├── src/
│ ├── index.ts
│ └── lib/
│ └── api/
│ └── myApi.ts
Example 2: Create domain package
# Directory structure
packages/domains/user-management/
├── package.json
├── project.json
├── src/
│ ├── index.ts
│ └── lib/
│ ├── types.ts
│ └── utils.ts
Example 3: Import from package
// After adding to tsconfig.base.json
import { exampleFunction } from "@category/package-name";
const result = exampleFunction();
Best Practices
- Use consistent naming:
@category/package-name - Add package to tsconfig.base.json paths
- Use proper Nx project configuration
- Include all required config files
- Set up proper TypeScript configuration
- Use src/index.ts as entry point
- Organize code in src/lib/ directory
- Add README.md with documentation
- Configure jest for testing
- Use proper tags in project.json