Table of Contents
The thing is like this, let’s talk directly
总结
Home headlines Interviewer: What happened when npm run xxx? Please start speaking!

Interviewer: What happened when npm run xxx? Please start speaking!

Apr 13, 2022 pm 05:28 PM
node npm

When the interviewer asks you what happened when you run npm run xxx, how do you answer? The following article will share with you an interview experience and see how the author answered. I hope it will be helpful to you!

The thing is like this, let’s talk directly

Interviewer: What happened when npm run xxx? The more detailed it is, the better.

Me (thinking, simple): First, DNS resolution, resolve the domain name into an IP address, and then TCP connection, TCP three-way handshake...

Interviewer: Stop, I’m not asking what happens from URL input to page display? , what happened when npm run xxx.

Interviewer: What happened when npm run xxx? Please start speaking!

Me (embarrassed, reflexively thought it was a question): emmmm, I remember that when npm run xxx, I will first go to the package.json file of the project. Find the corresponding xxx in scripts, and then execute the command of xxx. For example, when starting the vue project npm run serve, the command vue-cli-service serve is actually executed. (Good luck, luckily I still understand this bit of common sense)

package.json file

{
  "name": "h5",
  "version": "1.0.7",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve"
   },
}
Copy after login

Interviewer: Well, not bad, then why not execute it directlyvue-cli-service serveBut what about executing npm run serve?

Me (hesitating): emm, because npm run serve is shorter and easier to write.

Interviewer: What happened when npm run xxx? Please start speaking!

Interviewer: Think again.

Me (ah? Isn’t that right? Yes, I remembered it): Because if I execute vue-cli-service serve directly, an error will be reported because does not exist in the operating system. vue-cli-serviceThis instruction

Interviewer: What happened when npm run xxx? Please start speaking!

Interviewer: What happened when npm run xxx? Please start speaking!

Interviewer: Oh, yes, yes, yes, yes, yes, yo yo West!

Me (hehe, steady, I want 30k this time): Hee hee!

Interviewer: What happened when npm run xxx? Please start speaking!

Interviewer: Since vue-cli-service this command does not exist in the operating system, why execute npm run serve## When #, it is equivalent to executing vue-cli-service serve. Why can it succeed and not report an error that the command does not exist?

Me (Huh? Why don't you just give me a shark, I don't want to force myself to answer): Sorry, I haven't understood this yet.

Interviewer: emmm, okay, it doesn’t matter, let’s do the next algorithm question:....

....

The following is not relevant to this article The content has been omitted.

Interviewer: Okay, this is the end of the interview. We will reply to your interview results within a week

Beep beep beep...(hang up the phone)

well. It seems to be cool

Why does it succeed when executing

npm run serve and does not report an error that the command does not exist?

I quickly asked my big boss friend what happened in this process

Interviewer: What happened when npm run xxx? Please start speaking!

After some discussion, I finally found the answer.

Unwilling to admit defeat, I quickly called back the interviewer’s phone number.

Me: Hello, interviewer, I have found the answer. Can you please listen to it again?

Interviewer: Yes, yes, please speak.

Me: When we install dependencies, we execute them through npm i xxx. For example,

npm i @vue/cli-service, when npm installs this dependency, it will Several executable files named vue-cli-service have been created in the node_modules/.bin/ directory.

Interviewer: What happened when npm run xxx? Please start speaking!

Interviewer: What happened when npm run xxx? Please start speaking!

.bin directory, this directory is not any npm package. The files in the directory indicate that they are soft links. When you open the file, you can see

#!/bin/sh written at the top of the file, indicating that it is a script.

From this we can know that when using

npm run serve to execute vue-cli-service serve, although vue-cli-service# is not installed ## global command, but npm will find the vue-cli-service file in ./node_modules/.bin and execute it as a script, which is equivalent to executing . /node_modules/.bin/vue-cli-service serve (the last serve is passed in as a parameter). <p>面试官:可以啊,真不错,但是我还想继续问问,你说.bin 目录下的文件表示软连接,那这个bin目录下的那些软连接文件是哪里来的呢?它又是怎么知道这条软连接是执行哪里的呢?</p><p>我(窃喜,这个我们刚刚也讨论了):我们可以直接在新建的vue项目里面搜索vue-cli-service</p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/image/436/923/317/1649990074656960.png" class="lazy" title="164999006926942Interviewer: What happened when npm run xxx? Please start speaking!" alt="Interviewer: What happened when npm run xxx? Please start speaking!"/></p><p>可以看到,它存在项目最外层的<strong>package-lock.json</strong>文件中</p><p>从 package-lock.json 中可知,当我们npm i 整个新建的vue项目的时候,npm 将 bin/vue-cli-service.js 作为 bin 声明了。</p><p>所以在 npm install 时,npm 读到该配置后,就将该文件软链接到 ./node_modules/.bin 目录下,而 npm 还会自动把node_modules/.bin加入$PATH,这样就可以直接作为命令运行依赖程序和开发依赖程序,不用全局安装了。</p><p>假如我们在安装包时,使用 <code>npm install -g xxx 来安装,那么会将其中的 bin 文件加入到全局,比如 create-react-app 和 vue-cli ,在全局安装后,就可以直接使用如 vue-cli projectName 这样的命令来创建项目了。

面试官:搜噶,也就是说,npm i 的时候,npm 就帮我们把这种软连接配置好了,其实这种软连接相当于一种映射,执行npm run xxx 的时候,就会到 node_modules/bin中找对应的映射文件,然后再找到相应的js文件来执行。

我(疯狂点头):嗯嗯,是的,就是这样

面试官:我有点好奇。刚刚看到在node_modules/bin中 有三个vue-cli-service文件。为什么会有三个文件呢?

Interviewer: What happened when npm run xxx? Please start speaking!

我:如果我们在 cmd 里运行的时候,windows 一般是调用了 vue-cli-service.cmd,这个文件,这是 windows 下的批处理脚本:

@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" %*
Copy after login

所以当我们运行vue-cli-service serve这条命令的时候,就相当于运行 node_modules/.bin/vue-cli-service.cmd serve

然后这个脚本会使用 node 去运行 vue-cli-service.js这个 js 文件

由于 node 中可以使用一系列系统相关的 api ,所以在这个 js 中可以做很多事情,例如读取并分析运行这条命令的目录下的文件,根据模板生成文件等。

# unix 系默认的可执行文件,必须输入完整文件名
vue-cli-service

# windows cmd 中默认的可执行文件,当我们不添加后缀名时,自动根据 pathext 查找文件
vue-cli-service.cmd

# Windows PowerShell 中可执行文件,可以跨平台
vue-cli-service.ps1
Copy after login

面试官:原来如此,不错嘛小伙子,短短时间内就掌握清楚了,看来学习能力很强,不错不错,我很看好你,我会催hr尽快回复你的。先这样了,拜拜

我(欣喜若狂,功夫不负有心人啊):好啊,好啊,拜拜

哔哔哔...(电话挂断)

过了三十分钟....

今天是个好日子,心想的事儿都能成,今天是个好日子,打开了家门咱迎春风...(手机铃声响起)。

我:喂,您好。

hr:您好,我是xxx公司的hr,根据你面试的优秀表现,恭喜你获得了我司的offer,经过我最大的努力,我给你争取到了最大的薪资,薪资是月薪3500,您看满意吗?

我:....

哔哔哔....(电话挂断)

tmd,c

1Interviewer: What happened when npm run xxx? Please start speaking!

总结

  • 运行 npm run xxx的时候,npm 会先在当前目录的 node_modules/.bin 查找要执行的程序,如果找到则运行;

  • 没有找到则从全局的 node_modules/.bin 中查找,npm i -g xxx就是安装到到全局目录;

  • 如果全局目录还是没找到,那么就从 path 环境变量中查找有没有其他同名的可执行程序。

原文地址:https://juejin.cn/post/7078924628525056007

作者:阳光是sunny

更多node相关知识,请访问:nodejs 教程

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What to do if npm react installation error occurs What to do if npm react installation error occurs Dec 27, 2022 am 11:25 AM

Solution to npm react installation error: 1. Open the "package.json" file in the project and find the dependencies object; 2. Move "react.json" to "devDependencies"; 3. Run "npm audit in the terminal --production" to fix the warning.

How to delete node in nvm How to delete node in nvm Dec 29, 2022 am 10:07 AM

How to delete node with nvm: 1. Download "nvm-setup.zip" and install it on the C drive; 2. Configure environment variables and check the version number through the "nvm -v" command; 3. Use the "nvm install" command Install node; 4. Delete the installed node through the "nvm uninstall" command.

How to use express to handle file upload in node project How to use express to handle file upload in node project Mar 28, 2023 pm 07:28 PM

How to handle file upload? The following article will introduce to you how to use express to handle file uploads in the node project. I hope it will be helpful to you!

An in-depth analysis of Node's process management tool 'pm2” An in-depth analysis of Node's process management tool 'pm2” Apr 03, 2023 pm 06:02 PM

This article will share with you Node's process management tool "pm2", and talk about why pm2 is needed, how to install and use pm2, I hope it will be helpful to everyone!

Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Pi Node Teaching: What is a Pi Node? How to install and set up Pi Node? Mar 05, 2025 pm 05:57 PM

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Let's talk about how to use pkg to package Node.js projects into executable files. Let's talk about how to use pkg to package Node.js projects into executable files. Dec 02, 2022 pm 09:06 PM

How to package nodejs executable file with pkg? The following article will introduce to you how to use pkg to package a Node project into an executable file. I hope it will be helpful to you!

What to do if npm node gyp fails What to do if npm node gyp fails Dec 29, 2022 pm 02:42 PM

npm node gyp fails because "node-gyp.js" does not match the version of "Node.js". The solution is: 1. Clear the node cache through "npm cache clean -f"; 2. Through "npm install -g n" Install the n module; 3. Install the "node v12.21.0" version through the "n v12.21.0" command.

Token-based authentication with Angular and Node Token-based authentication with Angular and Node Sep 01, 2023 pm 02:01 PM

Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to