问题描述
我有一个简单的 Visual Studio 解决方案,运行 ASP.NET Core v2 并构建一个 React 应用程序.
现在,我想使用 NPM 安装一个简单的组件.在这个特定的例子中,它可能是:
npm install --save react-bootstrap-typeahead
我希望这个包只在我的解决方案中工作,而不是其他地方.
我的结果:
当我运行它时,我得到以下很好的错误,这显然是有道理的.如果 NPM 认为它可以在 'C:UsersLarsHoldgaardpackage.json'
处找到我的项目文件,那就不走运了.正确的路径是 C:UsersLarsHoldgaardDocumentsGithubLikvido.CreditRiskLikvido.CreditRiskLikvido.CreditRisk
.
npm : npm WARN saveError ENOENT: 没有这样的文件或目录,打开 'C:UsersLarsHoldgaardpackage.json'在行:1 字符:1+ npm install --save react-bootstrap-typeahead+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : NotSpecified: (npm WARN saveEr...dpackage.json':String) [], RemoteException+ FullyQualifiedErrorId : NativeCommandErrornpm警告恩恩ENOENT:没有这样的文件或目录,打开 'C:UsersLarsHoldgaardpackage.json'npm警告grunt-sass@2.0.0 需要 grunt@>=0.4.0 的对等点,但没有安装.您必须自己安装对等依赖项.npm警告react-rating@1.0.6 需要一个 react@>=0.13.0 的对等体,但没有安装.您必须自己安装对等依赖项.npm警告react-bootstrap-typeahead@2.5.1 需要一个 react@^0.14.0 ||^15.2.0 ||^16.0.0 但没有安装.您必须自己安装对等依赖项.npm警告react-bootstrap-typeahead@2.5.1 需要一个 react-dom@^0.14.0 ||^15.2.0 ||^16.0.0 但没有安装.您必须自己安装对等依赖项.npm警告prop-types-extra@1.0.1 需要 react@>=0.14.0 的对等体,但没有安装.您必须自己安装对等依赖项.npm警告react-overlays@0.8.3 需要一个 react@^0.14.9 ||>=15.3.0 但没有安装.您必须自己安装对等依赖项.npm警告react-overlays@0.8.3 需要一个 react-dom@^0.14.9 ||>=15.3.0 但没有安装.您必须自己安装对等依赖项.npm警告react-onclickoutside@6.7.1 需要一个 react@^15.5.x ||^16.x 但没有安装.您必须自己安装对等依赖项.npm警告react-onclickoutside@6.7.1 需要 react-dom@^15.5.x 的 peer ||^16.x 但没有安装.您必须自己安装对等依赖项.npm警告react-transition-group@2.2.1 需要一个 react@>=15.0.0 的对等体,但没有安装.您必须自己安装对等依赖项.npm警告react-transition-group@2.2.1 需要一个 react-dom@>=15.0.0 的对等体,但没有安装.您必须自己安装对等依赖项.npm警告LarsHoldgaard 没有描述npm警告LarsHoldgaard 没有存储库字段.npm警告LarsHoldgaard 没有 README 数据npm警告LarsHoldgaard 没有许可证字段.
我的想法:
作为一个控制台菜鸟,我想我只需要更改我当前的文件夹.但是如果我运行 dir
,我在正确的文件夹中,我可以看到我的 package.json
以及其他文件.
安装组件的正确方法是什么?
为避免手动导航到正确的目录,请使用 Mads Kristensen 的打开命令行"扩展.它在市场上免费提供.您可以在
提示:使用键盘快捷键 ALT+Space 而不是上下文菜单来打开命令提示符.
然后你可以运行你的 npm 命令:
npm install react-bootstrap-typeahead
附带说明:从 npm 5.0.0 开始,已安装的模块默认添加为依赖项,因此不再需要 --save 选项.
2019 年更新:
开发人员命令提示符和开发人员 Power Shell 现在已集成到 Visual Studio 2019(16.2 预览版 2)中 - 不再需要扩展.
您可以在 Tools/Command Line
默认情况下没有分配快捷方式 - 所以你必须自己做.
I have a simple Visual Studio solution, running ASP.NET Core v2 and building a React app.
Now, I want to install a simple component using the 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:UsersLarsHoldgaardpackage.json'
, it's out of luck. The correct path would be C:UsersLarsHoldgaardDocumentsGithubLikvido.CreditRiskLikvido.CreditRiskLikvido.CreditRisk
.
npm : npm WARN saveError ENOENT: no such file or directory, open 'C:UsersLarsHoldgaardpackage.json'
At line:1 char:1
+ npm install --save react-bootstrap-typeahead
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (npm WARN saveEr...dpackage.json':String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
npm
WARN
enoent
ENOENT: no such file or directory, open 'C:UsersLarsHoldgaardpackage.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.
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. It is available for free in the Marketplace. You find it here.
Once installed you can open a command prompt conviently directly from within Visual Studio.
Tipp: Use the Keyboard Shortcut ALT+Space instead of the context menu to open the command prompt.
You can then run your npm command:
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.
Update 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.
You find them under Tools/Command Line
By default no shortcut is assigned - so you have to do this yourself.
这篇关于如何在 Visual Studio 2017 中使用 NPM 并安装包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!