| Start | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Installing packages IntroductionPackage is a term used by npm to denote tools that developers can use for their projects. This includes everything from libraries and frameworks such as jQuery and AngularJS to task runners such as Gulp.js. The packages will come in a folder typically called Npm uses the command line to both install and manage packages, so users attempting to use npm should be familiar with basic commands on their operating system i.e.: traversing directories as well as being able to see the contents of directories. Installing NPMNote that in order to install packages, you must have NPM installed. The recommended way to install NPM is to use one of the installers from the Node.js download page (opens new window). You can check to see if you already have node.js installed by running either the After installing NPM via the Node.js installer, be sure to check for updates. This is because NPM gets updated more frequently than the Node.js installer. To check for updates run the following command: How to install packagesTo install one or more packages use the following: Note: This will install the package in the directory that the command line is currently in, thus it is important to check whether the appropriate directory has been chosen If you already have a If you want to install a specific version of a package use: If you want to install a version which matches a specific version range use: If you want to install the latest version use: The above commands will search for packages in the central Usually, modules will be installed locally in a folder named If you already created a In order to install packages and save them only if they are needed for development, not for running them, not if they are needed for the application to run, follow the following command: Installing dependenciesSome modules do not only provide a library for you to use, but they also provide one or more binaries which are intended to be used via the command line. Although you can still install those packages locally, it is often preferred to install them globally so the command-line tools can be enabled. In that case, If you want to see a list of all the installed packages and their associated versions in the current workspace, use: Adding an optional name argument can check the version of a specific package. Note: If you run into permission issues while trying to install an npm module globally, resist the temptation to issue a If you have build tools, or other development-only dependencies (e.g. Grunt), you might not want to have them bundled with the application you deploy. If that's the case, you'll want to have it as a development dependency, which is listed in the You will see that the package is then added to the To install dependencies of a downloaded/cloned node.js project, you can simply use npm will automatically read the dependencies from NPM Behind A Proxy ServerIf your internet access is through a proxy server, you might need to modify npm install commands that access remote repositories. npm uses a configuration file which can be updated via command line: You can locate your proxy settings from your browser's settings panel. Once you have obtained the proxy settings (server URL, port, username and password); you need to configure your npm configurations as follows.
|
||||||||||||||||||||||||||||||
| Uninstalling packages To uninstall one or more locally installed packages, use: The uninstall command for npm has five aliases that can also be used: If you would like to remove the package from the For a development dependency, use the For an optional dependency, use the For packages that are installed globally use the |
||||||||||||||||||||||||||||||
| Setting up a package configuration Node.js package configurations are contained in a file called That will try to read the current working directory for Git repository information (if it exists) and environment variables to try and autocomplete some of the placeholder values for you. Otherwise, it will provide an input dialog for the basic options. If you'd like to create a If you're creating a
To install a package and automatically save it to your The package and associated metadata (such as the package version) will appear in your dependencies. If you save if as a development dependency (using With this bare-bones |
||||||||||||||||||||||||||||||
| Running scripts You may define scripts in your To run the npm scripts are used most often for things like starting a server, building the project, and running tests. Here's a more realistic example: In the If your scripts become very long, they can be split into parts, like this: |
||||||||||||||||||||||||||||||
| Basic semantic versioning Before publishing a package you have to version it. npm supports semantic versioning (opens new window), this means there are patch, minor and major releases. For example, if your package is at version 1.2.3 to change version you have to:
You can also specify a version directly with:
When you set a package version using one of the npm commands above, npm will modify the version field of the package.json file, commit it, and also create a new Git tag with the version prefixed with a "v", as if you've issued the command:
Unlike other package managers like Bower, the npm registry doesn't rely on Git tags being created for every version. But, if you like using tags, you should remember to push the newly created tag after bumping the package version:
Or you can do this in one swoop with:
|
||||||||||||||||||||||||||||||
| Publishing a package First, make sure that you have configured your package (as said in Setting up a package configuration (opens new window)). Then, you have to be logged in to npmjs. If you already have a npm user If you don't have a user To check that your user is registered in the current client After that, when your package is ready to be published use And you are done. If you need to publish a new version, ensure that you update your package version, as stated in Basic semantic versioning (opens new window). Otherwise, |
||||||||||||||||||||||||||||||
| Removing extraneous packages To remove extraneous packages (packages that are installed but not in dependency list) run the following command: To remove all |
||||||||||||||||||||||||||||||
Scopes and repositories If the name of your own package starts with You can also persist these settings in a This is useful when automating the build on a CI server f.e. |
||||||||||||||||||||||||||||||
| Listing currently installed packages To generate a list (tree view) of currently installed packages, use ls, la and ll are aliases of list command. la and ll commands shows extended information like description and repository. Options The response format can be changed by passing options.
If you want, you can also go to the package's home page. |
||||||||||||||||||||||||||||||
| Updating npm and packages Since npm itself is a Node.js module, it can be updated using itself. If OS is Windows must be running command prompt as Admin If you want to check for updated versions you can do: In order to update a specific package: This will update the package to the latest version according to the restrictions in package.json In case you also want to lock the updated version in package.json: |
||||||||||||||||||||||||||||||
| Locking modules to specific versions By default, npm installs the latest available version of modules according to each dependencies' semantic version (opens new window). This can be problematic if a module author doesn't adhere to semver and introduces breaking changes in a module update, for example. To lock down each dependencies' version (and the versions of their dependencies, etc) to the specific version installed locally in the This will then create a |
||||||||||||||||||||||||||||||
| Setting up for globally installed packages You can use If you update your path, you can call On many OSes, You can tell This will use the prefix whenever you run In order to use the globally installed module, it needs to be on your path: Now when you run Note: When you |
||||||||||||||||||||||||||||||
| Linking projects for faster debugging and development Building project dependencies can sometimes be a tedious task. Instead of publishing a package version to NPM and installing the dependency to test the changes, use Help textSteps for linking project dependenciesWhen creating the dependency link, note that the package name is what is going to be referenced in the parent project.
Steps for linking a global tool
Problems that may ariseLinking projects can sometimes cause issues if the dependency or global tool is already installed. Syntax
Parameters
|
Read Article
https://develop.horje.com/learn/1434/reference