Mastering npm-link and npm-logout CLI Options
There are times when you would want to create a symlink to a package, there are also times when you wish to logout of the registry. You can use the npm-link and npm-logout. to achieve these respectively. In this tutorial we will examine how these two cli options work.
Npm-link
This command is used to create a symlink to a package folder.
Synopsis
npm link (in package dir)
npm link [<@scope>/]<pkg>[@<version>]
alias: npm ln
Description
There are two processes required to symlink a package.
The first process is to run npm link in a package folder to create a symlink in the global folder {prefix}/lib/node_modules/<package> that will link to the package where the npm link command was executed. It also links any bins in the package to {prefix}/bin/{name}.
The next thing you need to do is that in some other location, you should run npm link package-name. This will create a symbolic link from globally-installed package-name to node_modules/ of the current folder.
It should be noted that package-name is taken from package.json, and not from the directory name.
The package name can optionally be prefixed with a scope. The scope has to be preceded by an @-symbol, followed by a slash.
When you creating tarballs for npm publish, the linked packages will be snapshotted to their current state by resolving the symbolic links.
This is very handy for installing your own stuff, so that you can conveniently work on it and test it without having to continually rebuild.
For instance:
cd ~/projects/node-redis #will go into the package directory
npm link # will create global link
cd ~/projects/node-bloggy #will go into some other package directory.
npm link redis # will link-install the package
Now, all the changes to ~/project/node-redis will be reflected in ~/projects/node-bloggy/node_modules/node-redis/.
You can also shortcut the two steps into one. For instance, if you want to do the above use-case in a shorter way, you can run:
cd ~/projects/node-bloggy # will go into the dir of your main project
npm link ../node-redis # will link the dir of your dependency
The second line is equivalent to doing:
(cd ../node-redis; npm link)
npm link redis
what this means is that it will first create a global link, and then it will link the global installation target into your project's node_modules folder.
It should be noted that, you are referring to the directory name, node-redis and not the package name redis.
You have to include the scope that comes with the linked package if it is scoped:
npm link @myorg/privatepackage
npm-logout
This command enables you to logout of the registry.
Synopsis
npm logout [--registry=<url>] [--scope=<@scope>]
Description
Whenever you are logged into a registry that supports token-based authentication, this command tells the server to end this token's session. It will invalidate the token everywhere that you are using it, and not just for the current environment.
When you are logged into a legacy registry that uses username and password authentication, this command clears the credentials in your user configuration. In this case, it only affects the current environment.
If you provide-scope, this command will find the credentials for the registry that is connected to that scope, if it is set.
Configuration
Registry
This is the base URL of the npm package registry, if you also specify the scope, the scope will take precedence.
Default: https://registry.npmjs.org/
Scope
When you specify scope, you are logged out of the specified scope.
Default: The scope is the scope of your current project, if it is specified else it is none.
npm logout --scope=@myco
Previous:
Understanding npm-install, npm-install-ci-test, and npm-install-test CLI Commands.
Next:
Mastering npm-ls and npm CLI Commands for Effective Package Management.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/npm/npm-link-and-npm-logout-cli-option.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics