💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**npm:node package manager** npm is the world’s largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well. [[npm官方文档]](https://docs.npmjs.com/about-npm/) 什么是npm、cnpm、bower? 简单地说,就是帮你下载好你需要的css或者js库,而且三者功能也都是一样的。那为什么要下载这3个不同的呢?据说npm容易被墙……而cnpm是淘宝的镜像,所以通常用cnpm代替npm。至于bower,是因为bower更多地用于前端开发。但是因为它也是依赖于npm的,所以没有npm,就没法载bower……不知道说了那么多你懂了没,但是这不重要~你所需要做的就是分别安装这3个,然后将bower为己所用。 [TOC] ## About NPM npm consists of three distinct components: * the website * the Command Line Interface (CLI) * the registry Use the [*website*](https://npmjs.com/) to discover packages, set up profiles, and manage other aspects of your npm experience. For example, you can set up [Orgs](https://www.npmjs.com/features)(organizations) to manage access to public or private packages. The [*CLI*](https://docs.npmjs.com/cli/npm) runs from a terminal, and is how most developers interact with npm. The [*registry*](https://docs.npmjs.com/misc/registry) is a large public database of JavaScript software and the meta-information surrounding it. **Use npm to . . .** * Adapt packages of code for your apps, or incorporate packages as they are. * Download standalone tools you can use right away. * Run packages without downloading using[npx](https://www.npmjs.com/package/npx). * Share code with any npm user, anywhere. * Restrict code to specific developers. * Create Orgs (organizations) to coordinate package maintenance, coding, and developers. * Form virtual teams by using Orgs. * Manage multiple versions of code and code dependencies. * Update applications easily when underlying code is updated. * Discover multiple ways to solve the same puzzle. * Find other developers who are working on similar problems and projects. ## Configuring your local environment [[官方网站]](https://docs.npmjs.com/getting-started/configuring-your-local-environment) ## Packages and modules [[官方网站]](https://docs.npmjs.com/about-packages-and-modules) The npm registry contains packages, many of which are also Node modules, or contain Node modules. ### Package A **package** is a file or directory that is described by a`package.json`file. A package must contain a`package.json`file in order to be published to the npm registry. A package is any of the following: * a) A folder containing a program described by a`package.json`file. * b) A gzipped tarball containing (a). * c) A URL that resolves to (b). * d) A `<name>@<version>` that is published on the registry with (c). * e) A `<name>@<tag>` that points to (d). * f) A `<name>` that has a `latest` tag satisfying (e). * g) A `git` url that, when cloned, results in (a). Git URLs used for npm packages can be formatted in the following ways: * `git://github.com/user/project.git#commit-ish` * `git+ssh://user@hostname:project.git#commit-ish` * `git+http://user@hostname/project/blah.git#commit-ish` * `git+https://user@hostname/project/blah.git#commit-ish` The `commit-ish` can be any tag, sha, or branch that can be supplied as an argument to `git checkout`. The default `commit-ish` is `master`. ### Module A **module** is any file or directory in the`node_modules`directory that can be loaded by the Node.js`require()`function. To be loaded by the Node.js`require()`function, a module must be one of the following: * A folder with a `package.json` file containing a `"main"` field. * A folder with an `index.js` file in it. * A JavaScript file. >[warning] Note:Since modules are not required to have a `package.json` file, not all modules are packages. Only modules that have a `package.json` file are also packages. In the context of a Node program, the `module` is also the thing that was loaded *from* a file. For example, in the following program: ~~~ var req = require('request') ~~~ ## npm package scope, access level, and visibility [[官方网站]](https://docs.npmjs.com/package-scope-access-level-and-visibility) Visibility of npm packages depends on the scope (namespace) in which the package is contained, and the access level (private or public) set for the package. >[warning] Note:To create Org-scoped packages, you must first create an Org. ### Public registry | Scope | Access level | Can view and download | Can write (publish) | | --- | --- | --- | --- | | Org | Private | Members of a team in the Org with read access to the package | Members of a team in the Org with read and write access to the package | | Org | Public | Everyone | Members of a team in the Org with read and write access to the package | | User | Private | The package owner and users who have been granted read access to the package | The package owner and users who have been granted read and write access to the package | | User | Public | Everyone | The package owner and users who have been granted read and write access to the package | | Unscoped | Public | Everyone | The package owner and users who have been granted read and write access to the package | >[warning] Note:Only user accounts can create and manage unscoped packages. Orgs can only manage scoped packages. ### npm Enterprise The following table applies to customers who purchased [npm Enterprise](https://docs.npmjs.com/enterprise) after July 26, 2018. | Scope | Access level | Can view and download | Can write (publish) | | --- | --- | --- | --- | | Org | Private | Logged-in members of the Enterprise registry who belong to a team in the Org with read access to the package | Logged-in members of the Enterprise registry who belong to a team in the Org with read and write access to the package | | Org | Public | All logged-in members of the Enterprise registry | Logged-in members of the Enterprise registry who belong to a team in the Org with read and write access to the package | | User | Private | The package owner and logged-in members of the Enterprise registry who have been granted read access to the package | The package owner and logged-in members of the Enterprise registry who have been granted read and write access to the package | | User | Public | All logged-in members of the Enterprise registry | The package owner and logged-in members of the Enterprise registry who have been granted read and write access to the package | | Unscoped | Public | All users of the Enterprise registry | None (see note below) | >[warning] Note:The unscoped namespace on npm Enterprise is reserved for unscoped packages in the public npm registry. To prevent npm Enterprise users from accidentally publishing proprietary code to the public npm registry, where it would be visible to anyone on the internet, we do not allow publishing unscoped packages to npm Enterprise. ## Getting packages from the registry [[官方网站]](https://docs.npmjs.com/packages-and-modules/getting-packages-from-the-registry) ### Searching for and choosing packages to download You can use the npm search bar to find packages to use in your projects. npm search uses npms and the npms analyzer; for more information on both, see https://npms.io/about. ### Downloading and installing packages locally You can `install` a package locally if you want to depend on the package from your own module, using something like Node.js `require`. This is `npm install`’s default behavior. **Installing an unscoped package** Unscoped packages are always public, which means they can be searched for, downloaded, and installed by anyone. To install a public package, on the command line, run ~~~ npm install <package_name> ~~~ This will create the`node_modules`directory in your current directory (if one doesn’t exist yet) and will download the package to that directory. >[warning] Note:If there is no `package.json` file in the local directory, the latest version of the package is installed. If there is a `package.json` file, npm installs the latest version that satisfies the [semver rule](https://docs.npmjs.com/about-semantic-versioning) declared in`package.json`. **Installed a scoped public package** [Scoped public packages](https://docs.npmjs.com/about-scopes) can be downloaded and installed by anyone, as long as the scope name is referenced during installation: ~~~ npm install @scope/package-name ~~~ **Installing a private package** [Private packages](https://docs.npmjs.com/about-private-packages)can only be downloaded and installed by those who have been granted read access to the package. Since private packages are always scoped, you must reference the scope name during installation: ~~~ npm install @scope/private-package-name ~~~ **Testing package installation** To confirm that `npm install` worked correctly, in your module directory, check that a `node_modules` directory exists and that it contains a directory for the package(s) you installed: ~~~ ls node_modules | grep lodash lodash@ ~~~ **Installed package version** If there is a `package.json` file in the directory in which `npm install` is run, npm instalsx the latest version of the package that satisfies the [semantic versioning rule](https://docs.npmjs.com/about-semantic-versioning) declared in `package.json`. If there is no `package.json` file, the latest version of the package is installed. **Installing a package with dist-tags** Like `npm publish`, `npm install <package_name>` will use the `latest` tag by default. To override this behavior, use`npm install <package_name>@<tag>`. For example, to install the`example-package`at the version tagged with`beta`, you would run the following command: ~~~ npm install example-package@beta ~~~ ### Downloading and installing packages globally Tip:If you are using npm 5.2 or higher, we recommend using `npx` to run packages globally. [Installing](https://docs.npmjs.com/cli/install) a package globally allows you to use the code in the package as a set of tools on your local computer. To download and install packages globally, on the command line, run the following command: ~~~ npm install -g <package_name> ~~~ If you get an EACCES permissions error, you may need to reinstall npm with a version manager or manually change npm’s default directory. For more information, see “[Resolving EACCES permissions errors when installing packages globally](https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally)”. ## cnpm 使用说明 [[淘宝 NPM 镜像]](https://npm.taobao.org/) 可以使用淘宝定制的[`cnpm`](https://github.com/cnpm/cnpm)(gzip 压缩支持) 命令行工具代替默认的`npm`: ~~~bash $ npm install -g cnpm --registry=https://registry.npm.taobao.org ~~~ 或者直接通过添加`npm`参数`alias`一个新命令: ~~~bash alias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc" # Or alias it in .bashrc or .zshrc $ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc ~~~ ## npm安装package.json中的模块依赖 1.package.json不存在时 命令:`npm init`可自动创建package.json文件 2.package.json存在时 直接命令:`npm install` 或者 `npm install –save-dev`会自动将package.json中的模块安装到node-modules文件夹下