在Visual Studio 2015任务运行器中运行Gulpfile出错



我正在构建一个gulpfile在我的aps.net winforms项目中使用。到目前为止,我在package.json

中有以下内容
{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
  "browserify": "^13.1.0",
  "del": "2.2.0",
  "gulp": "^3.9.1",
  "gulp-sourcemaps": "^2.2.0",
  "gulp-typescript": "^3.0.2",
  "tsify": "^2.0.2",
  "vinyl-source-stream": "^1.1.0"
 }
}

和gulp文件中有

/// <binding BeforeBuild='default' />
/*
 This file in the main entry point for defining Gulp tasks and using Gulp      plugins.
  Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
*/
var gulp = require('gulp');
var del = require('del');
var ts = require("gulp-typescript");
var tsProject = ts.createProject("tsconfig.json");
var sourcemaps = require('gulp-sourcemaps');
gulp.task('default', function () {
  // place code for your default task here
});

我的节点版本是6.9.1

如果我从命令行('gulp')运行这个,它工作正常。但是在Visual Studio任务运行器中,它无法加载,并且在输出中我看到以下错误。这个错误在我添加var sourcemaps = require('gulp-sourcemaps');

行后开始
 Failed to run "H:devmyprojGulpfile.js"...
cmd.exe /c gulp --tasks-simple
H:devmyprojnode_modulesgulp-sourcemapsnode_modulesstrip-bomindex.js:2
module.exports = x => {
                    ^
SyntaxError: Unexpected token >
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (H:devmyprojnode_modulesgulp-sourcemapssrcinit.js:10:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

还添加var tsify = require("tsify");,我得到另一个错误…

Failed to run "H:devmyprojectGulpfile.js"...
cmd.exe /c gulp --tasks-simple
TypeError: Object #<Object> has no method 'debuglog'
    at Object.<anonymous> (H:devmyprojectnode_modulestsifyindex.js:4:32)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (H:devmyprojectgulpfile.js:17:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

有人知道为什么我得到这个吗?

提前感谢!

我的问题是我没有意识到Visual Studio有它自己的(过时的)版本的节点工具(安装在Visual Studio文件夹的某个地方)。多么凌乱!

为了使它使用"全局"节点工具,我可以转到菜单工具|选项|项目和解决方案|外部Web工具并将$(PATH)移动到$(DevEnvDir)*位置之前。在我这样做之后,我的Gulp文件将被加载。

另一种选择可能是通过工具|扩展和更新更新Visual Studio节点工具,但是(我认为)我更喜欢使用我将在Visual Studio之外使用的相同外部工具。

经过一些调查,我面临同样的问题,这似乎是你的nodejs安装之间的版本兼容性问题,和你的包依赖"strip-bom"。

一个特别的修复方法是:

module.exports = x => {

:

module.exports = function (x) {

你可能想看看这个:Gulp-Sourcemaps, SyntaxError: Unexpected token>

这似乎解决了我的问题,但不是一个很好的方法。


问题是您可能正在运行旧版本的node。尝试使用命令node -vnodejs -v来检查您的版本。

虽然这可能不是你的情况,但对于其他人来说,看到这个很好,Debian/Ubuntu实际上在它的包管理器"apt-get"上有一个过时的node版本。所以你很有可能还在运行旧版本,比如0.10.25。

我推荐的真正的修复方法(我最后也这么做了)是升级节点版本。截至目前,版本6.9.1似乎是最新的稳定版本。

你可以在nodejs官方网站查看安装指南:

https://nodejs.org/en/download/package-manager/debian-and-ubuntu-based-linux-distributions

最新更新