Лекция 12
// локальная установка
> npm install package_name
// глобальная
> npm install -g package_name
// сокращение
> npm i package_name
{
"name": "my-awesome-package",
"version": "1.0.0"
}
// lowercase
// без пробелов
// одно слово
// можно - и _
"name"
// X.Y.Z
"version"
description
keywords
homepage
bugs: { url, email }
license
author | contributors: { name, email, url }
private
files # файлы для публикации
main # главный файл
bin # для создания symlink
man # ссылка на документацию
repository # ссылка на VCS репозиторий
scripts # список скриптов
"engines" : { "node" : ">=0.10.3 <0.12" }
"os" : [ "darwin", "linux" ]
"cpu" : [ "x64", "ia32" ]
// создание
// name, version, main, scripts, keywords
// author, license, repository
npm init
> npm set init.author.email "wombat@npmjs.com"
> npm set init.author.name "ag_dubs"
> npm set init.license "MIT"
// ~/.npm-init.js
module.exports = {
customField: 'Custom Field',
otherCustomField: 'This field is really cool'
}
// or
module.exports
= prompt("what's your favorite ice cream?",
"I LIKE THEM ALL");
{
"dependencies": {
"my_dep": "^1.0.0"
},
"devDependencies": {
"my_test_framework": "^3.1.0"
},
"optionalDependencies": {
"my_optional_util": "^4.2.4"
},
"peerDependencies": {
"react": "^16.0.1"
},
// npm pack
"bundledDependencies": [
"renderized", "super-streams"
]
}
> npm install package_name --save
> npm install package_name --save-dev
> npm install
> npm install --production
> npm outdated
> npm update
> npm update package
> npm update -g
> npm outdated -g --depth=0
> npm uninstall package --save
> npm uninstall package --save-dev
> npm uninstall package -g
> npm adduser
> npm login
> npm publish
// README.md, .gitignore, .npmignore
// https://npmjs.com/package/package_name
> npm view bluebird version
> npm version 1.0.2
> npm version [major | minor | patch]
"bluebird": "3.4.7"
// все что выше в пределах major
"bluebird": "^3.4.5"
+:
3.4.5
3.4.6
3.4.7
-:
4.0.0
// все что выше в пределах minor
"bluebird": "~3.3.1"
+:
3.3.1
3.3.2
3.3.3
-:
3.4.0
"bluebird": ">3.3.1"
"bluebird": ">=3.3.1"
"bluebird": "<3.3.1"
"bluebird": "3.3.1 - 3.3.3"
{
"dependencies": {
"@username/project-name": "^1.0.0"
}
}
> npm publish --tag beta
> npm install bluebird@beta
> npm dist-tag add bluebird@4.0.0-0 beta
> npm dist-tag rm bluebird beta
> npm dist-tag ls bluebird
> npm deprecate <pkg>[@<version>] <message>
> npm deprecate my-thing@"< 0.2.3"
"critical bug fixed in v0.2.3"
> npm link (in package dir)
> npm link [<@scope>/]<pkg>[@<version>]
> cd ~/projects/node-redis
> npm link
> cd ~/projects/node-bloggy
> npm link redis
> npm pack [[<@scope>/]<pkg>...]
> npm prune [[<@scope>/]<pkg>...] [--production]
> npm dedupe
{
"name": "A",
"version": "0.1.0",
"dependencies": {
"B": "<0.1.0"
}
}
{
"name": "B",
"version": "0.0.1",
"dependencies": {
"C": "<0.1.0"
}
}
{
"name": "C",
"version": "0.0.1"
}
npm install A
A@0.1.0
`-- B@0.0.1
`-- C@0.0.1
если есть B@0.0.2
A@0.1.0
`-- B@0.0.2
`-- C@0.0.1
npm-shrinkwrap.json
{
"name": "A",
"version": "0.1.0",
"dependencies": {
"B": {
"version": "0.0.1",
"from": "B@^0.0.1",
"resolved": "https://registry.npmjs.org/B/-/B-0.0.1.tgz",
"dependencies": {
"C": {
"version": "0.0.1",
"from": "org/C#v0.0.1",
"resolved": "..."
}
}
}
}
}
# генерируется автоматически
# тот же формат что и у npm-shrinkwrap.json
# приватный, хранится в VCS
# единное дерево зависимостей
# история дерева зависимостей
npm unpublish [<@scope>/]pkg[@version]
.npmingore
.npmrc
> yarn init
> yarn add [package]
> yarn add [package]@[version]
> yarn add [package]@[tag]
> yarn remove [package]
> yarn install
> yarn install --force
> yarn install --production
package.json (workspace root):
{
"private": true,
"workspaces": ["workspace-a", "workspace-b"]
}
workspace-a/package.json:
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5"
}
}
workspace-a/package.json:
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"cross-env": "5.0.5",
"workspace-a": "1.0.0"
}
}
> yarn install
/package.json
/yarn.lock
/node_modules
/node_modules/cross-env
/node_modules/workspace-a -> /workspace-a
/workspace-a/package.json
/workspace-b/package.json