简体   繁体   English

如何在 Visual Studio 2017 中使用 NPM 和安装包?

[英]How to use NPM and install packages inside Visual Studio 2017?

I have a simple Visual Studio solution, running ASP.NET Core v2 and building a React app.我有一个简单的 Visual Studio 解决方案,运行 ASP.NET Core v2 并构建一个 React 应用程序。

Now, I want to install a simple component using the NPM.现在,我想使用 NPM 安装一个简单的组件。 In this particular example, it could be:在这个特定的例子中,它可能是:

npm install --save react-bootstrap-typeahead

I want this package to work just in my solution and nowhere else.我希望这个包只在我的解决方案中工作,而不是在其他地方。

My result:我的结果:

When I run this, I get the following nice error which obviously makes some sense.当我运行它时,我得到以下很好的错误,这显然是有道理的。 If NPM believes it can find my project file at 'C:\\Users\\LarsHoldgaard\\package.json' , it's out of luck.如果 NPM 认为它可以在'C:\\Users\\LarsHoldgaard\\package.json'找到我的项目文件,那就太不走运了。 The correct path would be C:\\Users\\LarsHoldgaard\\Documents\\Github\\Likvido.CreditRisk\\Likvido.CreditRisk\\Likvido.CreditRisk .正确的路径是C:\\Users\\LarsHoldgaard\\Documents\\Github\\Likvido.CreditRisk\\Likvido.CreditRisk\\Likvido.CreditRisk

npm : npm WARN saveError ENOENT: no such file or directory, open 'C:\Users\LarsHoldgaard\package.json'
At line:1 char:1
+ npm install --save react-bootstrap-typeahead
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (npm WARN saveEr...d\package.json':String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

npm

WARN

enoent
 ENOENT: no such file or directory, open 'C:\Users\LarsHoldgaard\package.json'

npm

WARN
 grunt-sass@2.0.0 requires a peer of grunt@>=0.4.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-rating@1.0.6 requires a peer of react@>=0.13.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-bootstrap-typeahead@2.5.1 requires a peer of react@^0.14.0 || ^15.2.0 || ^16.0.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-bootstrap-typeahead@2.5.1 requires a peer of react-dom@^0.14.0 || ^15.2.0 || ^16.0.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 prop-types-extra@1.0.1 requires a peer of react@>=0.14.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-overlays@0.8.3 requires a peer of react@^0.14.9 || >=15.3.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-overlays@0.8.3 requires a peer of react-dom@^0.14.9 || >=15.3.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-onclickoutside@6.7.1 requires a peer of react@^15.5.x || ^16.x but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-onclickoutside@6.7.1 requires a peer of react-dom@^15.5.x || ^16.x but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-transition-group@2.2.1 requires a peer of react@>=15.0.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 react-transition-group@2.2.1 requires a peer of react-dom@>=15.0.0 but none is installed. You must install peer dependencies yourself.

npm

WARN
 LarsHoldgaard No description

npm

WARN
 LarsHoldgaard No repository field.

npm

WARN
 LarsHoldgaard No README data

npm

WARN
 LarsHoldgaard No license field.

My thinking:我的想法:

Being a console noob , I would guess I just needed to change my current folder.作为控制台菜鸟,我想我只需要更改当前文件夹。 But if I run dir , I am in the right folder, and I can see my package.json along with other files.但是如果我运行dir ,我就在正确的文件夹中,我可以看到我的package.json和其他文件。

What is the right way to install components?安装组件的正确方法是什么?

To avoid navigating manually to the correct directory use the "Open Command Line" extension from Mads Kristensen.为了避免手动导航到正确的目录,请使用 Mads Kristensen 的“Open Command Line”扩展。 It is available for free in the Marketplace.它可以在 Marketplace 中免费获得。 You find it here .你可以在这里找到它。

Once installed you can open a command prompt conviently directly from within Visual Studio.安装后,您可以直接从 Visual Studio 中方便地打开命令提示符。

在此处输入图片说明

Tipp: Use the Keyboard Shortcut ALT+Space instead of the context menu to open the command prompt.提示:使用键盘快捷键 ALT+Space 而不是上下文菜单来打开命令提示符。

You can then run your npm command:然后你可以运行你的 npm 命令:

npm install react-bootstrap-typeahead

As a side note: As of npm 5.0.0, installed modules are added as a dependency by default, so the --save option is no longer required.附带说明:从 npm 5.0.0 开始,已安装的模块默认添加为依赖项,因此不再需要 --save 选项。

Update 2019 : 2019 年更新

Developer Command Prompt and Developer Power Shell are now integrated into Visual Studio 2019 ( 16.2 Preview 2 ) - no need for an extension anymore.开发人员命令提示符和开发人员 Power Shell 现在已集成到 Visual Studio 2019(16.2 预览版 2)中 - 不再需要扩展。

You find them under Tools/Command Line您可以在Tools/Command Line下找到它们

By default no shortcut is assigned - so you have to do this yourself.默认情况下没有分配快捷方式 - 因此您必须自己执行此操作。

在此处输入图片说明

I think the easiest way is to simple use the UI, Visual Studio provides.我认为最简单的方法是简单地使用 Visual Studio 提供的 UI。

Create in the root of your project a package.json (Todo so, right click your project, add item and search for NPM . You will find a npm Configuration File ):在你的项目的根目录中创建一个package.json (这样做,右键单击你的项目,添加项目并搜索NPM 。你会找到一个npm Configuration File ):

{
  "name": "SomeName",
  "version": "1.0.0",
  "private": true,
  "devDependencies": {
    "react-bootstrap-typeahead": "*"
  }
}

Note that * is for the latest version.请注意, *表示最新版本。 This is not recommended.不建议这样做。 Better to specify the version you want.最好指定您想要的版本。 You will notice, that you have support of intellisence for versions and package names.您会注意到,您支持版本和包名称的智能。

Everytime you make changes to the json file, simple press CTRL + S .每次对 json 文件进行更改时,只需按CTRL + S 即可 Visual Studio automaticly calls NPM and restores the packages. Visual Studio 会自动调用 NPM 并还原包。

For how to use the command line, other have already answerd.关于如何使用命令行,其他人已经回答了。 But as being a command line noob myself, I prefer this way.但作为一个命令行菜鸟,我更喜欢这种方式。

You can use the Package Manager Console to run npm command.您可以使用包管理器控制台运行 npm 命令。

To open the Package Manager Console, go to Tools > Nuget Package Manager and select Package Manager Console and then enter your npm command.要打开包管理器控制台,请转到“工具”>“Nuget 包管理器”并选择“包管理器控制台”,然后输入您的 npm 命令。

在此处输入图片说明

在此处输入图片说明


Update:更新:

The latest visual studio 16.8.3 onwards, you will find the terminal built into visual studio.最新的visual studio 16.8.3 以后,你会发现visual studio 内置了终端。

You can find it by right-clicking at your solution or a shortcut Ctrl + ` :您可以通过右键单击您的解决方案或快捷键Ctrl + `来找到它: 在此处输入图片说明

在此处输入图片说明

  • In Window's Explorer, navigate to where the package.json file is located in your project.在 Window 的资源管理器中,导航到package.json文件在项目中的位置。
  • Create a folder named node_modules in the same directory as your package.json file在与package.json文件相同的目录中创建一个名为node_modules的文件夹
  • While holding the left [Shift] key, right click in the folder containing the project.json file.在按住左[Shift]键的同时,右键单击包含project.json文件的文件夹。
  • From the context menu select 'Open command window here'.从上下文菜单中选择“在此处打开命令窗口”。
  • Input your npm command npm install --save react-bootstrap-typeahead输入你的 npm 命令npm install --save react-bootstrap-typeahead

I use a different approach, configuring npm per SOLUTION, instead of per PROJECT.我使用不同的方法,按解决方案配置 npm,而不是按项目。
Please refer to my BLOG:请参考我的博客:

A better way to use Visual Studio with npm ( and Gulp ) 将 Visual Studio 与 npm(和 Gulp)一起使用的更好方法

It is working fine and you may use Command Line ou Package Manager Console do install/update/uninstall npm packages.它工作正常,您可以使用命令行或包管理器控制台安装/更新/卸载 npm 包。 I am currently using it with Visual Studio 2019 and ASP.NET Core MVC.我目前将它与 Visual Studio 2019 和 ASP.NET Core MVC 一起使用。

Adding my 2 cents from 2021.从 2021 年开始增加我的 2 美分。

Visual Studio comes with built-in npm support, no CLI required. Visual Studio 带有内置的 npm 支持,不需要 CLI。 VS can automatically install/restore packages in the background - on project open and/or after making changes to packages.json file. VS 可以在后台自动安装/恢复包 - 在项目打开时和/或在对packages.json文件进行更改后。 You can enable this here:您可以在此处启用此功能:

在此处输入图片说明

For example, here's an article about configuring automatic minification and compilation for js/css files via npm tool, using just naked Visual Studio (DISCLAIMER: I wrote that blog post myself last year)例如,这里有一篇关于通过 npm 工具为js/css文件配置自动缩小和编译的文章,仅使用裸 Visual Studio(免责声明:我去年自己写了那篇博文)

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

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