-
Notifications
You must be signed in to change notification settings - Fork 0
Nx project setup guide
- Node.js (LTS version)
- Run
npx create-nx-workspace@latestand fill in the prompts:- enter a workspace name (typically same as repository and references organization and/or product)
- select a workspace with a single Angular app
- select a name for your application (more specific than workspace name, e.g.
clientorwebsite) - select
scssstylesheet format
- Once dependencies have been installed and workspace has been created, navigate to workspace directory (or open it in your IDE). Try running Angular app with
ng serveorng serve <app-name>and visit http://localhost:4200/ in your browser. - Optionally, you may also verify production build by running
ng buildorng build <app-name>and then previewing resulting static assets vianpx lite-server --baseDir=dist/apps/<app-name>.
To ensure strict type-checking across your entire workspace, add the following flags to the root tsconfig.base.json:
{
// ...
"compilerOptions": {
// ...
"strict": true,
"noUncheckedIndexedAccess": true,
"noPropertyAccessFromIndexSignature": true,
},
}- Change
npmScopeinnx.jsonwith your workspace-wide prefix (Nx uses it for TypeScript path aliases in new libraries). - Nx assumes a name for your default branch. If you wish to use another default branch name, then modify
affected.defaultBaseinnx.jsonaccordingly (so thatnx affectedcommands work properly).
- Replace contents of
.prettierrcwith company-standard .prettierrc. - Install Prettier plugins with
npm i -D @trivago/prettier-plugin-sort-imports. - In
.prettierrc, replace@fu-prefix withnpmScopefromnx.json. - Apply new formatting rules to workspace files using
npx nx format --all.
Tip: If you like, you can configure your IDE to automatically format saved files with Prettier (see instructions for VS Code, WebStorm etc.).
-
Replace contents of
.eslintrc.jsonwith company-standard .eslintrc.json. -
Remove
overridessection fromapps/<project>/.eslintrc.json, and add**/*.spec.ts,src/test-setup.ts(and**/*.stories.tsif using Storybook) toignorePatterns. It's contents should then look something like:{ "extends": ["../../.eslintrc.json"], "ignorePatterns": [ "!**/*", "**/*.spec.ts", "**/*.stories.ts", "src/test-setup.ts" ] } -
In root
.eslintrc.json, replace"fu"in prefix rules with your project's prefix (typically 2-3 lowercase characters). -
If you're not using NgRx in your project, remove
rulesprefixed byngrx/and remove"ngrx"frompluginsin root.eslintrc.json. -
Install additional ESLint plugins with
npm i -D eslint-plugin-{functional,rxjs,import,ngrx,max-params-no-constructor} eslint-import-resolver-typescript(skipeslint-plugin-ngrxif not applicable - see previous point). -
Running
npx nx affected:lintshould now result in new lint errors based on the new rules. Fix these issues to make linter succeed.Expand to see tips on fixing lint errors (for Nx 14)
- Rename
AppComponent's selector inapps/<app>/src/app/app.component.tsandapps/<app>/src/index.htmlto match your chosen prefix. - Add
changeDetection: ChangeDetectionStrategy.OnPushtoAppComponent's decorator metadata inapps/<app>/src/app/app.component.ts. - Add
// eslint-disable-next-line import/no-unassigned-importcomment beforeimport 'zone.js';inapps/<app>/src/polyfills.ts. - Fix
@typescript-eslint/no-confusing-void-expressionerror in.catch-method inapps/<app>/src/main.tsby putting function body in curly braces (err => { console.error(err); }). - In
apps/<app>-e2e/src/support/commands.ts, disable@typescript-eslint/consistent-type-definitionsrule via comment for line withChainableinterface declaration, convert method signatures to function properties, and replaceconsole.logwithconsole.info. - In
apps/<app>-e2e/src/support/e2e.ts, disableimport/no-unassigned-importvia comment for line withimport './commands';. - In
apps/<app>-e2e/.eslintrc.json, add new item inoverridesarray item for*.tsfiles -"rules": { "@typescript-eslint/explicit-function-return-type": "off", "import/no-default-export": "off", "import/no-extraneous-dependencies": "off" }.
Expand to see tips on fixing lint errors (for Nx 13)
- Rename
AppComponent's selector inapps/<app>/src/app/app.component.tsandapps/<app>/src/index.htmlto match your chosen prefix. - Add
changeDetection: ChangeDetectionStrategy.OnPushtoAppComponent's decorator metadata inapps/<app>/src/app/app.component.ts. - In
apps/<app>-e2e/src/support/commands.ts, disable@typescript-eslint/consistent-type-definitionsrule via comment for line withChainableinterface declaration, convert method signatures to function properties, and replaceconsole.logwithconsole.info. - In
apps/<app>-e2e/.eslintrc.json, add new item tooverridesarray -{ "files": ["*.po.ts"], "rules": { "@typescript-eslint/explicit-function-return-type": "off" } },.
Expand to see tips on fixing lint errors (for Nx 12)
- Rename
AppComponent's selector inapps/<app>/src/app/app.component.tsandapps/<app>/src/index.htmlto match your chosen prefix. - Add
changeDetection: ChangeDetectionStrategy.OnPushtoAppComponent's decorator metadata inapps/<app>/src/app/app.component.ts. - In
apps/<app>-e2e/src/support/commands.ts, removeCypressnamespace declaration and replaceconsole.logwithconsole.info.
- Rename
Tip: If you intend to organize your workspace using Nx libraries (e.g. to reuse code across multiple apps, publish them to NPM/GPR, or just encourage a more modular architecture), consider using tags (defined in
nx.json) as the base for dependency graph constraints (defined in root.eslintrc.json) - see Nx docs for more info.
To automatically set OnPush change detection strategy when generating new Angular components (and optionally prevent automatically creating *.component.spec.ts file), extend the @nrwl/angular:component schematic in angular.json:
{
// ...
"schematics": {
// ...
"@nrwl/angular:component": {
"style": "scss",
"changeDetection": "OnPush",
"skipTests": true
}
}
}If you wish to automatically trigger actions (e.g. format, lint, test) based on locally executed Git commands (e.g. commit, push), integrating Husky allows you to share that configuration with the rest of your team.
- Install Husky with:
npm i -D husky
- Set up
prepareNPM script to ensure Husky gets installed:npm set-script prepare "husky install" # if your NPM version is older than 7, replace npm with npx npm@7 npm run prepare
- Add a husky Git hook:
- E.g. to format files on each commit:
npx husky add .husky/pre-commit 'FILES=$(git diff --name-only --cached --diff-filter=d)' npx husky add .husky/pre-commit 'npx prettier --write --ignore-unknown $FILES' npx husky add .husky/pre-commit 'git add $FILES'
- E.g. to format files on each commit:
Expand to see modified instructions if Nx workspace is a sub-directory within Git repo
Let's say your Git repository places your Nx workspace inside a Frontend directory...
- Set up
prepareNPM script to ensure Husky gets installed:npm set-script prepare "cd .. && husky install Frontend/.husky" # if your NPM version is older than 7, replace npm with npx npm@7 npm run prepare
- Add a husky Git hook:
- E.g. to format files on each commit:
npx husky add .husky/pre-commit "cd Frontend" npx husky add .husky/pre-commit "CHANGED_FILES=$(git diff --name-only --cached --diff-filter=d --relative)" npx husky add .husky/pre-commit "FORMATTED_FILES=$(npx prettier --write --ignore-unknown --no-error-on-unmatched-pattern --list-different $CHANGED_FILES)" npx husky add .husky/pre-commit "echo Formatted files: $FORMATTED_FILES" npx husky add .husky/pre-commit "git add $FORMATTED_FILES"
- E.g. to format files on each commit: