简体   繁体   English

在VS2015中使用gulp在ASP.NET 4.5.2 MVC项目中编译LESS

[英]Using gulp in VS2015 to compile LESS in ASP.NET 4.5.2 MVC project

I tried the new ASP.NET vNext and I really liked the way gulp worked. 我尝试了新的ASP.NET vNext,我真的很喜欢gulp的工作方式。 I'm trying to compile LESS using gulp in an ASP.NET 4.5.2 MVC project now. 我正在尝试使用gulp在ASP.NET 4.5.2 MVC项目中编译LESS。

I followed this for help. 我跟着这个寻求帮助。
Here's what I've done so far: 这是我到目前为止所做的:

Added a package.json file with the following code. 添加了包含以下代码的package.json文件。

{
  "name": "package",
  "version": "1.0.0",
  "private": true,
  "devDependencies": {
    "gulp": "3.9.0",
    "gulp-bower": "0.0.10",
    "gulp-config": "0.3.0",
    "gulp-less": "3.0.3",
    "gulp-plumber": "1.0.1"
  }
}

Created a Gulp Task in gulpfile.js 在gulpfile.js中创建了一个Gulp任务

var gulp = require('gulp');
var less = require('gulp-less');
var path = require('path');
var plumber = require('gulp-plumber');
gulp.task('less', function () {
    return gulp.src('./Content/**/*.less')
    .pipe(plumber())
      .pipe(less({
          paths: [path.join(__dirname, 'less', 'includes')]
      }))
      .pipe(gulp.dest('./content/'));
});

But then I get the following error (in the output window): 但后来我得到以下错误(在输出窗口中):

Failed to run "C:\\Users\\me\\documents\\visual studio 2015\\Projects\\projectname\\Gulpfile.js"... cmd.exe /c gulp --tasks-simple 'gulp' is not recognized as an internal or external command, operable program or batch file. 无法运行“C:\\ Users \\ me \\ documents \\ visual studio 2015 \\ Projects \\ projectname \\ Gulpfile.js”... cmd.exe / c gulp --tasks-simple'gulp'未被识别为内部或外部命令,可操作程序或批处理文件。

I'm guessing npm din't download gulp. 我猜npm不要下载gulp。 Any idea how to get this working? 知道如何让这个工作吗?

You shouldn't need to install Gulp globally to work with it in Visual Studio 2015. When Gulp is installed into the project by npm, it adds gulp.cmd to a folder in your project (./node_modules/.bin by default). 您不需要全局安装Gulp以在Visual Studio 2015中使用它。当Gulp通过npm安装到项目中时,它会将gulp.cmd添加到项目中的文件夹(默认情况下为./node_modules/.bin)。 Visual Studio includes that path by default in the paths where it looks for external tools like Grunt, Gulp, Bower, and npm. 默认情况下,Visual Studio在其查找外部工具(如Grunt,Gulp,Bower和npm)的路径中包含该路径。 This is configurable in Tools->Options->Projects And Solutions->External Web Tools . 这可以在Tools->Options->Projects And Solutions->External Web Tools

My guess is that npm didn't successfully install the packages when you first created the package.json file. 我的猜测是,当你第一次创建package.json文件时,npm没有成功安装软件包。 Whenever you save your package.json file, Visual Studio will run npm install for your project, so you don't even need to open a console window. 每当您保存package.json文件时,Visual Studio将为您的项目运行npm install ,因此您甚至不需要打开控制台窗口。 But if you added an existing package.json file to the project, it doesn't install for you automatically. 但是,如果您将现有的package.json文件添加到项目中,则不会自动为您安装。 In that case you need to run npm install yourself, or open and save the file to get VS to do it for you. 在这种情况下,您需要自己运行npm install ,或者打开并保存文件以让VS为您执行此操作。

Typing npm install in the Package Manager Console will add a node_modules folder to the project. 在包管理器控制台中键入npm install将向项目添加node_modules文件夹。 All of the dependencies specified in the package.json file will be added to that folder. package.json文件中指定的所有依赖项都将添加到该文件夹​​中。

In this case, now that gulp is installed, the error should be gone. 在这种情况下,现在已经安装了gulp,错误应该消失了。

The short answer is, to get rid of this error, install gulp globally with the following command: npm install gulp -g 简而言之,要解决此错误,请使用以下命令全局安装gulp:npm install gulp -g

In VS2015 RC gulp had to be installed globally. 在VS2015中,必须在全球范围内安装RC gulp。 Otherwise VS wouldn't recognize it. 否则VS不会认出来。 You can find a detailed explanation here: http://www.dotnetcurry.com/visualstudio/1096/using-grunt-gulp-bower-visual-studio-2013-2015 您可以在此处找到详细说明: http//www.dotnetcurry.com/visualstudio/1096/using-grunt-gulp-bower-visual-studio-2013-2015

Is supposed to be fixed in VS2015 RTM, I haven't checked that yet though. 应该在VS2015 RTM中修复,但我还没有检查过。

Running npm install for the separate packages should not be necessary. 不需要为单独的包运行npm install。 If you enable hidden folders, you should see that VS downloads all nodejs modules by default after you saved your package.json file. 如果启用隐藏文件夹,则应该看到VS在保存package.json文件后默认下载所有nodejs模块。 The download log can be found in the output window "Bower/NPM" in VS2015 RTM. 可以在VS2015 RTM的输出窗口“Bower / NPM”中找到下载日志。 If you enable hidden folders or take a look in your web project directory, you should see a node_modules folder with all the packages specified in your package.json file. 如果启用隐藏文件夹或查看Web项目目录,则应该看到node_modules文件夹,其中包含package.json文件中指定的所有包。

In my case I needed to move 在我的情况下,我需要移动

$(PATH) 

before 之前

.\node_modules\bin

I have found if you create the visual studio solution folder on a UNC path this error will occur. 我发现如果您在UNC路径上创建visual studio解决方案文件夹,则会发生此错误。

Often UNC paths map your Documents folder when using a windows on virtual machine (eg Parallels for Mac). 在虚拟机上使用窗口时,UNC路径通常会映射您的Documents文件夹(例如Parallels for Mac)。

To fix the issue I moved the visual studio solution folder to the virtual machine disk drive, then reopened the project. 为了解决这个问题,我将visual studio解决方案文件夹移动到了虚拟机磁盘驱动器,然后重新打开了项目。

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

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