为什么输入 npm install 就可以自动安装对应的模块

面试题求解答
为什么输入 npm install 就可以自动安装对应的模块

npm install就是安装模块的命令呢,如果找不到的就要指定模块名称。

npm 模块安装机制:
发出npm install命令
查询node_modules目录之中是否已经存在指定模块
若存在,不再重新安装
若不存在
npm 向 registry 查询模块压缩包的网址
下载压缩包,存放在根目录下的.npm目录里
解压压缩包到当前项目的node_modules目录

可以参考一下:https://blog.csdn.net/wangningjing87/article/details/101689923

我们再用脚手架创建项目的时候一版都会有一个文件叫做package.json,在这个json文件中不仅包含了你项目的基本信息,还有这个项目依赖的插件,比如:

{
  "name": "pcManage",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.19.2",
    "core-js": "^3.6.4",
    "element-ui": "^2.13.0",
    "js-base64": "^2.5.2",
    "js-cookie": "^2.2.1",
    "node-sass": "^4.13.1",
    "nprogress": "^0.2.0",
    "sass-loader": "^8.0.2",
    "screenfull": "^5.0.2",
    "vue": "^2.6.11",
    "vue-quill-editor": "^3.0.6",
    "vue-router": "^3.1.5",
    "vuex": "^3.1.2"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.2.0",
    "@vue/cli-plugin-eslint": "~4.2.0",
    "@vue/cli-plugin-router": "~4.2.0",
    "@vue/cli-plugin-vuex": "~4.2.0",
    "@vue/cli-service": "~4.2.0",
    "babel-eslint": "^10.0.3",
    "babel-plugin-component": "^1.1.1",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^6.1.2",
    "node-sass": "^4.13.1",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.11",
    "webpack": "^4.42.0"
  }
}

这个文件的配置devDependencies项就是你再开发过程中会使用到的第三方包,devDependencies项就是你再发布环境或者生产环境会使用到的包,首先在执行npm install的时候程序就会再package.json这个文件下查看你项目依赖的包,如果没有安装的,它会依次帮你安装上,也就是个快捷方式而已,安装在哪了呢,就是项目的node_modules文件夹
若您觉得我的回答有用,望采纳