npm install

npm install我之前应该是经常用的,经常帮开发部署nodejs应用,经常就是npm install,npm run build,然后执行node xxx.js

确实是好久没用了,忘得就只记得这两个命令了。没想到面试又会碰到:说一下npm install都干了那些事? 。写一下

参考:https://blog.csdn.net/csdn_yudong/article/details/83721870

npm install 执行了什么操作

1、在git clone项目的时候,项目文件中并没有node_modules文件夹。这个文件夹保存的是我们项目开发中所使用的依赖模块。这个文件夹可能几百兆大小,如果放到github上,其它人clone的时候会非常慢,这个时候需要用一个package.json依赖配置文件解决这个问题。

2、下载这个项目,搭建环境的时候,只需进入该项目目录,直接npm install,npm就会根据这个文件去寻找需要的函数库,也就是依赖。

3、npm install执行完以后,我们会发现在项目下多了一个 node_modules文件夹。我们安装的依赖文件都可以在这里面找到

所以,npm install就是通过package.json文件安装需要的函数库,这些函数库就是所谓的依赖,会被放在当前目录下的node_modules文件夹

npm run build

参考:https://www.cnblogs.com/qcl-524077/articles/9234723.html

看一下package.json文件的内容

可以看到里面有dependencies段,devDependencies段,scripts段,

# cat package.json 
{
  "name": "socketserver",
  "version": "1.0.0",
  "description": "",
  "main": "socketServer.js",
  "dependencies": {
    "body-parser": "^1.15.2",
    "debug": "^3.1.0",
    "express": "^4.14.0",
    "log": "^1.4.0",
    "mysql": "^2.11.1",
    "redis": "^2.8.0",
    "request": "^2.78.0",
    "semver": "^5.4.1",
    "socket.io": "^2.0.4",
    "socket.io-client": "^2.1.1",
    "typescript": "^3.0.1"
  },
  "devDependencies": {
    "@types/debug": "0.0.30",
    "@types/request": "^2.0.13",
    "@types/socket.io": "^1.4.31"
  },
  "scripts": {
    "tsc": "tsc",
    "build": "npm run tsc",
    "start": "node socketServer.js",
    "start:debug": "DEBUG=socket-server,game,game-manager node socketServer.js",
    "server:debug": "DEBUG=socket-server node socketServer.js",
    "game:debug": "DEBUG=game,game-manager node socketServer.js"
  },
  "author": "",
  "license": "ISC"
}

npm run build就是执行了npm run tsc,也就是运行了tsc

npm run tsc - 执行一次 TypeScript 编译器 参考:https://www.cnblogs.com/haogj/p/5059170.html

"tsc": "tsc",
"build": "npm run tsc",

node xxx.js

这就是执行node脚本了

github上有很多的nodejs+docker 范例的

随便贴一个:https://github.com/b00giZm/docker-compose-nodejs-examples