Building Vue Apps with the Nx Standalone Projects Setup

In this tutorial you'll learn how to use Vue with Nx in a "standalone" (non-monorepo) setup. Not to be confused with the "Vue Standalone API", a standalone project in Nx is a non-monorepo setup where you have a single application at the root level. This setup is very similar to what the Vue CLI gives you.

What are you going to learn?

  • how to create a new standalone (single-project) Nx workspace setup for Vue
  • how to run a single task (i.e. serve your app) or run multiple tasks in parallel
  • how to leverage code generators to scaffold components
  • how to modularize your codebase and impose architectural constraints for better maintainability

Note, while you could easily use Nx together with your manually set up Vue application, we're going to use the @nx/vue plugin for this tutorial which provides some nice enhancements when working with Vue. Visit our "Why Nx" page to learn more about plugins and what role they play in the Nx architecture.

Warm Up

Here's the source code of the final result for this tutorial.

Creating a new Vue App

Create a new Vue application with the following command:

~

npx create-nx-workspace@latest myvueapp --preset=vue-standalone

1 > NX Let's create a new workspace [https://nx.dev/getting-started/intro] 2 3Test runner to use for end to end (E2E) tests · cypress 4Default stylesheet format · css 5Enable distributed caching to make your CI faster · Yes 6 7 > NX Creating your v17.0.0 workspace. 8 9 To make sure the command works reliably in all environments, and that the preset is applied correctly, 10 Nx will run "npm install" several times. Please wait. 11

You can choose any test runner or stylesheet format you like. In this tutorial we're going to use Cypress and css. The above command generates the following structure:

1└─ myvueapp 2 ├─ .vscode 3 │ └─ extensions.json 4 ├─ e2e 5 │ ├─ ... 6 │ ├─ project.json 7 │ ├─ src 8 │ │ ├─ e2e 9 │ │ │ └─ app.cy.ts 10 │ │ ├─ ... 11 │ └─ tsconfig.json 12 ├─ src 13 │ ├─ app 14 │ │ ├─ App.spec.ts 15 │ │ ├─ App.vue 16 │ │ └─ NxWelcome.vue 17 │ ├─ main.ts 18 │ └─ styles.css 19 ├─ index.html 20 ├─ jest.config.ts 21 ├─ jest.preset.js 22 ├─ nx.json 23 ├─ package-lock.json 24 ├─ package.json 25 ├─ project.json 26 ├─ README.md 27 ├─ tsconfig.app.json 28 ├─ tsconfig.base.json 29 ├─ tsconfig.json 30 ├─ tsconfig.spec.json 31 └─ vite.config.ts 32

The setup includes..

  • a new Vue application at the root of the Nx workspace (src)
  • a Cypress based set of e2e tests (e2e/)
  • Prettier preconfigured
  • ESLint preconfigured
  • Vitest preconfigured

Let me explain a couple of things that might be new to you.

FileDescription
nx.jsonThis is where we fine-tune how Nx works. We define what cacheable operations there are, and configure our task pipeline. More on that soon.
project.jsonThis file contains the targets that can be invoked for the myreactapp project. It is like a more evolved version of simple package.json scripts with more metadata attached. You can read more about it here.

Serving the App

The most common tasks are already defined in the package.json file:

package.json
1{ 2 "name": "myvueapp", 3 "scripts": { 4 "start": "nx serve", 5 "build": "nx build", 6 "test": "nx test" 7 } 8 ... 9} 10

To serve your new Vue application, just run: npm start. Alternatively you can directly use Nx by using

nx serve

Your application should be served at http://localhost:4200.

Nx uses the following syntax to run tasks:

Syntax for Running Tasks in Nx

All targets, such as serve, build, test or your custom ones, are defined in the project.json file.

1{ 2 "name": "myvueapp", 3 ... 4 "targets": { 5 "lint": { ... }, 6 "build": { ... }, 7 "serve": { ... }, 8 "preview": { ... }, 9 "test": { ... }, 10 "serve-static": { ... }, 11 }, 12} 13

Each target contains a configuration object that tells Nx how to run that target.

1{ 2 "name": "myvueapp", 3 ... 4 "targets": { 5 "serve": { 6 "executor": "@nx/vite:dev-server", 7 "defaultConfiguration": "development", 8 "options": { 9 "buildTarget": "myvueapp:build" 10 }, 11 "configurations": { 12 "development": { 13 "buildTarget": "myvueapp:build:development", 14 "hmr": true 15 }, 16 "production": { 17 "buildTarget": "myvueapp:build:production", 18 "hmr": false 19 } 20 } 21 }, 22 ... 23 }, 24} 25
Nx 15 and lower use @nrwl/ instead of @nx/

The most critical parts are:

  • executor - this is of the syntax <plugin>:<executor-name>, where the plugin is an NPM package containing an Nx Plugin and <executor-name> points to a function that runs the task. In this case, the @nx/vite plugin contains the dev-server executor which serves the React app using Vite.
  • options - these are additional properties and flags passed to the executor function to customize it

Learn more about how to run tasks with Nx.

Testing and Linting - Running Multiple Tasks

Our current setup not only has targets for serving and building the Vue application, but also has targets for unit testing, e2e testing and linting. Again, these are defined in the project.json file. We can use the same syntax as before to run these tasks:

1nx test # runs tests using Jest 2nx lint # runs linting with ESLint 3nx e2e e2e # runs e2e tests with Cypress 4

More conveniently, we can also run them in parallel using the following syntax:

myvueapp

nx run-many -t test lint e2e

1 2nx run e2e:lint (1s) 3nx run myvueapp:lint (1s) 4nx run myvueapp:test (2s) 5nx run e2e:e2e (6s) 6 7 —————————————————————————————————————————————————————— 8 9 > NX Successfully ran targets test, lint, e2e for 2 projects (8s) 10

Caching

One thing to highlight is that Nx is able to cache the tasks you run.

Note that all of these targets are automatically cached by Nx. If you re-run a single one or all of them again, you'll see that the task completes immediately. In addition, (as can be seen in the output example below) there will be a note that a matching cache result was found and therefore the task was not run again.

myvueapp

nx run-many -t test lint e2e

1 2 ✔ nx run myvueapp:lint [existing outputs match the cache, left as is] 3 ✔ nx run e2e:lint [existing outputs match the cache, left as is] 4 ✔ nx run myvueapp:test [existing outputs match the cache, left as is] 5 ✔ nx run e2e:e2e [existing outputs match the cache, left as is] 6 7 ——————————————————————————————————————————————————————— 8 9 > NX Successfully ran targets test, lint, e2e for 2 projects (143ms) 10 11 Nx read the output from the cache instead of running the command for 4 out of 4 tasks. 12

Not all tasks might be cacheable though. You can configure cacheableOperations in the nx.json file. You can also learn more about how caching works.

Nx Plugins? Why?

One thing you might be curious about is the project.json. You may wonder why we define tasks inside the project.json file instead of using the package.json file with scripts that directly launch Vite.

Nx understands and supports both approaches, allowing you to define targets either in your package.json or project.json files. While both serve a similar purpose, the project.json file can be seen as an advanced form of package.json scripts, providing additional metadata and capabilities. In this tutorial, we utilize the project.json approach primarily because we take advantage of Nx Plugins.

So, what are Nx Plugins? Nx Plugins are optional packages that extend the capabilities of Nx, catering to various specific technologies. For instance, we have plugins tailored to Vue (e.g., @nx/vue), Vite (@nx/vite), Cypress (@nx/cypress), and more. These plugins offer additional features, making your development experience more efficient and enjoyable when working with specific tech stacks.

visit our "Why Nx" page for more deails.

Creating New Components

You can just create new React components as you normally would. However, Nx plugins usually also ship generators. They allow you to easily scaffold code, configuration or entire projects. To see what capabilities the @nx/vue plugin ships, run the following command and inspect the output:

myvueapp

npx nx list @nx/vue

1 2> NX Capabilities in @nx/vue: 3 4 GENERATORS 5 6 init : Initialize the `@nx/vue` plugin. 7 application : Create a Vue application. 8 library : Create a Vue library. 9 component : Create a Vue component. 10 setup-tailwind : Set up Tailwind configuration for a project. 11 storybook-configuration : Set up storybook for a Vue app or library. 12 stories : Create stories for all components declared in an app or library. 13
Nx 15 and lower use @nrwl/ instead of @nx/
Prefer a more visual UI?

If you prefer a more integrated experience, you can install the "Nx Console" extension for your code editor. It has support for VSCode, IntelliJ and ships a LSP for Vim. Nx Console provides autocompletion support in Nx configuration files and has UIs for browsing and running generators.

More info can be found in the integrate with editors article.

Run the following command to generate a new "hello-world" component. Note how we append --dry-run to first check the output.

myvueapp

npx nx g @nx/vue:component hello-world --no-export --unit-test-runner=vitest --dry-run

1> NX Generating @nx/vue:component 2 3CREATE src/components/hello-world/hello-world.vue 4CREATE src/components/hello-world/__tests__/hello-world.spec.ts 5 6NOTE: The "dryRun" flag means no changes were made. 7
Nx 15 and lower use @nrwl/ instead of @nx/

As you can see it generates a new component in the app/hello-world/ folder. If you want to actually run the generator, remove the --dry-run flag.

src/app/hello-world/hello-world.vue
1<script setup lang="ts"> 2defineProps<{}>(); 3</script> 4 5<template> 6 <p>Welcome to HelloWorld!</p> 7</template> 8 9<style scoped></style> 10

Building the App for Deployment

If you're ready and want to ship your application, you can build it using

myvueapp

npx nx build

1> nx run myvueapp:build:production 2 3vite v4.3.9 building for production... 415 modules transformed. 5dist/myvueapp/index.html 0.43 kB │ gzip: 0.29 kB 6dist/myvueapp/assets/index-5056d525.css 7.90 kB │ gzip: 1.78 kB 7dist/myvueapp/assets/index-a94ce881.js 62.46 kB │ gzip: 24.36 kB 8built in 534ms 9 10 ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 11 12 > NX Successfully ran target build for project myvueapp (1s) 13

All the required files will be placed in the dist/myvueapp folder and can be deployed to your favorite hosting provider.

You're ready to go!

In the previous sections you learned about the basics of using Nx, running tasks and navigating an Nx workspace. You're ready to ship features now!

But there's more to learn. You have two possibilities here:

Modularizing your Vue App with Local Libraries

When you develop your Vue application, usually all your logic sits in the app folder. Ideally separated by various folder names which represent your "domains". As your app grows, this becomes more and more monolithic though.

The following structure is a common example of this kind of monolithic code organization:

1└─ myvueapp 2 ├─ ... 3 ├─ src 4 │ ├─ app 5 │ │ ├─ products 6 │ │ ├─ cart 7 │ │ ├─ ui 8 │ │ ├─ ... 9 │ │ └─ App.vue 10 │ ├─ ... 11 │ └─ main.ts 12 ├─ ... 13 ├─ package.json 14 ├─ ... 15

Nx allows you to separate this logic into "local libraries". The main benefits include

  • better separation of concerns
  • better reusability
  • more explicit "APIs" between your "domain areas"
  • better scalability in CI by enabling independent test/lint/build commands for each library
  • better scalability in your teams by allowing different teams to work on separate libraries

Creating Local Libraries

Let's assume our domain areas include products, orders and some more generic design system components, called ui. We can generate a new library for each of these areas using the Vue library generator:

1nx g @nx/vue:library products --directory=modules/products --unit-test-runner=vitest --bundler=vite 2nx g @nx/vue:library orders --directory=modules/orders --unit-test-runner=vitest --bundler=vite 3nx g @nx/vue:library shared-ui --directory=modules/shared/ui --unit-test-runner=vitest --bundler=vite 4
Nx 15 and lower use @nrwl/ instead of @nx/

Note how we use the --directory flag to place the libraries into a subfolder. You can choose whatever folder structure you like, even keep all of them at the root-level.

Running the above commands should lead to the following directory structure:

1└─ myvueapp 2 ├─ ... 3 ├─ e2e/ 4 ├─ modules 5 │ ├─ products 6 │ │ ├─ .eslintrc.json 7 │ │ ├─ README.md 8 │ │ ├─ vite.config.ts 9 │ │ ├─ project.json 10 │ │ ├─ src 11 │ │ │ ├─ index.ts 12 │ │ │ ├─ components 13 │ │ │ │ ├─ products.spec.ts 14 │ │ │ │ └─ products.vue 15 │ │ │ └─ vue-shims.d.ts 16 │ │ ├─ tsconfig.json 17 │ │ ├─ tsconfig.lib.json 18 │ │ └─ tsconfig.spec.json 19 │ ├─ orders 20 │ │ ├─ ... 21 │ │ ├─ src 22 │ │ │ ├─ index.ts 23 │ │ │ ├─ components 24 │ │ │ │ ├─ ... 25 │ │ │ │ └─ orders.vue 26 │ │ ├─ ... 27 │ └─ shared 28 │ └─ ui 29 │ ├─ ... 30 │ ├─ src 31 │ │ ├─ index.ts 32 │ │ └─ components 33 │ │ └─ shared-ui.vue 34 │ └─ ... 35 ├─ ... 36 ├─ src 37 │ ├─ app 38 │ │ ├─ ... 39 │ │ ├─ App.vue 40 │ ├─ ... 41 ├─ ... 42

Each of these libraries

  • has its own project.json file with corresponding targets you can run (e.g. running tests for just orders: nx test orders)
  • has a dedicated index.ts file which is the "public API" of the library
  • is mapped in the tsconfig.base.json at the root of the workspace

Importing Libraries into the Vue Application

All libraries that we generate automatically have aliases created in the root-level tsconfig.base.json.

tsconfig.base.json
1{ 2 "compilerOptions": { 3 ... 4 "paths": { 5 "products": ["modules/products/src/index.ts"], 6 "orders": ["modules/orders/src/index.ts"], 7 "shared-ui": ["modules/shared/ui/src/index.ts"] 8 }, 9 ... 10 }, 11} 12

Hence we can easily import them into other libraries and our Vue application. For example: let's use our existing Products component in modules/products/src/components/products.vue:

modules/products/src/components/products.vue
1<script setup lang="ts"> 2defineProps<{}>(); 3</script> 4 5<template> 6 <p>Welcome to Products!</p> 7</template> 8 9<style scoped></style> 10

Make sure the Products component is exported via the index.ts file of our products library (which it should already be). The modules/products/src/index.ts file is the public API for the products library with the rest of the workspace. Only export what's really necessary to be usable outside the library itself.

modules/products/src/index.ts
1export { default as Products } from './components/products.vue'; 2

We're ready to import it into our main application now. First, let's set up the Vue Router.

npm install vue-router --legacy-peer-deps

Configure it in the main.ts file.

src/main.ts
1import './styles.css'; 2 3import { createApp } from 'vue'; 4import App from './app/App.vue'; 5import NxWelcome from './app/NxWelcome.vue'; 6import * as VueRouter from 'vue-router'; 7 8const routes = [ 9 { path: '/', component: NxWelcome }, 10 { 11 path: '/products', 12 component: () => import('products').then((m) => m.Products), 13 }, 14]; 15 16const router = VueRouter.createRouter({ 17 history: VueRouter.createWebHashHistory(), 18 routes, 19}); 20 21const app = createApp(App); 22 23app.use(router); 24app.mount('#root'); 25

Then we can set up navigation links and the RouterView in the main App component.

src/App.vue
1<script setup lang="ts"> 2import { RouterLink, RouterView } from 'vue-router'; 3</script> 4 5<template> 6 <nav> 7 <ul> 8 <li> 9 <RouterLink to="/">Home</RouterLink> 10 </li> 11 <li> 12 <RouterLink to="/products">Products</RouterLink> 13 </li> 14 </ul> 15 </nav> 16 17 <RouterView /> 18</template> 19

If you now navigate to http://localhost:4200/products you should see the Products component being rendered.

Browser screenshot of navigating to the products route

Let's do the same process for our orders library. Import the Orders component into the main.ts routes:

src/app/main.ts
1import './styles.css'; 2 3import { createApp } from 'vue'; 4import App from './app/App.vue'; 5import NxWelcome from './app/NxWelcome.vue'; 6import * as VueRouter from 'vue-router'; 7 8const routes = [ 9 { path: '/', component: NxWelcome }, 10 { 11 path: '/products', 12 component: () => import('products').then((m) => m.Products), 13 }, 14 { path: '/orders', component: () => import('orders').then((m) => m.Orders) }, 15]; 16 17const router = VueRouter.createRouter({ 18 history: VueRouter.createWebHashHistory(), 19 routes, 20}); 21 22const app = createApp(App); 23 24app.use(router); 25app.mount('#root'); 26

And update the navigation links:

src/App.vue
1<script setup lang="ts"> 2import { RouterLink, RouterView } from 'vue-router'; 3</script> 4 5<template> 6 <nav> 7 <ul> 8 <li> 9 <RouterLink to="/">Home</RouterLink> 10 </li> 11 <li> 12 <RouterLink to="/products">Products</RouterLink> 13 </li> 14 <li> 15 <RouterLink to="/orders">Orders</RouterLink> 16 </li> 17 </ul> 18 </nav> 19 20 <RouterView /> 21</template> 22

Similarly, navigating to http://localhost:4200/orders should now render the Orders component.

Note that both the Products component and Orders component are lazy loaded so the initial bundle size will be smaller.

Visualizing your Project Structure

Nx automatically detects the dependencies between the various parts of your workspace and builds a project graph. This graph is used by Nx to perform various optimizations such as determining the correct order of execution when running tasks like nx build, identifying affected projects and more. Interestingly you can also visualize it.

Just run:

nx graph

You should be able to see something similar to the following in your browser (hint: click the "Show all projects" button).

Loading...

Notice how shared-ui is not yet connected to anything because we didn't import it in any of our projects. Also the arrows to orders and products are dashed because we're using lazy imports.

Exercise for you: change the codebase so that shared-ui is used by orders and products. Note: you need to restart the nx graph command to update the graph visualization or run the CLI command with the --watch flag.

Imposing Constraints with Module Boundary Rules

Once you modularize your codebase you want to make sure that the modules are not coupled to each other in an uncontrolled way. Here are some examples of how we might want to guard our small demo workspace:

  • we might want to allow orders to import from shared-ui but not the other way around
  • we might want to allow orders to import from products but not the other way around
  • we might want to allow all libraries to import the shared-ui components, but not the other way around

When building these kinds of constraints you usually have two dimensions:

  • type of project: what is the type of your library. Example: "feature" library, "utility" library, "data-access" library, "ui" library (see library types)
  • scope (domain) of the project: what domain area is covered by the project. Example: "orders", "products", "shared" ... this really depends on the type of product you're developing

Nx comes with a generic mechanism that allows you to assign "tags" to projects. "tags" are arbitrary strings you can assign to a project that can be used later when defining boundaries between projects. For example, go to the project.json of your orders library and assign the tags type:feature and scope:orders to it.

modules/orders/project.json
1{ 2 ... 3 "tags": ["type:feature", "scope:orders"], 4 ... 5} 6

Then go to the project.json of your products library and assign the tags type:feature and scope:products to it.

modules/products/project.json
1{ 2 ... 3 "tags": ["type:feature", "scope:products"], 4 ... 5} 6

Finally, go to the project.json of the shared-ui library and assign the tags type:ui and scope:shared to it.

modules/shared/ui/project.json
1{ 2 ... 3 "tags": ["type:ui", "scope:shared"], 4 ... 5} 6

Notice how we assign scope:shared to our UI library because it is intended to be used throughout the workspace.

Next, let's come up with a set of rules based on these tags:

  • type:feature should be able to import from type:feature and type:ui
  • type:ui should only be able to import from type:ui
  • scope:orders should be able to import from scope:orders, scope:shared and scope:products
  • scope:products should be able to import from scope:products and scope:shared

To enforce the rules, Nx ships with a custom ESLint rule. Open the .eslintrc.base.json at the root of the workspace and add the following depConstraints in the @nx/enforce-module-boundaries rule configuration:

.eslintrc.base.json
1{ 2 ... 3 "overrides": [ 4 { 5 ... 6 "rules": { 7 "@nx/enforce-module-boundaries": [ 8 "error", 9 { 10 "enforceBuildableLibDependency": true, 11 "allow": [], 12 "depConstraints": [ 13 { 14 "sourceTag": "*", 15 "onlyDependOnLibsWithTags": ["*"] 16 }, 17 { 18 "sourceTag": "type:feature", 19 "onlyDependOnLibsWithTags": ["type:feature", "type:ui"] 20 }, 21 { 22 "sourceTag": "type:ui", 23 "onlyDependOnLibsWithTags": ["type:ui"] 24 }, 25 { 26 "sourceTag": "scope:orders", 27 "onlyDependOnLibsWithTags": [ 28 "scope:orders", 29 "scope:products", 30 "scope:shared" 31 ] 32 }, 33 { 34 "sourceTag": "scope:products", 35 "onlyDependOnLibsWithTags": ["scope:products", "scope:shared"] 36 }, 37 { 38 "sourceTag": "scope:shared", 39 "onlyDependOnLibsWithTags": ["scope:shared"] 40 } 41 ] 42 } 43 ] 44 } 45 }, 46 ... 47 ] 48} 49
Nx 15 and lower use @nrwl/ instead of @nx/

To test it, go to your modules/products/src/components/products.vue file and import the Orders component from the orders project:

modules/products/src/components/products.vue
1<script setup lang="ts"> 2defineProps<{}>(); 3 4// 👇 this import is not allowed 5import { Orders } from 'orders'; 6</script> 7 8<template> 9 <p>Welcome to Products!</p> 10</template> 11 12<style scoped></style> 13

If you lint your workspace you'll get an error now:

~/workspace

nx run-many -t lint

1 ✖ nx run products:lint 2 Linting "products"... 3 4 /Users/isaac/Documents/code/nx-recipes/vue-standalone/modules/products/src/components/products.vue 5 5:1 error A project tagged with "scope:products" can only depend on libs tagged with "scope:products", "scope:shared" @nx/enforce-module-boundaries 6 7 ✖ 1 problem (1 error, 0 warnings) 8 9 Lint errors found in the listed files. 10 11 ✔ nx run orders:lint (1s) 12 ✔ nx run myvueapp:lint (1s) 13 ✔ nx run e2e:lint (682ms) 14 ✔ nx run shared-ui:lint (797ms) 15 16 ————————————————————————————————————————————————————————————————————— 17 18 > NX Ran target lint for 5 projects (2s) 19 20 ✔ 4/5 succeeded [0 read from cache] 21 22 ✖ 1/5 targets failed, including the following: 23 - nx run products:lint 24 25
Nx 15 and lower use @nrwl/ instead of @nx/

If you have the ESLint plugin installed in your IDE you should immediately see an error:

ESLint module boundary error

Learn more about how to enforce module boundaries.

Next Steps

Congrats, you made it!! You now know how to leverage the Nx standalone applications preset to build modular Vue applications.

Here's some more things you can dive into next:

Also, make sure you