npm
npm换源
| 12
 3
 4
 
 | $ npm config set registry https://registry.npmjs.com
 $ npm config set registry https://registry.npm.taobao.org
 $ npm config set registry http://mirrors.cloud.tencent.com/npm/
 
 | 
CDN
关于 cdn.jsdelivr.net 挂了:
将:https://cdn.jsdelivr.net/npm
更换为:https://unpkg.com
国内镜像源:https://unpkg.zhimg.com/
npm相关设置
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 
 | $ npm config ls				$ npm config get prefix
 $ npm config set prefix
 $ npm config get cache
 $ npm config set cache
 $ npm root -g
 
 $ npm set init-author-name "细粒丁"
 $ npm set init-author-email 'Your email'
 $ npm set init-author-url 'http://yourdomain.com'
 $ npm set init-license "MIT"
 
 | 
其他命令
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | $ npm install <package-name>			$ npm install <package-name>@latest
 $ npm install <package-name>@<版本号>
 
 $ npm info <package-name> version
 $ npm info <package-name> versions
 
 $ npm view <package-name> version
 $ npm view <package-name> versions
 
 $ npm adduser
 $ npm login
 $ npm publish
 $ npm whoami
 $ npm unpublish <package-name> --force
 $ npm deprecate <package-name> [@版本] "描述"
 
 | 
发版
详情官方发版文档
- 创建文件夹(文件夹名即为你的包名) 
- 进入文件夹 
- 初始化包,并生成package.json 
- 编写好文件,及其package.json中的相关信息(如名称、版本号、入口文件、描述等) 
- 包使用export导出,本地通过 - require引入测试
 
- —— 确保能正常使用则继续(下面则正式发包)—— 
- 换源,将国内源换成 npm官网的源 
- 登录帐号 
- 发版 - publish
 
- npm install 自己的包名,进行测试 
- 添加 - README.md文件
 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 
 | mkdir test-directory
 
 
 cd test-directory
 
 
 
 npm init [-y]
 
 
 npm config set registry https://registry.npmjs.com
 
 
 npm login
 
 
 npm publish
 
 
 npm install test-directory
 
 
 
 
 
 npm unpublish <package-name> --force
 
 
 
 
 
 npm deprecate <package-name> [@版本] "描述"
 
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 
 | {
 "name": "test-directory",
 "version": "0.0.1",
 "description": "将汉语转换成拼音",
 "main": "./lib/index.js",
 "scripts": {
 "test": "echo \"Error: no test specified\" && exit 1"
 },
 "keywords": [
 "汉语",
 "拼音"
 ],
 "git respository":
 "author": "细粒丁",
 "license": "MIT"
 }
 
 
 | 
设置环境变量后,全局安装的包,require(‘’)提示报不存在:
- 重启终端 
- 重启客户端 
- 检查环境变量是否存在 NODE_PATH,没有 则在系统变量添加 NODE_PATH: - npm root -g的路径
 
- 若是install后 命令不可用,如hexo、webpack,则检查环境变量是否 添加了 npm config get prefix 的路径 
一键升级所有依赖的插件:
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | $ npm install -g npm-check-updates
 
 
 $ npm-check-updates
 
 
 
 $ ncu -u
 
 | 
1. webpack包:
| 12
 3
 4
 5
 6
 
 | $ npm install --save-dev webpack$ npm install --save-dev webpack-cli
 
 $ npm install --save-dev style-loader css-loader
 $ npm install --save-dev file-loader
 $ npm install --save-dev csv-loader xml-loader
 
 | 
2. express 获取post中的数据
| 1
 | $ npm install --save body-parser
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | var express = require('express')
 
 var bodyParser = require('body-parser')
 
 var app = express()
 
 
 
 
 
 app.use(bodyParser.urlencoded({ extended: false }))
 
 
 app.use(bodyParser.json())
 
 
 | 
参考
3. 跨域 & 不默认请求网页图标
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 
 | response.writeHead(200, {"Content-Type": "application/json; charset=utf-8",
 "Access-Control-Allow-Origin": "*",
 "Access-Control-Allow-Headers": "*",
 
 "x-frame-options":"deny",
 "x-content-type-options":	"nosniff",
 "x-xss-protection":	"1",
 "content-security-policy":	"default-src 'none'",
 });
 
 | 
4. forever
解决 node.js 后台服务器无操作自动断开连接
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | $ npm install -g forever
 
 
 $ forever start index.js
 
 
 $ forever list
 
 
 $ forever stop index.js
 
 
 $ forever --help
 
 | 
启动
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | $ forever start ./index.js
 
 
 $ forever start -l forever.log ./index.js
 
 
 $ forever start -l forever.log -a ../index.js
 
 
 $ orever start -o out.log -e err.log ./index.js
 
 
 $ forever start -w ./index.js
 
 | 
不太可靠的nohup
| 1
 | $ nohup python3 github-calendar.py >> ./log/github-calendar.out 2>&1 &
 |