构建编译工具 - NPM
NPM简介
NPM全称是 Node Package Manager, 是用 JavaScript 写代码包管理工具, 运行在 Node.js 上.
npm 的发展是跟 Node.js 的发展相辅相成的。
Node.js 内置了npm作为包管理器, 随着 Node.js 的火爆, 现在用 npm 来分享代码已经成了前端的标配。
使用过程
1. 代码仓库(registry)存放共享代码
2. 使用 npm publish 把代码提交到 registry 
3. 其他人如果想使用这些代码,就把代码项目名称写到 package.json 里,然后运行 npm install 命令下载代码
4. 下载的代码存放在 node_modules 目录里,以供使用
这些"代码"被叫做包(package),NPM 名字也就是包管理器(Node Package Manager)。
package.json 位于模块的目录下,用于定义包的属性。
安装Node和NPM
Installation instructions - Ubuntu
# 在 Ubuntu上安装Node.js v12.x: curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - sudo apt-get install -y nodejs # 安装NPM新版本 sudo apt install npm # 升级 npm i -g npm //最新版本
设置registry
root@anliven:~# node -v v12.16.3 root@anliven:~# npm -v 6.14.4 root@anliven:~# npm config set registry https://registry.npm.taobao.org
Installation instructions - Windows
使用免安装版:
- Download | Node.js (nodejs.org)
- https://nodejs.org/dist/
- https://nodejs.org/dist/v12.16.1/node-v12.16.1-win-x64.zip
下载并解压,放到固定目录, 然后将此目录路径加入到用户或系统的path环境变量。
例如:

然后执行命令验证

常用命令
npm -v # 查看 npm 的版本 npm help # 查看 npm 命令列表 npm -l # 查看命令的简单用法 npm config list -l # 查看 npm 的配置 npm config set registry # 设置源 npm config set cache # 设置模块缓存路径 npm init # 初始化,生成新的package.json文件 npm run build # 构建 npm list # 当前项目安装的所有模块 npm list -g --depth 0 # 列出全局安装的模块 npm install -g <moduleName> # 安装模块 npm uninstall # 卸载模块 npm update # 更新模块
NPM命令帮助
# npm help npm <command> Usage: npm install install all the dependencies in your project npm install <foo> add the <foo> dependency to your project npm test run this project's tests npm run <foo> run the script named <foo> npm <command> -h quick help on <command> npm -l display usage info for all commands npm help <term> search for help on <term> npm help npm more involved overview All commands: access, adduser, audit, bugs, cache, ci, completion, config, dedupe, deprecate, diff, dist-tag, docs, doctor, edit, exec, explain, explore, find-dupes, fund, get, help, help-search, hook, init, install, install-ci-test, install-test, link, ll, login, logout, ls, org, outdated, owner, pack, ping, pkg, prefix, profile, prune, publish, query, rebuild, repo, restart, root, run-script, sbom, search, set, shrinkwrap, star, stars, start, stop, team, test, token, uninstall, unpublish, unstar, update, version, view, whoami Specify configs in the ini-formatted file: /root/.npmrc or on the command line via: npm <command> --key=value More configuration info: npm help config Configuration fields: npm help 7 config npm@10.8.2 /opt/node-v20.17.0-linux-x64/lib/node_modules/npm
参考信息
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号