How npm run Executes Scripts and Uses node_modules/.bin in Vue CLI Projects
The article explains, through a mock interview, why running npm run xxx triggers npm to look up the script in package.json, creates local binary links in node_modules/.bin, and executes the corresponding command such as vue‑cli‑service serve without requiring a global installation.
The piece is framed as a mock interview where the candidate is asked what actually happens when the command npm run xxx is executed, leading to a detailed technical explanation.
npm first reads the project's package.json to find a matching entry in the scripts section; for example, npm run serve maps to the command vue-cli-service serve .
The reason the raw binary vue-cli-service cannot be run directly is that it is not a globally installed executable. During npm i , npm creates a symlink (or wrapper) for each package's declared binary inside node_modules/.bin , and automatically adds this directory to the process $PATH .
When npm run serve is invoked, npm resolves the script to vue-cli-service serve and actually runs ./node_modules/.bin/vue-cli-service serve . This works even without a global installation because the local .bin link points to the package's executable script.
If a package is installed globally with npm install -g xxx , its binary is linked to a global node_modules/.bin directory, making the command available system‑wide.
<code>@ECHO off
GOTO start
:find_dp0
SET dp0=%~dp0
EXIT /b
:start
SETLOCAL
CALL :find_dp0
IF EXIST "%dp0%\node.exe" (
SET "_prog=%dp0%\node.exe"
) ELSE (
SET "_prog=node"
SET PATHEXT=%PATHEXT:;.JS;=;%
)
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\..\@vue\cli-service\bin\vue-cli-service.js" %*
</code>On Windows the .cmd wrapper ( vue-cli-service.cmd ) ultimately calls node to execute vue-cli-service.js , while on Unix‑like systems the plain vue-cli-service binary is used.
Running npm run xxx makes npm search node_modules/.bin for the executable and runs it if found.
If not found locally, npm falls back to the global node_modules/.bin (installed with -g ).
When neither location contains the binary, the system PATH is consulted for a matching command.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.