简体   繁体   English

我的 NPM CLI package 可以在 CMD 上执行而无需全局安装吗?

[英]Can my NPM CLI package be executed on CMD without installing globally?

I have written an NPM package which has its own CLI commands.我写了一个 NPM package ,它有自己的 CLI 命令。

Let's name that package as xyz and imagine it's now avaialable on npmjs.com让我们将 package 命名为xyz并想象它现在可以在npmjs.com上使用

So, let's say a user installs this package in his project by running npm install xyz .因此,假设用户通过运行npm install xyz在他的项目中安装此 package 。

And now he wants to run a CLI command provided by xyz package on his terminal in his project.现在他想在他的项目中的终端上运行xyz package 提供的 CLI 命令。

xyz do_this

Can this be done without installing this package globally by user?这可以在不由用户全局安装此 package 的情况下完成吗? Or without any further configuration for the user?或者没有任何进一步的用户配置?

Here's some part of the package.json of the xyz package.这是 xyz package 的package.json的一部分。

{
  "name": "xyz",
  "version": "1.0.0",
  "description": "Description",
  "main": "index.js",
  "preferGlobal": true,
  "bin": "./index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
........

Here's how npm works.以下是 npm 的工作原理。 When you install a package in the local directory, it installs the executable dependencies files inside node_modules inside the folder with package.json, but if you use --global, it places it globally where the user has set their path.当您在本地目录中安装 package 时,它会将可执行的依赖文件安装在 node_modules 中的文件夹内,该文件夹位于 package.json 的文件夹中,但如果您将其全局设置为全局路径。 For example, when I run npm install http-server , my executable ends up as ./node_modules/http-server/bin/http-server but when i install it globally, I have it as node_modules/http-server/bin例如,当我运行npm install http-server时,我的可执行文件以./node_modules/http-server/bin/http-server结尾,但是当我全局安装它时,我将它作为node_modules/http-server/bin

The work around for this is, if you want to just run the executable, just execute it inside like so ./node_modules/http-server/bin/http-server .解决这个问题的方法是,如果您只想运行可执行文件,只需在内部执行它,就像这样./node_modules/http-server/bin/http-server If you want it as a command, you'll need too have the user add the directory to their Path so when the user enters the command, the computer looks for the command inside that folder.如果您希望它作为命令,您还需要让用户将目录添加到他们的路径中,这样当用户输入命令时,计算机会在该文件夹中查找命令。 Here's a guide to adding PATH directories in Linux, you'll just add the directory /pathtofolder/node_modules/http-server/bin/ or whatever your folder is.这是在 Linux 中添加 PATH 目录的指南,您只需添加目录/pathtofolder/node_modules/http-server/bin/或任何您的文件夹。 https://linuxize.com/post/how-to-add-directory-to-path-in-linux/ https://linuxize.com/post/how-to-add-directory-to-path-in-linux/

For example, if I wanted to add http-server from my local folder to my path, I would run例如,如果我想将本地文件夹中的 http-server 添加到我的路径中,我会运行

export PATH="/pathtofolder/node_modules/http-server/bin/:$PATH"

Good luck!祝你好运! Let me know how I can help you!让我知道我可以如何帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM