欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

npm 学习

程序员文章站 2022-05-31 20:07:34
...

npm 的使用

命令概览

zengwe$ npm --help

Usage: npm <command>

where <command> is one of:
    access, adduser, audit, bin, bugs, c, cache, ci, cit,
    completion, config, create, ddp, dedupe, deprecate,
    dist-tag, docs, doctor, edit, explore, get, help,
    help-search, hook, i, init, install, install-test, it, link,
    list, ln, login, logout, ls, outdated, owner, pack, ping,
    prefix, profile, prune, publish, rb, rebuild, repo, restart,
    root, run, run-script, s, se, search, set, shrinkwrap, star,
    stars, start, stop, t, team, test, token, tst, un,
    uninstall, unpublish, unstar, up, update, v, version, view,
    whoami

npm <command> -h  quick help on <command>
npm -l            display full usage info
npm help <term>   search for help on <term>
npm help npm      involved overview

Specify configs in the ini-formatted file:
    **\.npmrc
or on the command line via: npm <command> --key value
Config info can be viewed via: npm help config

[email protected] **\nodejs\node_modules\npm

项目初始化

zengwe$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (temp)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to **\temp\package.json:

{
  "name": "temp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes) yes

生成package.json文件

安装包

已有的项目

zengwe$ npm install

安装package.json中的devDependencies和dependencies包

新增的包

zengwe$ npm install request

会在dependencies添加上新安装的包

开发时用的包(非打包的包)

zengwe$ npm install --save-dev request

会在devDependencies添加上新安装的包

安装指定版本的包

zengwe$ npm install [email protected]

安装request版本为1.0.0

zengwe$ npm install request^1.0.0

安装版本> 1.0.0即可

全局安装

zengwe$ npm install -g angular-cli

更新

zengwe$ npm update -g angular-cli
zengwe$ npm update request

卸载

zengwe$ npm uninstall -g angular-cli
zengwe$ npm uninstall -g request 

使用node_modules中的命令

比如我在网上clone了一个angular的项目启动项目必须用ng serve,但是我并未安装angular-cli,
解决办法在package.json的scripts中添加’ng serve’
缺点不灵活
可以在项目目录下使用

zengwe$ npx ng serve --port 4201

未完……

相关标签: npm