You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/shared/migration/adding-to-existing-project.md
+96-61Lines changed: 96 additions & 61 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,7 @@
1
1
# Adding Nx to your Existing Project
2
2
3
-
Nx can be added to any type of project, not just monorepos. The main benefit is to get caching abilities for the package scripts. Each project usually has a set of scripts in the `package.json`:
3
+
Nx can be added to any type of project, not just monorepos. The main benefit is to get caching abilities for the package
4
+
scripts. Each project usually has a set of scripts in the `package.json`:
4
5
5
6
```json {% fileName="package.json" %}
6
7
{
@@ -16,7 +17,8 @@ Nx can be added to any type of project, not just monorepos. The main benefit is
16
17
You can make these scripts faster by leveraging Nx's caching capabilities. For example:
17
18
18
19
- You change some spec files: in that case the `build` task can be cached and doesn't have to re-run.
19
-
- You update your docs, changing a couple of markdown files: then there's no need to re-run builds, tests, linting on your CI. All you might want to do is trigger the Docusaurus build.
20
+
- You update your docs, changing a couple of markdown files: then there's no need to re-run builds, tests, linting on
21
+
your CI. All you might want to do is trigger the Docusaurus build.
20
22
21
23
## Install Nx on a Non-Monorepo Project
22
24
@@ -26,21 +28,69 @@ Run the following command:
26
28
npx nx@latest init
27
29
```
28
30
29
-
This will set up Nx for you - updating the `package.json` file and creating a new `nx.json` file with Nx configuration based on your answers during the set up process. The set up process will suggest installing Nx plugins that might be useful based on your existing repository. The example below is using the `@nx/eslint` and `@nx/next` plugins to run ESLint and Next.js tasks with Nx:
31
+
Running this command will ask you a few questions about your workspace and then set up Nx for you accordingly. The setup
32
+
process detects tools which are used in your workspace and suggests installing Nx plugins to integrate the tools you use
33
+
with Nx. Running those tools through Nx will have caching enabled when possible, providing you with a faster alternative
34
+
for running those tools. You can start with a few to see how it works and then add more with
35
+
the [`nx add`](/nx-api/nx/documents/add) command later. You can also decide to add them all and get the full experience
36
+
right
37
+
away because adding plugins will not break your existing workflow.
38
+
39
+
The first thing you may notice is that Nx updates your `package.json` scripts during the setup process. Nx Plugins setup
40
+
Nx commands which run the underlying tool with caching enabled. When a `package.json` script runs a command which can be
41
+
run through Nx, Nx will replace that script in the `package.json` scripts with an Nx command that has
42
+
caching automatically enabled. Anywhere those `package.json` scripts are used, including your CI, will become faster
43
+
when possible. Let's go through an example where the `@nx/next/plugin` and `@nx/eslint/plugin` plugins are added to a
The `@nx/next/plugin` plugin adds a `next:build` target which runs `next build` and sets up caching correctly. In other
63
+
words, running `nx next:build` is the same as running `next build` with the added benefit of it being cacheable. Hence,
64
+
Nx replaces `next build` in the `package.json``build` script to add caching to anywhere running `npm run build`.
65
+
Similarly, `@nx/eslint/plugin` sets up the `nx eslint:lint` command to run `eslint ./src` with caching enabled.
66
+
The `test` script was not recognized by any Nx plugin, so it was left as is. After Nx has been setup,
67
+
running `npm run build` or `npm run lint` multiple times, will be instant when possible.
68
+
69
+
You can also run any npm scripts directly through Nx with `nx build` or `nx lint` which will run the `npm run build`
70
+
and `npm run lint` scripts respectively. In the later portion of the setup flow, Nx will ask if you would like some of
71
+
those npm scripts to be cacheable. By making those cacheable, running `nx build` rather than `npm run build` will add
72
+
another layer of cacheability. However, `nx build` must be run instead of `npm run build` to take advantage of the
73
+
cache.
74
+
75
+
## Inferred Tasks
76
+
77
+
You may have noticed that `@nx/next` provides `dev` and `start` tasks in addition to the `next:build` task. Those tasks
78
+
were created by the `@nx/next/plugin` plugin from your existing Next.js configuration. You can see the configuration for
79
+
the Nx Plugins in `nx.json`:
30
80
31
81
```json {% fileName="nx.json" %}
32
82
{
33
83
"plugins": [
34
84
{
35
85
"plugin": "@nx/eslint/plugin",
36
86
"options": {
37
-
"targetName": "lint"
87
+
"targetName": "eslint:lint"
38
88
}
39
89
},
40
90
{
41
91
"plugin": "@nx/next/plugin",
42
92
"options": {
43
-
"buildTargetName": "build",
93
+
"buildTargetName": "next:build",
44
94
"devTargetName": "dev",
45
95
"startTargetName": "start"
46
96
}
@@ -49,33 +99,11 @@ This will set up Nx for you - updating the `package.json` file and creating a ne
49
99
}
50
100
```
51
101
52
-
When Nx updates your `package.json` scripts, it looks for scripts that can be replaced with an Nx command that has caching automatically enabled. The `package.json` defined above would be updated to look like this:
53
-
54
-
```json {% fileName="package.json" %}
55
-
{
56
-
"name": "my-workspace",
57
-
...
58
-
"scripts": {
59
-
"build": "nx build",
60
-
"lint": "nx lint",
61
-
"test": "node ./run-tests.js"
62
-
},
63
-
...
64
-
"nx": {
65
-
"includedScripts": []
66
-
}
67
-
}
68
-
```
69
-
70
-
The `@nx/next` plugin can run `next build` for you and set up caching correctly, so it replaces `next build` with `nx build`. Similarly, `@nx/eslint` can set up caching for `eslint ./src`. When you run `npm run build` or `npm run lint` multiple times, you'll see that caching is enabled. You can also call Nx directly from the terminal with `nx build` or `nx lint`.
71
-
72
-
The `test` script was not recognized by any Nx plugin, so it was left as is.
73
-
74
-
The `includedScripts` array allows you to specify `package.json` scripts that can be run with the `nx build` syntax.
75
-
76
-
## Inferred Tasks
102
+
Each plugin can accept options to customize the projects which they create. You can see more information about
103
+
configuring the plugins on the [`@nx/next/plugin`](/nx-api/next) and [`@nx/eslint/plugin`](/nx-api/eslint) plugin pages.
77
104
78
-
You may have noticed that `@nx/next` provides `dev` and `start` tasks in addition to the `build` task. Those tasks were created by the `@nx/next` plugin from your existing Next.js configuration. To view all available tasks, open the Project Details view with Nx Console or use the terminal to launch the project details in a browser window.
105
+
To view all available tasks, open the Project Details view with Nx Console or use the terminal to launch the project
106
+
details in a browser window.
79
107
80
108
```shell
81
109
nx show project my-workspace --web
@@ -90,7 +118,7 @@ nx show project my-workspace --web
90
118
"data": {
91
119
"root": ".",
92
120
"targets": {
93
-
"lint": {
121
+
"eslint:lint": {
94
122
"cache": true,
95
123
"options": {
96
124
"cwd": ".",
@@ -107,7 +135,7 @@ nx show project my-workspace --web
107
135
"executor": "nx:run-commands",
108
136
"configurations": {}
109
137
},
110
-
"build": {
138
+
"next:build": {
111
139
"options": {
112
140
"cwd": ".",
113
141
"command": "next build"
@@ -146,28 +174,27 @@ nx show project my-workspace --web
@@ -203,52 +229,61 @@ nx show project my-workspace --web
203
229
204
230
{% /project-details %}
205
231
206
-
The project detail view lists all available tasks, the configuration values for those tasks and where those configuration values are being set.
232
+
The project detail view lists all available tasks, the configuration values for those tasks and where those
233
+
configuration values are being set.
207
234
208
235
## Configure an Existing Script to Run with Nx
209
236
210
237
If you want to run one of your existing scripts with Nx, you need to tell Nx about it.
211
238
212
239
1. Preface the script with `nx exec -- ` to have `npm run test` invoke the command with Nx.
213
-
2. Add the script to `includedScripts`.
214
-
3. Define caching settings.
240
+
2. Define caching settings.
215
241
216
-
The `nx exec` command allows you to keep using `npm test` or `npm run test` (or other package manager's alternatives) as you're accustomed to. But still get the benefits of making those operations cacheable. Configuring the `test` script from the example above to run with Nx would look something like this:
242
+
The `nx exec` command allows you to keep using `npm test` or `npm run test` (or other package manager's alternatives) as
243
+
you're accustomed to. But still get the benefits of making those operations cacheable. Configuring the `test` script
244
+
from the example above to run with Nx would look something like this:
217
245
218
246
```json {% fileName="package.json" %}
219
247
{
220
248
"name": "my-workspace",
221
249
...
222
250
"scripts": {
223
-
"build": "nx build",
224
-
"lint": "nx lint",
251
+
"build": "nx next:build",
252
+
"lint": "nx eslint:lint",
225
253
"test": "nx exec -- node ./run-tests.js"
226
254
},
227
255
...
228
256
"nx": {
229
-
"includedScripts": ["test"],
230
257
"targets": {
231
258
"test": {
232
259
"cache": "true",
233
-
"inputs": ["default", "^default"],
260
+
"inputs": [
261
+
"default",
262
+
"^default"
263
+
],
234
264
"outputs": []
235
265
}
236
266
}
237
267
}
238
268
}
239
269
```
240
270
241
-
Now if you run `npm run test` or `nx test` twice, the results will be retrieved from the cache. The `inputs` used in this example are as cautious as possible, so you can significantly improve the value of the cache by [customizing Nx Inputs](/recipes/running-tasks/configure-inputs) for each task.
271
+
Now if you run `npm run test` or `nx test` twice, the results will be retrieved from the cache. The `inputs` used in
272
+
this example are as cautious as possible, so you can significantly improve the value of the cache
273
+
by [customizing Nx Inputs](/recipes/running-tasks/configure-inputs) for each task.
242
274
243
275
## Learn More
244
276
245
277
{% cards %}
246
278
247
-
{% card title="Customizing Inputs and Named Inputs" description="Learn more about how to fine-tune caching with custom inputs" type="documentation" url="/recipes/running-tasks/configure-inputs" /%}
279
+
{% card title="Customizing Inputs and Named Inputs" description="Learn more about how to fine-tune caching with custom
{% card title="Cache Task Results" description="Learn more about how caching works" type="documentation" url="/features/cache-task-results" /%}
282
+
{% card title="Cache Task Results" description="Learn more about how caching works" type="documentation" url="
283
+
/features/cache-task-results" /%}
250
284
251
-
{% card title="Adding Nx to NPM/Yarn/PNPM Workspace" description="Learn more about how to add Nx to an existing monorepo" type="documentation" url="/recipes/adopting-nx/adding-to-monorepo" /%}
285
+
{% card title="Adding Nx to NPM/Yarn/PNPM Workspace" description="Learn more about how to add Nx to an existing
0 commit comments