How to Fix the "tsx: not found" Error During Vue Vite Builds in Docker

Published: 2026-01-10
Author: DP
Views: 11
Category: Vue
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