NodeJS相关

发布时间 2023-07-26 18:42:20作者: 胡安

全局对象

相比于浏览器中的window全局对象,node的全局对象是global

console.log(window) // window is not defined

例如,你用node执行上面的代码,就会发现浏览器中存在的windownode中并不存在。

在node环境中全局对象为global,在global对象中会存在一些和window对象中同名且作用相同的方法,比如:

global.console.log
global.setInterval
global.clearInterval
global.setTimeout
global.clearTimeout
global.setImmediate

在node环境中声明的变量不会添加到全局对象中,变量声明之后只能在当前文件中使用。(主要是注意,node和浏览器中声明的变量特性有所不同)

var message = 'hello'
console.log(global.message) // undefined

模块包装函数(Module Wrapper Function)

Node是怎么实现模块化的,为什么在模块文件内部定义的变量在模块文件外部不能直接访问到呢?

也就是说,我在一个文件中写过一个cool function,那么我要怎么才能在另一个文件中直接用呢?

  1. 第一种思路,直接原文导入,但是这样就会造成同名的问题,如果a文件中有一个cool function,b文件中也有,那么它不是给我覆盖了吗?
  2. 第二种思路,把它们都进行包装,占用各自的命名空间,除非主动导入,否则不会造成命名污染,这也就是node采用的思路。

其实每一个模块文件中都会有module对象和require方法,也就是说,你并没有发现任何定义它们的地方,但是你可以直接在js文件中用,那么它们是从哪里来的?

在模块文件执行之前,模块文件中的代码会被包裹在模块包装函数当中,这样每个模块中的代码就都有了自己的作用域,所以可以在模块外部就不能访问模块内部的成员了。

(function(exports, require, module, __filename, __dirname){
	// entire module code lives here
})

从这个模块包装函数中可以看到,module和require实际上是模块内部成员,不是全局对象global下面的属性。

__filename:当前模块文件名称。
__dirname:当前文件所在路径。
exports:引用地址指向了module.exports对象,可以理解为是module.exports对象的简写形式。(简单理解就是默认exports = module.exports)

内置模块

path

file system

npm

  • 软件包依赖的相关问题

    • 依赖包存在的位置./node_modules ,并且不止安装了我们主动安装的依赖,还有依赖的依赖,以及依赖的依赖的依赖等,那么如果我们主动安装的两个依赖包,它们各自依赖了同一个依赖包,但是两个依赖的版本却不相同,那怎么办呢?

      解决方法就是:如果后安装的依赖包所依赖的包已经存在,但是版本不同,那么就让这个依赖包自己保存这个依赖包,存在自己的node_modules中。

版本控制

  1. 版本号规范

    Major Version (主要版本):添加新功能,或者破坏了现有的API,如:3.0.0

    Minor Version (次要版本):添加新功能(不会破坏现有的API,在现有的API基础上进行添加),如:3.12.0

    Patch Version (补丁版本):修复了某些bug,如:3.12.23

  2. 版本指定(更新)规则(更新的时候怎么选择,是否更新大版本、次版本)

    ^3.23.2:主要版本不变,更新次要版本和补丁版本;

    ^3.23.2:主要版本和次要版本都不变,仅更新补丁版本;

    3.23.2:也就是使用指定版本,不做任何更新

查看软件包实际版本

我们安装并使用了别人提供的包,怎么查看它们的依赖包呢?或者它们依赖的依赖呢?

  • 方式一:想看谁的就打开谁的package.json文件,看其中的version字段;
  • 方式二:通过npm list 命令查看所有依赖软件包的具体版本,--depth 选项指定查看依赖包的层级。

查看软件包元数据

  • 方式一:在npmjs.com 中查找目标软件包,并查看其元数据;

  • 方式二:通过命令的方式:

    npm view <pkg>
    npm view <pkg> [field]
    
    比如:
    npm view mongoose dist-tags dependencies
    

安装指定版本的软件包

npm i <pkg>@<version>
安装多个软件包可以直接在后面接着写
npm i mongoose@2.2.2 lodash@2.2.2

本地安装与全局安装

本地安装:这里的本地安装的本地是指仅安装在当前项目文件夹的node_modules中,也就仅仅能在本项目中使用。默认安装的包都是本地安装,npm i <pkg>

全局安装:安装在node的公共依赖包中,每个项目都可以使用。若要全局安装,需要特地指定,加-g参数。npm i <pkg> -g

查看全局安装包的位置:npm root -g

删除所有的全局安装包:npm un npm-check-updates -g

查看所有的全局安装包:npm list -g --depth 0

查看过期(未更新)的软件包:npm outdated -g

  • nodemon
    • 面临的问题:如果我每次对文件进行了修改都要手动执行一次,才能看到效果,怎么自动执行提高效率呢?
    • 通过nodemon,监控文件变化,自动执行文件
    • npm i nodemon -g
    • nodemon app.js 也就是本该用node的地方,直接用nodemon就行了。
  • npm-check-updates 强制更新
    • npm-check-updates 可以查看应用中有哪些软件包过期了,并进行强制更新(也就是说会忽略前面介绍过的更新规则)

npm的配置

应用场景举例:

​ 由于下载依赖包的网站npmjs.com为国外站点,因为墙的因素,大多数时候下载速度会很慢,怎么解决呢?

​ 通过修改npm的配置,指定更新的源地址为国内的镜像站。

  1. 获取npm的配置

    npm config -l --json

    -l 列表所有默认配置选项

    --json 以json格式显示配置选项

  2. 设置npm配置

    获取npm的下载地址:npm config get registry

    获取npm用户配置文件:npm config get userconfig

  3. 更改npm源地址为国内镜像地址

    npm config set registry https://registry.npm.taobao.org
    npm config set registry https://registry.npmjs.org/
    cat .npmrc
    

npx

npx也是npm软件包提供的命令,它是node平台下的软件包执行器,主要用途有两个:

  1. 避免安装保存软件包,某些工具我们只是临时使用,用完就删掉就行了,也就是一次性使用,用npx就能实现了;
  2. 避免全局软件包版本冲突;
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> (in a browser)
npm help npm       more involved overview (in a browser)

All commands:

    access          Set access level on published packages

                    Usage:
                    npm access list packages [<user>|<scope>|<scope:team> [<package>]
                    npm access list collaborators [<package> [<user>]]
                    npm access get status [<package>]
                    npm access set status=public|private [<package>]
                    npm access set mfa=none|publish|automation [<package>]
                    npm access grant <read-only|read-write> <scope:team> [<package>]
                    npm access revoke <scope:team> [<package>]

                    Options:
                    [--json] [--otp <otp>] [--registry <registry>]

                    Run "npm help access" for more info

    adduser         Add a registry user account

                    Usage:
                    npm adduser

                    Options:
                    [--registry <registry>] [--scope <@scope>] [--auth-type <legacy|web>]

                    alias: add-user

                    Run "npm help adduser" for more info

    audit           Run a security audit

                    Usage:
                    npm audit [fix|signatures]

                    Options:
                    [--audit-level <info|low|moderate|high|critical|none>] [--dry-run] [-f|--force]
                    [--json] [--package-lock-only]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
                    [--foreground-scripts] [--ignore-scripts]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    Run "npm help audit" for more info

    bugs            Report bugs for a package in a web browser

                    Usage:
                    npm bugs [<pkgname> [<pkgname> ...]]

                    Options:
                    [--no-browser|--browser <browser>] [--registry <registry>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    alias: issues

                    Run "npm help bugs" for more info

    cache           Manipulates packages cache

                    Usage:
                    npm cache add <package-spec>
                    npm cache clean [<key>]
                    npm cache ls [<name>@<version>]
                    npm cache verify

                    Options:
                    [--cache <cache>]

                    Run "npm help cache" for more info

    ci              Clean install a project

                    Usage:
                    npm ci

                    Options:
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
                    [--strict-peer-deps] [--foreground-scripts] [--ignore-scripts] [--no-audit]
                    [--no-bin-links] [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    aliases: clean-install, ic, install-clean, isntall-clean

                    Run "npm help ci" for more info

    completion      Tab Completion for npm

                    Usage:
                    npm completion

                    Run "npm help completion" for more info

    config          Manage the npm configuration files

                    Usage:
                    npm config set <key>=<value> [<key>=<value> ...]
                    npm config get [<key> [<key> ...]]
                    npm config delete <key> [<key> ...]
                    npm config list [--json]
                    npm config edit
                    npm config fix

                    Options:
                    [--json] [-g|--global] [--editor <editor>] [-L|--location <global|user|project>]
                    [-l|--long]

                    alias: c

                    Run "npm help config" for more info

    dedupe          Reduce duplication in the package tree

                    Usage:
                    npm dedupe

                    Options:
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--strict-peer-deps] [--no-package-lock]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] [--ignore-scripts]
                    [--no-audit] [--no-bin-links] [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    alias: ddp

                    Run "npm help dedupe" for more info

    deprecate       Deprecate a version of a package

                    Usage:
                    npm deprecate <package-spec> <message>

                    Options:
                    [--registry <registry>] [--otp <otp>]

                    Run "npm help deprecate" for more info

    diff            The registry diff command

                    Usage:
                    npm diff [...<paths>]

                    Options:
                    [--diff <package-spec> [--diff <package-spec> ...]] [--diff-name-only]
                    [--diff-unified <number>] [--diff-ignore-all-space] [--diff-no-prefix]
                    [--diff-src-prefix <path>] [--diff-dst-prefix <path>] [--diff-text] [-g|--global]
                    [--tag <tag>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    Run "npm help diff" for more info

    dist-tag        Modify package distribution tags

                    Usage:
                    npm dist-tag add <package-spec (with version)> [<tag>]
                    npm dist-tag rm <package-spec> <tag>
                    npm dist-tag ls [<package-spec>]

                    Options:
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    alias: dist-tags

                    Run "npm help dist-tag" for more info

    docs            Open documentation for a package in a web browser

                    Usage:
                    npm docs [<pkgname> [<pkgname> ...]]

                    Options:
                    [--no-browser|--browser <browser>] [--registry <registry>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    alias: home

                    Run "npm help docs" for more info

    doctor          Check your npm environment

                    Usage:
                    npm doctor [ping] [registry] [versions] [environment] [permissions] [cache]

                    Options:
                    [--registry <registry>]

                    Run "npm help doctor" for more info

    edit            Edit an installed package

                    Usage:
                    npm edit <pkg>[/<subpkg>...]

                    Options:
                    [--editor <editor>]

                    Run "npm help edit" for more info

    exec            Run a command from a local or remote npm package

                    Usage:
                    npm exec -- <pkg>[@<version>] [args...]
                    npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
                    npm exec -c '<cmd> [args...]'
                    npm exec --package=foo -c '<cmd> [args...]'

                    Options:
                    [--package <package-spec> [--package <package-spec> ...]] [-c|--call <call>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    alias: x

                    Run "npm help exec" for more info

    explain         Explain installed packages

                    Usage:
                    npm explain <package-spec>

                    Options:
                    [--json] [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]

                    alias: why

                    Run "npm help explain" for more info

    explore         Browse an installed package

                    Usage:
                    npm explore <pkg> [ -- <command>]

                    Options:
                    [--shell <shell>]

                    Run "npm help explore" for more info

    find-dupes      Find duplication in the package tree

                    Usage:
                    npm find-dupes

                    Options:
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--strict-peer-deps] [--no-package-lock]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] [--ignore-scripts]
                    [--no-audit] [--no-bin-links] [--no-fund]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    Run "npm help find-dupes" for more info

    fund            Retrieve funding information

                    Usage:
                    npm fund [<package-spec>]

                    Options:
                    [--json] [--no-browser|--browser <browser>] [--no-unicode]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [--which <fundingSourceNumber>]

                    Run "npm help fund" for more info

    get             Get a value from the npm configuration

                    Usage:
                    npm get [<key> ...] (See `npm config`)

                    Options:
                    [-l|--long]

                    Run "npm help get" for more info

    help            Get help on npm

                    Usage:
                    npm help <term> [<terms..>]

                    Options:
                    [--viewer <viewer>]

                    alias: hlep

                    Run "npm help help" for more info

    help-search     Search npm help documentation

                    Usage:
                    npm help-search <text>

                    Options:
                    [-l|--long]

                    Run "npm help help-search" for more info

    hook            Manage registry hooks

                    Usage:
                    npm hook add <pkg> <url> <secret> [--type=<type>]
                    npm hook ls [pkg]
                    npm hook rm <id>
                    npm hook update <id> <url> <secret>

                    Options:
                    [--registry <registry>] [--otp <otp>]

                    Run "npm help hook" for more info

    init            Create a package.json file

                    Usage:
                    npm init <package-spec> (same as `npx <package-spec>`)
                    npm init <@scope> (same as `npx <@scope>/create`)

                    Options:
                    [-y|--yes] [-f|--force] [--scope <@scope>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--no-workspaces-update] [--include-workspace-root]

                    aliases: create, innit

                    Run "npm help init" for more info

    install         Install a package

                    Usage:
                    npm install [<package-spec> ...]

                    Options:
                    [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle]
                    [-E|--save-exact] [-g|--global]
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
                    [--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
                    [--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
                    [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    aliases: add, i, in, ins, inst, insta, instal, isnt, isnta, isntal, isntall

                    Run "npm help install" for more info

    install-ci-test Install a project with a clean slate and run tests

                    Usage:
                    npm install-ci-test

                    Options:
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
                    [--strict-peer-deps] [--foreground-scripts] [--ignore-scripts] [--no-audit]
                    [--no-bin-links] [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    aliases: cit, clean-install-test, sit

                    Run "npm help install-ci-test" for more info

    install-test    Install package(s) and run tests

                    Usage:
                    npm install-test [<package-spec> ...]

                    Options:
                    [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle]
                    [-E|--save-exact] [-g|--global]
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
                    [--strict-peer-deps] [--prefer-dedupe] [--no-package-lock] [--package-lock-only]
                    [--foreground-scripts] [--ignore-scripts] [--no-audit] [--no-bin-links]
                    [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    alias: it

                    Run "npm help install-test" for more info

    link            Symlink a package folder

                    Usage:
                    npm link [<package-spec>]

                    Options:
                    [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle]
                    [-E|--save-exact] [-g|--global]
                    [--install-strategy <hoisted|nested|shallow|linked>] [--legacy-bundling]
                    [--global-style] [--strict-peer-deps] [--no-package-lock]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] [--ignore-scripts]
                    [--no-audit] [--no-bin-links] [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    alias: ln

                    Run "npm help link" for more info

    ll              List installed packages

                    Usage:
                    npm ll [[<@scope>/]<pkg> ...]

                    Options:
                    [-a|--all] [--json] [-l|--long] [-p|--parseable] [-g|--global] [--depth <depth>]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] [--link]
                    [--package-lock-only] [--no-unicode]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    alias: la

                    Run "npm help ll" for more info

    login           Login to a registry user account

                    Usage:
                    npm login

                    Options:
                    [--registry <registry>] [--scope <@scope>] [--auth-type <legacy|web>]

                    Run "npm help login" for more info

    logout          Log out of the registry

                    Usage:
                    npm logout

                    Options:
                    [--registry <registry>] [--scope <@scope>]

                    Run "npm help logout" for more info

    ls              List installed packages

                    Usage:
                    npm ls <package-spec>

                    Options:
                    [-a|--all] [--json] [-l|--long] [-p|--parseable] [-g|--global] [--depth <depth>]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] [--link]
                    [--package-lock-only] [--no-unicode]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    alias: list

                    Run "npm help ls" for more info

    org             Manage orgs

                    Usage:
                    npm org set orgname username [developer | admin | owner]
                    npm org rm orgname username
                    npm org ls orgname [<username>]

                    Options:
                    [--registry <registry>] [--otp <otp>] [--json] [-p|--parseable]

                    alias: ogr

                    Run "npm help org" for more info

    outdated        Check for outdated packages

                    Usage:
                    npm outdated [<package-spec> ...]

                    Options:
                    [-a|--all] [--json] [-l|--long] [-p|--parseable] [-g|--global]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]

                    Run "npm help outdated" for more info

    owner           Manage package owners

                    Usage:
                    npm owner add <user> <package-spec>
                    npm owner rm <user> <package-spec>
                    npm owner ls <package-spec>

                    Options:
                    [--registry <registry>] [--otp <otp>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces]

                    alias: author

                    Run "npm help owner" for more info

    pack            Create a tarball from a package

                    Usage:
                    npm pack <package-spec>

                    Options:
                    [--dry-run] [--json] [--pack-destination <pack-destination>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    Run "npm help pack" for more info

    ping            Ping npm registry

                    Usage:
                    npm ping

                    Options:
                    [--registry <registry>]

                    Run "npm help ping" for more info

    pkg             Manages your package.json

                    Usage:
                    npm pkg set <key>=<value> [<key>=<value> ...]
                    npm pkg get [<key> [<key> ...]]
                    npm pkg delete <key> [<key> ...]
                    npm pkg set [<array>[<index>].<key>=<value> ...]
                    npm pkg set [<array>[].<key>=<value> ...]
                    npm pkg fix

                    Options:
                    [-f|--force] [--json]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces]

                    Run "npm help pkg" for more info

    prefix          Display prefix

                    Usage:
                    npm prefix [-g]

                    Options:
                    [-g|--global]

                    Run "npm help prefix" for more info

    profile         Change settings on your registry profile

                    Usage:
                    npm profile enable-2fa [auth-only|auth-and-writes]
                    npm profile disable-2fa
                    npm profile get [<key>]
                    npm profile set <key> <value>

                    Options:
                    [--registry <registry>] [--json] [-p|--parseable] [--otp <otp>]

                    Run "npm help profile" for more info

    prune           Remove extraneous packages

                    Usage:
                    npm prune [[<@scope>/]<pkg>...]

                    Options:
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]] [--dry-run]
                    [--json] [--foreground-scripts] [--ignore-scripts]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    Run "npm help prune" for more info

    publish         Publish a package

                    Usage:
                    npm publish <package-spec>

                    Options:
                    [--tag <tag>] [--access <restricted|public>] [--dry-run] [--otp <otp>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]
                    [--provenance|--provenance-file <file>]

                    Run "npm help publish" for more info

    query           Retrieve a filtered list of packages

                    Usage:
                    npm query <selector>

                    Options:
                    [-g|--global]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    Run "npm help query" for more info

    rebuild         Rebuild a package

                    Usage:
                    npm rebuild [<package-spec>] ...]

                    Options:
                    [-g|--global] [--no-bin-links] [--foreground-scripts] [--ignore-scripts]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    alias: rb

                    Run "npm help rebuild" for more info

    repo            Open package repository page in the browser

                    Usage:
                    npm repo [<pkgname> [<pkgname> ...]]

                    Options:
                    [--no-browser|--browser <browser>] [--registry <registry>]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    Run "npm help repo" for more info

    restart         Restart a package

                    Usage:
                    npm restart [-- <args>]

                    Options:
                    [--ignore-scripts] [--script-shell <script-shell>]

                    Run "npm help restart" for more info

    root            Display npm root

                    Usage:
                    npm root

                    Options:
                    [-g|--global]

                    Run "npm help root" for more info

    run-script      Run arbitrary package scripts

                    Usage:
                    npm run-script <command> [-- <args>]

                    Options:
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--if-present] [--ignore-scripts]
                    [--foreground-scripts] [--script-shell <script-shell>]

                    aliases: run, rum, urn

                    Run "npm help run-script" for more info

    search          Search for packages

                    Usage:
                    npm search [search terms ...]

                    Options:
                    [-l|--long] [--json] [--color|--no-color|--color always] [-p|--parseable]
                    [--no-description] [--searchopts <searchopts>] [--searchexclude <searchexclude>]
                    [--registry <registry>] [--prefer-online] [--prefer-offline] [--offline]

                    aliases: find, s, se

                    Run "npm help search" for more info

    set             Set a value in the npm configuration

                    Usage:
                    npm set <key>=<value> [<key>=<value> ...] (See `npm config`)

                    Options:
                    [-g|--global] [-L|--location <global|user|project>]

                    Run "npm help set" for more info

    shrinkwrap      Lock down dependency versions for publication

                    Usage:
                    npm shrinkwrap

                    Run "npm help shrinkwrap" for more info

    star            Mark your favorite packages

                    Usage:
                    npm star [<package-spec>...]

                    Options:
                    [--registry <registry>] [--no-unicode] [--otp <otp>]

                    Run "npm help star" for more info

    stars           View packages marked as favorites

                    Usage:
                    npm stars [<user>]

                    Options:
                    [--registry <registry>]

                    Run "npm help stars" for more info

    start           Start a package

                    Usage:
                    npm start [-- <args>]

                    Options:
                    [--ignore-scripts] [--script-shell <script-shell>]

                    Run "npm help start" for more info

    stop            Stop a package

                    Usage:
                    npm stop [-- <args>]

                    Options:
                    [--ignore-scripts] [--script-shell <script-shell>]

                    Run "npm help stop" for more info

    team            Manage organization teams and team memberships

                    Usage:
                    npm team create <scope:team> [--otp <otpcode>]
                    npm team destroy <scope:team> [--otp <otpcode>]
                    npm team add <scope:team> <user> [--otp <otpcode>]
                    npm team rm <scope:team> <user> [--otp <otpcode>]
                    npm team ls <scope>|<scope:team>

                    Options:
                    [--registry <registry>] [--otp <otp>] [-p|--parseable] [--json]

                    Run "npm help team" for more info

    test            Test a package

                    Usage:
                    npm test [-- <args>]

                    Options:
                    [--ignore-scripts] [--script-shell <script-shell>]

                    aliases: tst, t

                    Run "npm help test" for more info

    token           Manage your authentication tokens

                    Usage:
                    npm token list
                    npm token revoke <id|token>
                    npm token create [--read-only] [--cidr=list]

                    Options:
                    [--read-only] [--cidr <cidr> [--cidr <cidr> ...]] [--registry <registry>]
                    [--otp <otp>]

                    Run "npm help token" for more info

    uninstall       Remove a package

                    Usage:
                    npm uninstall [<@scope>/]<pkg>...

                    Options:
                    [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle]
                    [-g|--global]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    aliases: unlink, remove, rm, r, un

                    Run "npm help uninstall" for more info

    unpublish       Remove a package from the registry

                    Usage:
                    npm unpublish [<package-spec>]

                    Options:
                    [--dry-run] [-f|--force]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces]

                    Run "npm help unpublish" for more info

    unstar          Remove an item from your favorite packages

                    Usage:
                    npm unstar [<package-spec>...]

                    Options:
                    [--registry <registry>] [--no-unicode] [--otp <otp>]

                    Run "npm help unstar" for more info

    update          Update packages

                    Usage:
                    npm update [<pkg>...]

                    Options:
                    [-S|--save|--no-save|--save-prod|--save-dev|--save-optional|--save-peer|--save-bundle]
                    [-g|--global] [--install-strategy <hoisted|nested|shallow|linked>]
                    [--legacy-bundling] [--global-style]
                    [--omit <dev|optional|peer> [--omit <dev|optional|peer> ...]]
                    [--strict-peer-deps] [--no-package-lock] [--foreground-scripts]
                    [--ignore-scripts] [--no-audit] [--no-bin-links] [--no-fund] [--dry-run]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root] [--install-links]

                    aliases: up, upgrade, udpate

                    Run "npm help update" for more info

    version         Bump a package version

                    Usage:
                    npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease | from-git]

                    Options:
                    [--allow-same-version] [--no-commit-hooks] [--no-git-tag-version] [--json]
                    [--preid prerelease-id] [--sign-git-tag]
                    [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--no-workspaces-update] [--include-workspace-root]

                    alias: verison

                    Run "npm help version" for more info

    view            View registry info

                    Usage:
                    npm view [<package-spec>] [<field>[.subfield]...]

                    Options:
                    [--json] [-w|--workspace <workspace-name> [-w|--workspace <workspace-name> ...]]
                    [-ws|--workspaces] [--include-workspace-root]

                    aliases: info, show, v

                    Run "npm help view" for more info

    whoami          Display npm username

                    Usage:
                    npm whoami

                    Options:
                    [--registry <registry>]

                    Run "npm help whoami" for more info

Specify configs in the ini-formatted file:
    F:\Users\Sher10ck\.npmrc
or on the command line via: npm <command> --key=value

More configuration info: npm help config
Configuration fields: npm help 7 config

npm@9.8.0

模块查找规则

先回答两个问题:谁查找模块,查找什么模块

  • 当然是代码中表明了引用,一般是require
  • 查找的当然是指定的模块(包),也就是require()中传入的参数

那么有两种情况:

  • 指定了查找路径

    require("./cool_package")
    

    默认为引用的是自己编写的文件

    1. 查找cool_package.js
    2. 查找cool_package.json
    3. 查找cool_package文件夹,查看入口文件(package.json->main)
    4. 查找cool_package文件夹中的index.js文件
  • 未指定查找路径

    require('cool_package')
    

    默认为引用的是依赖包

    path:[
        '/Users/administrators/Desktop/Node/code/node_modules',
        '/Users/administrators/Desktop/Node/node_modules',
        '/Users/administrators/Desktop/node_modules',
        '/Users/administrators/node_modules',
        '/Users/node_modules',
        '/node_modules',
    ]