Use npm
(node package manager) via Python
. Optionally also executable via SSH using the paramiko
module.
This code ultimately does nothing other than execute npm
via subprocess
. paramiko
is used for SSH
so that npm
can also be executed on external computers using Python
scripts. However, it is assumed that npm
is installed on the (target) computers.
Why do we even need a Python wrapper that executes npm
? It makes it easier to automate npm
in smart home software such as Home Assistant, openHAB and the like.
This code could be run in Python 2.7
and Python 3.x
, so you could choose how to use it. You have to install the paramiko
module.
For Python 2.7
you have to run:
python -m pip install paramiko
For Python 3.x
you have to run:
python3 -m pip install paramiko
Regardless of whether you are working locally or remotely, you must always import the NPM
module as follows:
from npm import NPM
Each function has a shell_check
parameter which is by default True
. This means that
NPM.start(args=["--port", "3000"])
is equivalent to
NPM.start(args=["--port", "3000"], shell_check=True)
This means that when the Python script is executed, the output of subprocess appears in the command line. There is virtually no difference to when I have entered the corresponding command directly in the command line instead of my Python programme. If you want more complex Python scripts for automation, you want to pass shell_check=False
to the function and save the return value of the function in a variable. You can of course check this for possible errors or whether everything has worked and can then continue your programme based on this (and possibly execute further npm commands).
To execute SSH
, paramiko
is used. SSH` can be executed here for every function. An example looks like this:
from npm import RemoteNPM
Remote = RemoteNPM(remote_host='your_remote_host', remote_user='your_username', remote_password='your_password')
Remote.access_public(package='mypackage', shell_check=True)
the local equivalent looks like this:
NPM.access_public(package='mypackage', shell_check=True)
Set access level on published packages
NPM.access_2fa_required("mypackage")
NPM.access_2fa_not_required("mypackage")
NPM.access_ls_packages("@myorg")
NPM.access_ls_collaborators("mypackage", "myuser")
NPM.access_edit("mypackage")
Add a registry user account
NPM.adduser(registry="https://registry.example.com", scope="@myorg", always_auth=True, auth_type="legacy")
Run a security audit
NPM.audit(output_format="json", audit_level="high", production=True, only="prod")
NPM.audit_fix(force=True, package_lock_only=True, dry_run=True)
Display npm bin folder
NPM.bin(global_install=True)
Bugs for a package in a web browser maybe
NPM.bugs("mypackage")
Build a package
NPM.build("myproject")
Manipulates packages cache
NPM.cache_add_tarball_file("/path/to/package.tgz")
NPM.cache_add_folder("/path/to/package")
NPM.cache_add_tarball_url("https://example.com/package.tgz")
NPM.cache_add_package("mypackage", "1.0.0")
NPM.cache_clean("/path/to/cache")
NPM.cache_verify()
Install a project with a clean slate
NPM.ci()
Tab completion for npm
NPM.source_npm_completion()
Manage the npm configuration files
NPM.config_set("registry", "https://registry.example.com", global_install=True)
NPM.config_get("registry")
NPM.config_delete("registry")
NPM.config_list(long_format=True, json_output=True)
NPM.config_edit()
NPM.npm_get("prefix")
NPM.npm_set("prefix", "/path/to/prefix", global_install=True)
Reduce duplication
NPM.dedupe()
NPM.ddp()
Deprecate a version of a package
NPM.deprecate("mypackage", "1.0.0", "This package is deprecated.")
Modify package distribution tags
NPM.dist_tag_add("mypackage", "1.0.0", "latest")
NPM.dist_tag_rm("mypackage", "beta")
NPM.dist_tag_ls("mypackage")
Docs for a package in a web browser maybe
NPM.docs(["mypackage", "anotherpackage"])
NPM.home(["mypackage", "anotherpackage"])
Check your environments
NPM.doctor()
Edit an installed package
NPM.edit("mypackage")
Browse an installed package
NPM.explore("mypackage", ["--", "ls"])
Retrieve funding information
NPM.fund("mypackage")
Search npm help documentation
NPM.npm_help("install", ["--save"])
Get help on npm
NPM.help_search("npm scripts")
Manage registry hooks
NPM.hook_ls("mypackage")
NPM.hook_add("myentity", "https://example.com/hook", "mysecret")
NPM.hook_update("123", "https://example.com/newhook", "newsecret")
NPM.hook_rm("456")
Create a package.json file
NPM.init(force=True, scope="@myorg")
NPM.init_scope("@myorg")
NPM.init_name("myproject")
Install a package
NPM.install()
NPM.install("@myorg/mypackage")
NPM.install("mypackage", tag="latest")
NPM.install("mypackage", version="1.0.0")
NPM.install("mypackage", version_range="^1.0.0")
NPM.install(alias="myalias", git_alias="mygitalias", git_repo="https://github.com/user/repo.git")
NPM.install(git_repo="https://github.com/user/repo.git")
NPM.install(tarball_file="/path/to/package.tgz")
NPM.install(tarball_url="https://example.com/package.tgz")
NPM.install(folder="/path/to/package")
Install a project with a clean slate and run tests
NPM.install_ci_test()
Install package(s) and run tests
NPM.install_test()
NPM.install_test("@myorg/mypackage")
NPM.install_test("mypackage", tag="latest")
NPM.install_test("mypackage", version="1.0.0")
NPM.install_test("mypackage", version_range="^1.0.0")
NPM.install_test(tarball_file="/path/to/package.tgz")
NPM.install_test(tarball_url="https://example.com/package.tgz")
NPM.install_test(folder="/path/to/package")
Symlink a package folder
NPM.link() # In einem Paketverzeichnis
NPM.link("mypackage")
NPM.link("mypackage", version="1.0.0", scope="@myorg")
Log out of the registry
NPM.logout(registry="https://example.com", scope="@myorg")
List installed packages
NPM.ls(["mypackage", "@anotherorg/anotherpackage"])
Manage orgs
NPM.org_set("myorg", "myuser", "developer")
NPM.org_rm("myorg", "myuser")
NPM.org_ls("myorg", "myuser")
Check for outdated packages
NPM.outdated(["mypackage", "@anotherorg/anotherpackage"])
Manage package owners
NPM.owner_add("newowner", "mypackage", scope="@myorg")
NPM.owner_rm("oldowner", "mypackage")
NPM.owner_ls("mypackage", scope="@myorg")
Create a tarball from a package
NPM.pack(["mypackage", "@anotherorg/anotherpackage"], dry_run=True)
Ping npm registry
NPM.ping(registry="https://example.com")
Display prefix
NPM.prefix(global_install=True)
Change settings on your registry profile
NPM.profile_get(parseable=True, json_output=True, property="username")
NPM.profile_set("email", "[email protected]", json_output=True)
NPM.profile_set_password()
NPM.profile_enable_2fa(mode="auth-and-writes")
NPM.profile_disable_2fa()
Remove extraneous packages
NPM.prune(["mypackage", "@anotherorg/anotherpackage"], production=True, dry_run=True, json_output=True)
Publish a package
NPM.publish(tarball_or_folder="mypackage.tgz", tag="beta", access="public", otp="123456", dry_run=True)
Rebuild a package
NPM.rebuild(["@myorg/mypackage"])
Open package repository page in the browser
NPM.repo("mypackage")
Restart a package
NPM.restart(["--", "--debug"])
Display npm root
NPM.root(global_install=True)
Run arbitrary package scripts
NPM.run_script("build", silent=True, args=["--prod"])
Search for packages
NPM.search(["package-name"], long_format=True, json_output=True, parseable=True)
Lock down dependency versions for publication
NPM.shrinkwrap()
Mark your favorite packages
NPM.star(["mypackage"])
NPM.unstar(["mypackage"])
View packages marked as favorites
NPM.stars("myuser")
Start a package
NPM.start(args=["--port", "3000"])
Stop a package
NPM.stop(args=["--port", "3000"])
Manage organization teams and team memberships
NPM.team_create("@myorg:developers")
NPM.team_destroy("@myorg:developers")
NPM.team_add("@myorg:developers", "user123")
NPM.team_rm("@myorg:developers", "user123")
NPM.team_ls("@myorg:developers")
NPM.team_edit("@myorg:developers")
Test a package
NPM.test(args=["--verbose"])
Manage your authentication tokens
NPM.token_list(json_output=True, parseable=True)
NPM.token_create(read_only=True, cidr="1.1.1.1/24,2.2.2.2/16")
NPM.token_revoke("token123")
Remove a package
NPM.uninstall(["mypackage"], save=True)
Remove a package from the registry
NPM.unpublish("mypackage", version="1.0.0", force=True)
Update a package
NPM.update(["mypackage"], global_install=True)
Bump a package version
NPM.version("2.0.0")
View registry info
NPM.view("mypackage", version="1.0.0", field="dependencies")
Display npm username
NPM.whoami(registry="https://example.com")