简体   繁体   English

如何在node.exe中使用npm?

[英]How to use npm with node.exe?

I have downloaded Windows Binary (.exe) of nodejs from the main page . 我从主页面下载了nodejs的Windows二进制文件(.exe)。

How can I install and use npm (Node package manager)? 如何安装和使用npm (节点包管理器)?

The current windows installer from nodejs.org as of v0.6.11 (2012-02-20) will install NPM along with NodeJS. v0.6.11 (2012-02-20)起, nodejs.org中的当前Windows安装程序将与NodeJS一起安装NPM。

NOTES: 笔记:

  • At this point, the 64-bit version is your best bet 此时,64位版本是您最好的选择
  • The install path for 32-bit node is "Program Files (x86)" in 64-bit windows. 32位节点的安装路径是64位窗口中的“Program Files(x86)”。
  • You may also need to add quotes to the path statement in environment variables, this only seems to be in some cases that I've seen. 您可能还需要在环境变量中的路径语句中添加引号,这似乎只是在我见过的某些情况下。
  • In Windows, the global install path is actually in your user's profile directory 在Windows中, 全局安装路径实际上位于用户的配置文件目录中
    • %USERPROFILE%\\AppData\\Roaming\\npm
    • %USERPROFILE%\\AppData\\Roaming\\npm-cache
    • WARNING: If you're doing timed events or other automation as a different user, make sure you run npm install as that user. 警告:如果您正在以其他用户身份执行定时事件或其他自动化,请确保以该用户身份运行npm install Some modules/utilities should be installed globally. 一些模块/实用程序应该全局安装。
    • INSTALLER BUGS: You may have to create these directories or add the ...\\npm directory to your users path yourself. 安装程序错误:您可能必须自己创建这些目录或将...\\npm目录添加到用户路径。

To change the "global" location for all users to a more appropriate shared global location %ALLUSERSPROFILE%\\(npm|npm-cache) (do this as an administrator): 要将所有用户的“全局”位置更改为更合适的共享全局位置%ALLUSERSPROFILE%\\(npm|npm-cache) (以管理员身份执行此操作):

  • create an [NODE_INSTALL_PATH]\\etc\\ directory 创建[NODE_INSTALL_PATH]\\etc\\目录
    • this is needed before you try npm config --global ... actions 在尝试npm config --global ... actions之前需npm config --global ...
  • create the global (admin) location(s) for npm modules 为npm模块创建全局(admin)位置
    • C:\\ProgramData\\npm-cache - npm modules will go here C:\\ProgramData\\npm-cache - npm模块将在这里
    • C:\\ProgramData\\npm - binary scripts for globally installed modules will go here C:\\ProgramData\\npm - 全局安装模块的二进制脚本将在这里
    • C:\\ProgramData\\npm\\node_modules - globally installed modules will go here C:\\ProgramData\\npm\\node_modules - 全局安装的模块将在这里
    • set the permissions appropriately 适当地设置权限
      • administrators: modify 管理员:修改
      • authenticated users: read/execute 经过身份验证的用户:读取/执行
  • Set global configuration settings (Administrator Command Prompt) 设置全局配置设置(管理员命令提示符)
    • npm config --global set prefix "C:\\ProgramData\\npm"
    • npm config --global set cache "C:\\ProgramData\\npm-cache"
  • Add C:\\ProgramData\\npm to your System's Path environment variable C:\\ProgramData\\npm添加到系统的Path环境变量中

If you want to change your user's "global" location to %LOCALAPPDATA%\\(npm|npm-cache) path instead: 如果要将用户的“全局”位置更改为%LOCALAPPDATA%\\(npm|npm-cache)路径,请执行以下操作:

  • Create the necessary directories 创建必要的目录
    • C:\\Users\\YOURNAME\\AppData\\Local\\npm-cache - npm modules will go here C:\\Users\\YOURNAME\\AppData\\Local\\npm-cache - npm模块将转到此处
    • C:\\Users\\YOURNAME\\AppData\\Local\\npm - binary scripts for installed modules will go here C:\\Users\\YOURNAME\\AppData\\Local\\npm - 已安装模块的二进制脚本将在此处
    • C:\\Users\\YOURNAME\\AppData\\Local\\npm\\node_modules - globally installed modules will go here C:\\Users\\YOURNAME\\AppData\\Local\\npm\\node_modules - 全局安装的模块将在此处
  • Configure npm 配置npm
    • npm config set prefix "C:\\Users\\YOURNAME\\AppData\\Local\\npm"
    • npm config set cache "C:\\Users\\YOURNAME\\AppData\\Local\\npm-cache"
  • Add the new npm path to your environment's PATH . 将新的npm路径添加到环境的PATH
    • setx PATH "%PATH%;C:\\Users\\YOURNAME\\AppData\\Local\\npm"

For beginners, some of the npm modules I've made the most use of are as follows. 对于初学者,我最常使用的一些npm模块如下。

More advanced JS options... 更高级的JS选项......

For testing, I reach for the following tools... 为了测试,我找到了以下工具......

  • mocha - testing framework 摩卡 - 测试框架
  • chai - assertion library, I like chai.expect chai - 断言库,我喜欢chai.expect
  • sinon - spies and stubs and shims sinon - 间谍和存根和垫片
  • sinon-chai - extend chai with sinon's assertion tools sinon-chai - 用sinon的断言工具扩展chai
  • babel-istanbul - coverage reports babel-istanbul - 报道报道
  • jest - parallel testing, assertions, mocking, coverage reports in one tool jest - 在一个工具中进行并行测试,断言,模拟,覆盖报告
  • babel-plugin-rewire - slightly easier for some mocking conditions vs. jest babel-plugin-rewire - 对于一些嘲讽条件和开玩笑稍微容易一些

Web tooling. 网络工具。

  • webpack - module bundler, package node-style modules for browser usage webpack - 模块捆绑器,用于浏览器使用的包节点式模块
  • babel - convert modern JS (ES2015+) syntax for your deployment environment. babel - 为您的部署环境转换现代JS(ES2015 +)语法。

If you build it... 如果你建造它......

  • shelljs - shell utilities for node scripts,. shelljs - 节点脚本的shell实用程序。 I used to use gulp/grunt, but these days will have a scripts directory that's referenced in package.json scripts via npm. 我以前使用gulp / grunt,但是现在将有一个scripts目录,通过npm在package.json 脚本中引用。 You can use gulp tools inside plain scripts. 您可以在普通脚本中使用gulp工具。

When Node.js is not installed using the msi installer , npm needs to be setup manually. 如果未使用msi安装程序安装 Node.js,则需要手动设置npm

setting up npm 设置npm

First, let's say we have the node.exe file located in the folder c:\\nodejs . 首先,假设我们将node.exe文件放在文件夹c:\\nodejs Now to setup npm - 现在设置npm -

  1. Download the latest npm release from GitHub ( https://github.com/npm/npm/releases ) 从GitHub下载最新的npm版本( https://github.com/npm/npm/releases
  2. Create folders c:\\nodejs\\node_modules and c:\\nodejs\\node_modules\\npm 创建文件夹c:\\nodejs\\node_modulesc:\\nodejs\\node_modules\\npm
  3. Unzip the downloaded zip file in c:\\nodejs\\node_modules\\npm folder 将下载的zip文件解压缩到c:\\nodejs\\node_modules\\npm文件夹中
  4. Copy npm and npm.cmd files from c:\\nodejs\\node_modules\\npm\\bin to c:\\nodejs folder npmnpm.cmd文件从c:\\nodejs\\node_modules\\npm\\bin复制到c:\\nodejs文件夹

In order to test npm , open cmd.exe change working directory to c:\\nodejs and type npm --version . 为了测试npm ,打开cmd.exe将工作目录更改为c:\\nodejs并键入npm --version You will see the version of npm if it is setup correctly. 如果设置正确,您将看到npm的版本。

Once setup is done, it can be used to install/uninstall packages locally or globally. 设置完成后,可以使用它在本地或全局安装/卸载软件包。 For more information on using npm visit https://docs.npmjs.com/ . 有关使用npm的更多信息,请访问https://docs.npmjs.com/

As the final step you can add node's folder path c:\\nodejs to the path environment variable so that you don't have to specify full path when running node.exe and npm at command prompt. 作为最后一步,您可以将节点的文件夹路径c:\\nodejspath环境变量中,这样您就不必在命令提示符下运行node.exenpm时指定完整路径。

npm can be downloaded (without installation) from here: npm可以从这里下载(无需安装):

http://nodejs.org/dist/npm/ http://nodejs.org/dist/npm/

https://github.com/npm/npm/releases https://github.com/npm/npm/releases

I just installed latest version of node (0.6.12) in Windows 7 using msi (node-v0.6.12.msi). 我刚刚使用msi(node-v0.6.12.msi)在Windows 7中安装了最新版本的节点(0.6.12)。

npm is already shipped with it, no need to include it separately. npm已经附带,不需要单独包含它。

I was facing permission issue while running npm (npm install mysql), from the path where my nodejs resided, ie C:\\Program Files (x86)\\nodejs 从我的nodejs所在的路径运行npm(npm install mysql)时,我遇到了权限问题,即C:\\ Program Files(x86)\\ nodejs

Then I followed below steps: 然后我按照以下步骤操作:

1) Added C:\\Program Files (x86)\\nodejs\\npm in environment variables - Path system variable. 1)在环境变量中添加了C:\\Program Files (x86)\\nodejs\\npm - 路径系统变量。

2) went back to only C:\\ in command prompt and gave the command - npm install mysql - and voila! 2)在命令提示符下回到只有C:\\并给出了命令npm install mysql - 瞧! it worked.. 有效..

Hope this helps. 希望这可以帮助。

I am running node.js on Windows with npm. 我用npm在Windows上运行node.js. The trick is simply use cygwin. 诀窍就是使用cygwin。 I followed the howto under https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows) . 我按照https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows)下的howto进行了操作。 But make sure that you use version 0.4.11 of nodejs or npm will fail! 但请确保使用版本0.4.11的nodejs或npm将失败!

I've just installed 64 bit Node.js v0.12.0 for Windows 8.1 from here . 我刚刚安装64位的Node.js v0.12.0为Windows 8.1从这里 It's about 8MB and since it's an MSI you just double click to launch. 它大约是8MB,因为它是MSI,你只需双击即可启动。 It will automatically set up your environment paths etc. 它会自动设置您的环境路径等。

Then to get the command line it's just [Win-Key]+[S] for search and then enter "node.js" as your search phrase. 然后获取命令行,只需[Win-Key]+[S]进行搜索,然后输入“node.js”作为搜索短语。

Choose the Node.js Command Prompt entry NOT the Node.js entry. 选择Node.js Command Prompt条目而不是Node.js条目。

Both will given you a command prompt but only the former will actually work. 两者都会给你一个命令提示符,但只有前者才会真正起作用。 npm is built into that download so then just npm -whatever at prompt. npm内置于该下载中,因此只需npm -whatever即时。

Use a Windows Package manager like chocolatey. 使用像巧克力这样的Windows程序包管理器。 First install chocolatey as indicated on it's homepage . 首先安装巧克力,如其主页上所示 That should be a breeze 那应该是轻而易举的事

Then, to install Node JS (Install), run the following command from the command line or from PowerShell: 然后,要安装Node JS(安装),请从命令行或从PowerShell运行以下命令:

C:> cinst nodejs.install C:> cinst nodejs.install

Here is a guide by @CTS_AE on how to use NPM with standalone node.exe: https://stackoverflow.com/a/31148216/228508 以下是@CTS_AE关于如何将NPM与独立node.exe一起使用的指南: https ://stackoverflow.com/a/31148216/228508

  1. Download the node.exe stand-alone from nodejs.org 从nodejs.org下载node.exe独立版
  2. Grab an NPM release zip off of github https://github.com/npm/npm/releases 从github https://github.com/npm/npm/releases获取 NPM发行版zip
  3. Create a folder named: node_modules in the same folder as node.exe 在与node.exe相同的文件夹中创建名为:node_modules的文件夹
  4. Extract the NPM zip into the node_modules folder 将NPM zip解压缩到node_modules文件夹中
  5. Rename the extracted npm folder to npm and remove any versioning ie: npm-3.3.4 –> npm. 将提取的npm文件夹重命名为npm并删除任何版本,即:npm-3.3.4 - > npm。
  6. Copy npm.cmd out of the /npm/bin/ folder into the root folder with node.exe 使用node.exe将npm.cmd从/ npm / bin /文件夹复制到根文件夹中

I just installed Node.js for the first time and it includes NPM, which can be ran from the Windows cmd. 我刚刚第一次安装了Node.js,它包含NPM,可以从Windows cmd运行。 However, make sure that you run it as an administrator. 但是,请确保以管理员身份运行它。 Right click on cmd and choose "run as administrator". 右键单击cmd并选择“以管理员身份运行”。 This allowed me to call npm commands. 这允许我调用npm命令。

Search all .npmrc file in your system. 搜索系统中的所有.npmrc文件。

Please verify that the path you have given is correct. 请验证您提供的路径是否正确。 If not please remove the incorrect path. 如果没有,请删除不正确的路径。

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

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