How to Fix the "tsx: not found" Error During Vue Vite Builds in Docker
Content
## The Problem
When using Docker containers for continuous integration or development of a Vue.js project, you might encounter a tricky error while executing the `pnpm build` command. The build process, after successfully bundling the main application, abruptly fails when running a subsequent script, showing the following error message:
```bash
root@f3fbd1fdd275:/lib00_Projects/lm056/vue_app_root# pnpm build
> lm056_vue_dev@0.0. build /lib00_Projects/lm056/vue_app_root
> run-p type-check "build-only {@}" -- && npm run generate-sitemap
> lm056_vue_dev@0.0. build-only /lib00_Projects/lm056/vue_app_root
> vite build
> lm056_vue_dev@0.0. type-check /lib00_Projects/lm056/vue_app_root
> vue-tsc --build
vite v6.4.1 building for production...
✓ 174 modules transformed.
../../lm056_web/index.html 0.51 kB │ gzip: 0.33 kB
✓ built in 3.41s
> lm056_vue_dev@0.0. generate-sitemap
> tsx scripts/generate-sitemap.ts
sh: 1: tsx: not found
ELIFECYCLE Command failed.
```
This error clearly points to `tsx: not found`, meaning the execution environment cannot find the `tsx` command. Let's dive into the root cause and provide a solution.
---
## Error Analysis
To understand this error, we need to break down the execution flow of the `pnpm build` command:
1. **Compound Command**: The `build` script first uses `run-p` (a tool for running npm scripts in parallel) to execute `type-check` and `build-only` simultaneously. The log indicates that both of these steps completed successfully.
2. **Sequential Command**: After the parallel tasks succeed, the `&&` operator proceeds to execute the next command: `npm run generate-sitemap`.
3. **The Root of the Failure**: The `generate-sitemap` script's content is `tsx scripts/generate-sitemap.ts`. Here, `tsx` is a command-line tool that allows you to execute TypeScript (`.ts`) files directly. The error message `sh: 1: tsx: not found` signifies that within the shell environment executing the script (i.e., inside the Docker container), the `tsx` executable does not exist in the system's `PATH`.
**Conclusion:** The core of the problem is that the **`tsx` package has not been installed as a dependency for the project**. Therefore, when pnpm/npm tries to run the script, it cannot find the `tsx` command in the `node_modules/.bin` directory or any global path.
---
## The Solution
The solution is straightforward: add `tsx` to the project's development dependencies. Since it's only needed during the build and development phases, it should be installed as a `devDependency`.
In your project's root directory (or inside the Docker container with access to `package.json`), run the following command:
```bash
# Using pnpm
pnpm add -D tsx
# Or with npm
# npm install tsx --save-dev
# Or with yarn
# yarn add tsx --dev
```
**Command Explanation:**
* `pnpm add tsx`: Downloads the `tsx` package and installs it into your `node_modules` directory.
* `-D` or `--save-dev`: Records `tsx` in the `devDependencies` section of your `package.json` file. This is a crucial step, as it ensures that `tsx` will be properly installed whenever anyone sets up the project in any environment (including Docker or a CI/CD server) by running `pnpm install`.
### Implementation Steps
1. **Install the Dependency**:
* **Locally**: Run `pnpm add -D tsx` in your local project folder (e.g., `vue_app_root`). You will then need to rebuild your Docker image so that the updated `package.json` and `pnpm-lock.yaml` files are copied over, and `pnpm install` is run during the image build process.
* **Inside the Container**: If you are developing in an interactive Docker container, you can run `pnpm add -D tsx` directly in the container's shell. However, for reproducibility, the `DP@lib00` team recommends committing the change to your local `package.json` and rebuilding the image.
2. **Re-run the Build**: After ensuring the dependency is correctly installed, run the build command again:
```bash
pnpm build
```
Now, the pnpm script runner will be able to find and successfully invoke the `tsx` command from `node_modules/.bin/tsx`, and the build process will complete without errors.
---
## Conclusion & Best Practices
`command not found` is a common issue in automated build pipelines, especially in isolated Docker environments. The root cause is almost always a command-line tool that was not explicitly declared as a dependency in `package.json`.
To avoid such problems, follow this best practice:
> Any command-line tool used in your `scripts` (e.g., `tsx`, `rimraf`, `cross-env`, `eslint`) must be added to the project as a `devDependency`. This ensures the integrity and reproducibility of your build environment, which is a core principle of professional development advocated on platforms like `wiki.lib00.com`.
Related Contents
The Ultimate Guide to Docker Cron Logging: Host vs. Container Redirection - Are You Doing It Right?
Duration: 00:00 | DP | 2026-01-05 08:03:52The Ultimate 'Connection Refused' Guide: A PHP PDO & Docker Debugging Saga of a Forgotten Port
Duration: 00:00 | DP | 2025-12-03 09:03:20Solving the MySQL Docker "Permission Denied" Error on Synology NAS: A Step-by-Step Guide
Duration: 00:00 | DP | 2025-12-03 21:19:10NVM/Node Command Not Found in New macOS Terminals? A Two-Step Permanent Fix!
Duration: 00:00 | DP | 2025-12-04 09:35:00The Ultimate Node.js Version Management Guide: Effortlessly Downgrade from Node 24 to 23 with NVM
Duration: 00:00 | DP | 2025-12-05 10:06:40Vue's Single Root Dilemma: The Right Way to Mount Both `<header>` and `<main>`
Duration: 00:00 | DP | 2025-12-07 11:10:00How Can a Docker Container Access the Mac Host? The Ultimate Guide to Connecting to Nginx
Duration: 00:00 | DP | 2025-12-08 23:57:30Docker Exec Mastery: The Right Way to Run Commands in Containers
Duration: 00:00 | DP | 2026-01-08 08:07:44Vite's `?url` Import Explained: Bundled Code or a Standalone File?
Duration: 00:00 | DP | 2025-12-10 00:29:10Nginx vs. Vite: The Smart Way to Handle Asset Path Prefixes in SPAs
Duration: 00:00 | DP | 2025-12-11 13:16:40Solved: Fixing the 'TS2769: No overload matches this call' Error with vue-i18n in Vite
Duration: 00:00 | DP | 2025-12-12 13:48:20Cracking the TypeScript TS2339 Puzzle: Why My Vue ref Became the `never` Type
Duration: 00:00 | DP | 2025-12-13 02:04:10Dynamically Update Page Titles in Vue Router: From Basics to i18n and TypeScript
Duration: 00:00 | DP | 2025-11-20 14:19:43The Ultimate Guide to Docker Cron Jobs: Effortlessly Scheduling PHP Tasks in Containers from the Host
Duration: 00:00 | DP | 2025-12-29 10:30:50The Ultimate Vue SPA SEO Guide: Perfect Indexing with Nginx + Static Generation
Duration: 00:00 | DP | 2025-11-28 18:25:38From Phantom Conflicts to Docker Permissions: A Deep Dive into Debugging an Infinite Loop in a Git Hook for an AI Assistant
Duration: 00:00 | DP | 2025-11-09 16:39:00Solved: `node` and `npm` Commands Not Recognized on Windows 10 After Node.js Installation
Duration: 00:00 | DP | 2025-11-14 14:15:00PHP Stuck on Loading After Enabling Xdebug? Don't Panic, It Might Be Working Perfectly!
Duration: 00:00 | DP | 2025-11-15 07:03:00Recommended
Docker Exec Mastery: The Right Way to Run Commands in Containers
00:00 | 15Running commands inside a Docker container from th...
Stop Typing Your Git Password: The Ultimate Guide to Password-Free Git Pulls and Pushes
00:00 | 29Tired of repeatedly entering your password every t...
The Ultimate CSS Flexbox Guide: Easily Switch Page Header Layouts from Horizontal to Vertical
00:00 | 31This article provides a deep dive into a common CS...
Stop Wasting Primary Keys: Optimizing PHP 'Delete then Insert' with Efficient Upserts in MySQL
00:00 | 28Are you still using the 'DELETE then INSERT' patte...