前端入坑——使用npm管理前端应用

本文详细介绍如何使用npm创建项目,包括初始化项目、配置package.json、安装与卸载包、管理依赖等关键步骤。同时,解析了package.json各项配置的意义,帮助读者掌握npm的核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、创建第一个npm项目,在windows下先创建一个文件夹再使用npm init命令。

      npm init 会自动创建一个package.json文件过程中可以对该文件进行配置。如下图

E:\station>md test

E:\station>cd test

E:\station\test>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: (test) test
version: (1.0.0) 1.0.0
description: TODO
entry point: (index.js) index.js
test command:
git repository:
keywords: key
author: lee
license: (ISC)
About to write to E:\station\test\package.json:

{
  "name": "test",
  "version": "1.0.0",
  "description": "TODO",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "key"
  ],
  "author": "lee",
  "license": "ISC"
}

2、npm安装完成后的package.json中的一些参数的配置所代表的意义

{
  "name": "turn-web-ui",
  "version": "1.0.0",
  "description": "转盘活动ui",
  "main": "index.js",
  "scripts": {
    //脚本 npm run <脚本名称>
    "test": "test.js"
  },
  "repository": {
    "type": "git",
    "url": "git@192.168.99.125:newservice/turn-web-ui.git"
  },
  "keywords": [
    "turn-web-ui"
  ],
  "author": "lee seen",
  "license": "ISC",
  "dependencies": {
    //运行环境
  },
  "devDependencies": {
    //生产时环境

  },
  "engines": {
    //记录当前nodejs和npm版本
  },
  "publishConfig": {
    //向npm仓库发布自己的项目
  }
}

3、npm全局安装与本地安装

     npm install   <>  -g 为全局安装 否则为本地安装

     本地安装会在当前项目中创建一个node_modules并将包安装到里面(如果有则不创建)

     全局安装是安装到计算机中的 在计算机的任何地方都可以用,本地安装是安装到当前项目中,只有当前项目才可以用。

     约定俗成的,对于那些提供命令行工具的包进行全局安装,其它包本地安装。

4、使用npm install 安装package.json中的包

      使用你npm  install <> --save 可以在安装模块时,同时将安装的模块信息记录在 package.json 文件中(--save-dev会保存在开发依赖中的)

5、卸载安装的包 npm uninstall --save 同时卸载package.json中的记录

6、使用npm list 查看当前模块的依赖树  (--depth=0只显示第一层 1则为现实第二层 以此类推)

 

 

本次笔记就先记录到这。。。下次实践有更深入的研究再来。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值