node.js 如何解决“npm 应该在节点 repl 之外,在您的普通 shell 中运行”的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24476805/
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
How to resolve 'npm should be run outside of the node repl, in your normal shell'
提问by Aniket Thakur
I am new to node.js. I downloaded and install node.js installer from the official site. I have added this installer folder in PATH environment variable and I am able to run programs. But when I try to install some package using npm in node console it shows the error npm should be run outside of the node repl, in your normal shell. I also tried it on a separate console. But it shows command not found though I have added node in the environment variable.
我是 node.js 的新手。我从官方网站下载并安装了 node.js 安装程序。我已经在 PATH 环境变量中添加了这个安装程序文件夹,我能够运行程序。但是当我尝试在节点控制台中使用 npm 安装一些包时,它显示错误npm should be run outside of the node repl, in your normal shell。我也在单独的控制台上尝试过。但是它显示找不到命令,尽管我在环境变量中添加了节点。




采纳答案by mscdex
It's better to use the actual (msi) installer from nodejs.orginstead of downloading the node executable only. The installer includes npm and makes it easier to manage your node installation. There is an installer for both 32-bit and 64-bit Windows.
最好使用来自nodejs.org的实际 (msi) 安装程序,而不是仅下载节点可执行文件。安装程序包含 npm,可以更轻松地管理您的节点安装。有适用于 32 位和 64 位 Windows 的安装程序。
Also a couple of other tidbits:
还有一些其他花絮:
Installing modules globally doesn't do what you might expect. The only modules you should install globally (the
-gflag in npm) are ones that install commands. So to install Express you would just donpm install expressand that will install Express to your current working directory. If you were instead looking for the Express project generator (command), you need to donpm install -g express-generatorfor Express 4.You can use node anywhere from your command prompt to execute scripts. For example if you have already written a separate script:
node foo.js. Or you can open up the REPL (as you've already found out) by just selecting the node.js (start menu) shortcut or by just typingnodein a command prompt.
全局安装模块不会做你所期望的。您应该全局安装的唯一模块(
-gnpm 中的标志)是安装命令的模块。因此,要安装 Express,您只需这样做npm install express,然后将 Express 安装到您当前的工作目录。如果您正在寻找 Express项目生成器(命令),则需要npm install -g express-generator为 Express 4做。您可以从命令提示符的任何位置使用 node 来执行脚本。例如,如果您已经编写了单独的脚本:
node foo.js. 或者,您可以通过选择 node.js(开始菜单)快捷方式或node在命令提示符中键入来打开 REPL(正如您已经发现的那样)。
回答by James Lawruk
For Windows users, run npmcommands from the Command Prompt (cmd.exe), not Node.Js(node.exe). So your "normal shell"is cmd.exe. (I agree this message can be confusing for a Windows, Node newbie.)
对于 Windows 用户,npm从命令提示符 (cmd.exe)运行命令,而不是Node.Js(node.exe)。所以你的“普通外壳”是 cmd.exe。 (我同意此消息对于 Windows 和 Node 新手来说可能会令人困惑。)
By the way, the Node.js Command Promptis actually just an easy shortcut to cmd.exe.
顺便说一句,Node.js 命令提示符实际上只是cmd.exe 的一个简单快捷方式。
Below is an example screenshot for installing grunt from cmd.exe:
下面是从 cmd.exe 安装 grunt 的示例截图:


回答by Aman
Do not run the application using node.js icon.
不要使用 node.js 图标运行应用程序。
Go to All Programmes->Node.js->Node.js command prompt.
转到所有程序->Node.js->Node.js 命令提示符。
Below is example screen shot.
下面是示例屏幕截图。




回答by CTS_AE
If you're like me running in a restricted environment without administrative privileges, that means your only way to get node up and running is to grab the executable (node.exe) without using the installer. You also cannot change the path variable which makes it that much more challenging.
如果您像我一样在没有管理权限的受限环境中运行,这意味着您启动和运行 node 的唯一方法是在不使用安装程序的情况下获取可执行文件 (node.exe)。您也无法更改路径变量,这使其更具挑战性。
Here's what I did (for Windows)
这就是我所做的(对于 Windows)
- Throw node.exe into its own folder (Downloaded the node.exe stand-alone)
- Grab an NPM release zip off of github: https://github.com/npm/npm/releases
- Create a folder named: node_modules in the node.exe folder
- Extract the NPM zip into the node_modules folder
- Make sure the top most folder is named npm (remove any of the versioning on the npm folder name ie: npm-2.12.1 --> npm)
- Copy npm.cmd out of the npm/bin folder into the top most folder with node.exe
- Open a command prompt to the node.exe directory (shift right-click "Open command window here")
- Now you will be able to run your npm installers via:
npm install -g express
- 将 node.exe 放入自己的文件夹中(单独下载 node.exe )
- 从 github 上获取 NPM 发布 zip:https: //github.com/npm/npm/releases
- 在 node.exe 文件夹中创建一个名为:node_modules 的文件夹
- 将 NPM zip 解压到 node_modules 文件夹中
- 确保最顶层的文件夹名为 npm(删除 npm 文件夹名称上的任何版本控制,即:npm-2.12.1 --> npm)
- 将 npm.cmd 从 npm/bin 文件夹中复制到带有 node.exe 的最顶层文件夹中
- 打开 node.exe 目录的命令提示符(shift 右键单击“在此处打开命令窗口”)
- 现在您将能够通过以下方式运行您的 npm 安装程序:
npm install -g express
Running the installers through npm will now auto install packages where they need to be located (node_modules and the root)
通过 npm 运行安装程序现在将在需要放置的地方(node_modules 和 root)自动安装包
Don't forget you will not be able to set the path variable if you do not have proper permissions. So your best route is to open a command prompt in the node.exe directory (shift right-click "Open command window here")
不要忘记,如果您没有适当的权限,您将无法设置路径变量。所以你最好的方法是在 node.exe 目录中打开一个命令提示符(shift 右键单击“在此处打开命令窗口”)
回答by Aniket Thakur
As mscdex said NPM comes with the nodejs msi installed file. I happened to just install the node js installer (standalone). To separately add NPM I followed following step
正如 mscdex 所说,NPM 带有 nodejs msi 安装文件。我碰巧只安装了 node js 安装程序(独立)。要单独添加 NPM,我遵循以下步骤
- Download the latest zip file of NPM from here.
- Extract it in the same file as that of node js installer.
- If you have added the directory containing to node js installer to PATH env variable then now even npm should be a recognized command.
- 从这里下载 NPM 的最新 zip 文件。
- 将其解压缩到与 node js 安装程序相同的文件中。
- 如果您已将包含 node js 安装程序的目录添加到 PATH 环境变量,那么现在甚至 npm 也应该是一个可识别的命令。
回答by Aarji George
回答by NuuoeiZ
You must get directory right path of program(node.js in program files).
您必须获得程序的目录正确路径(程序文件中的node.js)。
such as
如


and use "npm install -g phonegap"
并使用“npm install -g phonegap”
回答by NuuoeiZ
It's simple. Press the Windows logo on your keyboard. Then, type node.js command promptin the search bar and run it.

这很简单。按键盘上的 Windows 徽标。然后,在搜索栏中键入node.js 命令提示符并运行它。


