node.js 找不到 http-server 命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46851400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:32:36  来源:igfitidea点击:

http-server command not found

node.jshttpserver

提问by Jordan Forbes

I'm trying to run a basic http-server to test out some html files and I keep running into the same error.

我正在尝试运行一个基本的 http 服务器来测试一些 html 文件,但我一直遇到同样的错误。

I did sudo npm install -g http-servera bunch of times, but each time I try

我做 sudo npm install -g http-server了很多次,但每次我都尝试

simple git:(master) http-server

I keep getting this error:

我不断收到此错误:

zsh: command not found: http-server

zsh: command not found: http-server

I've tried other variations such as http-server / 8000, I've tried using different command syntax but nothing seems to be working.

我尝试过其他变体,例如 http-server / 8000,我尝试过使用不同的命令语法,但似乎没有任何效果。

my npm version is 5.5.1, my node version is 8.3.0, and my OS is Mac OSX Sierra v10.12.6

我的 npm 版本是 5.5.1,我的节点版本是 8.3.0,我的操作系统是 Mac OSX Sierra v10.12.6

Please let me know if you can help or if you see anything I'm doing wrong that I'm just not noticing.

请让我知道您是否可以提供帮助,或者如果您看到我做错了什么而我没有注意到。

edit: tiny update I was able to get a server going with python but I'd really like to know why this wasn't working.

编辑:微小的更新我能够让服务器使用 python,但我真的很想知道为什么这不起作用。

edit 2: problem solved thanks!

编辑2:问题解决了谢谢!

回答by zero298

You may not have your npm binaries in PATH.

你可能没有你的 npm 二进制文件PATH

Make sure that your npm binaries are in path by running echo $PATH. You should see, somewhere in the printed output, something like:

通过运行确保您的 npm 二进制文件在路径中echo $PATH。您应该在打印输出的某处看到类似以下内容的内容:

/home/bob/.npm-packages/bin:/usr/lobal/bin:/other/paths/that/contain/bins

/home/bob/.npm-packages/binis the directory where my npm binaries are installed whenever I run npm -g install whatever.

/home/bob/.npm-packages/bin是每次运行 .npm 时安装 npm 二进制文件的目录npm -g install whatever

If you don't see something like that, read Fixing npm permissionswhich will walk you through making sure that your environment is set up correctly. Option 2 explicitly talks about fixing PATH.

如果您没有看到类似的内容,请阅读修复 npm 权限,这将引导您确保您的环境设置正确。选项 2 明确谈到修复PATH

Another handy thing that I usually do is add all this to my .bashrcor .bashprofilewhich is in your home directory:

我通常做的另一件方便的事情是将所有这些添加到 my.bashrc.bashprofile您的主目录中:

  • On macOS /Users/username/
  • On *nix: /home/username/
  • 在 macOS 上 /Users/username/
  • 在 *nix 上: /home/username/

.bashrc

.bashrc

NPM_PACKAGES="${HOME}/.npm-packages"
PATH="$NPM_PACKAGES/bin:$PATH"

However, since it looks like you are using zshell, you'll have to use whatever convention they follow for rc files.

但是,由于看起来您正在使用 zshell,因此您必须使用它们对 rc 文件遵循的任何约定。



You can either fix that or, you can install http-server at a package level for your project and then start it through an npm command.

您可以修复该问题,也可以在项目的包级别安装 http-server,然后通过 npm 命令启动它。

Run npm install --save-dev http-server

npm install --save-dev http-server

and put in your package.json:

并放入你的 package.json:

{
    "scripts": {
        "start": "http-server ."
    }
}

and then run

然后运行

npm start

回答by Explosion Pills

If you do sudo npm install -gand you don't have an npmprefix set, the file is probablyinstalled in /usr/local/bin, but it could be installed more or less anywhere. Whatever directory it's being installed to is probably not in your $PATH, so you should figure out where it's installed and then update your $PATHto include it.

如果您这样做了sudo npm install -g并且您没有npm设置前缀,则该文件可能安装在 中/usr/local/bin,但它可以或多或少地安装在任何地方。无论它被安装到目录可能不是你的$PATH,所以你应该弄清楚它的安装位置,然后更新您的$PATH包含它。

I would very strongly recommend you avoid avoid running sudowith npm. Instead, you can update your prefixin your npm configuration (or update manually in ~/.npmrcto something like prefix=~/.npmbut it's up to you. Then you also have to make sure that ~/.npm/binis in your path.

我强烈建议您避免sudo使用 npm 运行。相反,您可以prefix在您的 npm 配置中更新您的配置(或手动更新~/.npmrc为类似的内容,prefix=~/.npm但这取决于您。然后您还必须确保它~/.npm/bin在您的路径中。

Even though global packages can be handy sometimes, if they are tied to particular projects I think it's better to just install it as part of the project and run it there:

尽管有时全局包可能很方便,但如果它们与特定项目相关联,我认为最好将其作为项目的一部分安装并在那里运行:

cd /path/to/project/with/http-server
npm install http-server

# any of:
npx http-server
node_modules/.bin/http-server

Also if you have a package.json#scripts, you can reference any locally installed binary as in:

此外,如果您有一个package.json#scripts,则可以引用任何本地安装的二进制文件,如下所示:

"scripts": {
  "server": "http-server"
}

and then use npm run server.

然后使用npm run server.

回答by Lijo

npm install http-server -g.

please install http-server globally and there after u can run the command

请全局安装http-server,然后你就可以运行命令了

http-server -o