我正在使用npm来管理我的ASP.NET核心应用程序所需的jQuery,Bootstrap,Font Awesome和类似的客户端库.
对我有用的方法首先将package.json文件添加到项目中,如下所示:
{
"version": "1.0.0",
"name": "myapp",
"private": true,
"devDependencies": {
},
"dependencies": {
"bootstrap": "^3.3.6",
"font-awesome": "^4.6.1",
"jquery": "^2.2.3"
}
}
Run Code Online (Sandbox Code Playgroud)
npm将这些包恢复到node_modules文件夹中,该文件夹与项目目录中的wwwroot处于同一级别:
由于ASP.NET Core提供了来自wwwroot文件夹的静态文件,并且node_modules不在那里,我不得不进行一些更改才能完成这项工作,第一个:在app.UseStaticFiles之前添加app.UseFileServer. cs文件:
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"node_modules")),
RequestPath = new PathString("/node_modules"),
EnableDirectoryBrowsing = true
});
app.UseStaticFiles();
Run Code Online (Sandbox Code Playgroud)
第二个,包括project.json文件中的publishOptions中的node_modules:
"publishOptions": {
"include": [
"web.config",
"wwwroot",
"Views",
"node_modules"
]
},
Run Code Online (Sandbox Code Playgroud)
这适用于我的开发环境,当我将它部署到我的Azure App Service实例时,它也可以工作,jquery,bootstrap和font-awesome静态文件得到很好的服务,但我不确定这个实现.
这样做的正确方法是什么?
这个解决方案是在从多个来源收集大量信息并尝试一些不起作用之后得出的,并且从wwwroot外部提供这些文件似乎有点奇怪.
任何建议将不胜感激.
我有一个简单的Visual Studio解决方案,运行ASP.NET Core v2并构建一个React应用程序.
现在,我想使用NPM安装一个简单的组件.在这个特定的例子中,它可能是:
npm install --save react-bootstrap-typeahead
Run Code Online (Sandbox Code Playgroud)
我希望这个软件包只在我的解决方案中工作,而不是其他任何地方.
我的结果:
当我运行它时,我得到以下很好的错误,这显然是有道理的.如果NPM认为它可以找到我的项目文件'C:\Users\LarsHoldgaard\package.json',那就不走运了.正确的道路是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 …Run Code Online (Sandbox Code Playgroud)