【问题标题】:Gulp-autoprefixer throwing ReferenceError: Promise is not definedGulp-autoprefixer 抛出 ReferenceError: Promise is not defined
【发布时间】:2015-12-06 01:24:41
【问题描述】:

我尝试使用 gulp 编译我的 sass,然后使用 gulp-autoprefixer 自动添加前缀,但出现错误。

var gulp = require('gulp'),
    sass = require('gulp-sass'),
    autoprefixer = require('gulp-autoprefixer');

gulp.task('test', function(){
    gulp.src('_sass/main.sass')
        .pipe(sass())
        .pipe(autoprefixer()) 
        .pipe(gulp.dest('./assets/css')); 
});

我正在尝试运行此 Gulpfile.js 并且我正在使用:

"gulp": "~3.9.0",
"gulp-sass": "~2.0.4",
"gulp-autoprefixer": "~3.0.1",

和 NPM 版本1.3.10

当我运行gulp test 时,我得到了这个:

/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:152
        this.processing = new Promise(function (resolve, reject) {
                              ^
ReferenceError: Promise is not defined
    at LazyResult.async (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:152:31)
    at LazyResult.then (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:75:21)
    at DestroyableTransform._transform (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/index.js:28:13)
    at DestroyableTransform.Transform._read (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:172:10)
    at DestroyableTransform.Transform._write (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:160:12)
    at doWrite (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:326:12)
    at writeOrBuffer (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:312:5)
    at DestroyableTransform.Writable.write (/home/matei/Tests/test-4/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:239:11)
    at write (/home/matei/Tests/test-4/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/home/matei/Tests/test-4/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)

我真的不知道我做错了什么。当我使用 sass 或纯 css 时不起作用。我认为我的文件有问题。

【问题讨论】:

  • 当 Autoprefixer 抛出错误时,不确定这与 Sass(或 CSS)有什么关系。
  • 不确定这是否是解决方案....但是您的 NPM 版本已过时。尝试更新 npm 和你所有的包,我之前在运行旧版本时遇到过这样的奇怪怪癖。

标签: node.js npm gulp autoprefixer


【解决方案1】:

这并不能直接回答问题,但它可能对尝试运行 ionic 2 教程时遇到此错误的人有用。

正如其他答案所指出的,问题是缺少es6-promise

我在尝试启动 ionic 2 教程时遇到了同样的错误 (https://github.com/driftyco/ionic2-starter-tutorial): (我的 ionic 2 版本是 2.0.0-beta.25,最新的教程提交是 ed9ef2fcce887e4d1c08c375c849b06b8394bad7)

这是我尝试使用ionic serve 运行应用程序时得到的堆栈跟踪:

Running 'serve:before' gulp task before serve
[18:37:00] Starting 'clean'...
[18:37:01] Finished 'clean' after 1.02 s
[18:37:01] Starting 'watch'...
[18:37:01] Starting 'sass'...
[18:37:01] Starting 'html'...
[18:37:01] Starting 'fonts'...
[18:37:01] Starting 'scripts'...
[18:37:01] Finished 'scripts' after 62 ms
[18:37:01] Finished 'html' after 72 ms
[18:37:01] Finished 'fonts' after 77 ms
Caught exception:
 ReferenceError: Promise is not defined
    at LazyResult.async (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:157:31)
    at LazyResult.then (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/postcss/lib/lazy-result.js:79:21)
    at DestroyableTransform._transform (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/index.js:24:6)
    at DestroyableTransform.Transform._read (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:159:10)
    at DestroyableTransform.Transform._write (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_transform.js:147:83)
    at doWrite (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:313:64)
    at writeOrBuffer (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:302:5)
    at DestroyableTransform.Writable.write (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-autoprefixer/node_modules/through2/node_modules/readable-stream/lib/_stream_writable.js:241:11)
    at DestroyableTransform.ondata (/home/stitakis/dev/playground/ionic2/myTutorial/node_modules/ionic-gulp-sass-build/node_modules/gulp-sass/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:531:20)
    at DestroyableTransform.EventEmitter.emit (events.js:95:17) 

同样,正如其他答案所指出的,这是解决此问题的方法:

  1. 编辑 gulpfile.js 并在第 6 行添加:require('es6-promise').polyfill();

  2. 安装缺少的依赖项:npm install es6-promise --save

更改后,问题得到解决,我可以启动本地服务器了。

【讨论】:

  • 这解决了我的问题,我需要做的就是将 npm install es6-promise 与 --save 一起使用,并将此 require 与 .pollyfill 一起使用,使用 .Promise() 作为上面的 awnser 对我不起作用。
【解决方案2】:

在 package.json 所在的项目位置安装 es6-promise

npm install es6-promise

然后让 gulpfile.js 的第一行代码如下:

var Promise = require('es6-promise').Promise;

【讨论】:

    【解决方案3】:

    有同样的问题。对我来说,更新节点不起作用,但在我的 gulpfile 开头添加它:

    require('es6-promise').polyfill();
    

    【讨论】:

    • 这是一个很好的“修复”,更好的解决方案是更新node.js(见@Matei的回答)
    • @MonkeyKing 就像我在回答中所说,更新节点并没有解决问题
    • 什么包?我得到cannot find module es6-promise
    • @Victor 尝试在您的终端中运行npm install es6-promise
    【解决方案4】:

    我使用以下方法将 node.js 更新到了最新版本:

    # Using Ubuntu
    curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
    sudo apt-get install -y nodejs
    

    对于我的Ubuntu机器,如图here

    之后我使用以下方法更新了 NPM:

    sudo npm cache clean -f
    sudo npm install -g n
    sudo n stable
    

    如图here

    现在gulp-autoprefixer 开始工作,但我收到来自gulp-sass 的错误。我使用这条指令更新了它:

    1. 删除您的 node_modules 文件夹
    2. 从你的 package.json 文件中删除 gulp-sass
    3. 从你的 package.json 文件中移除 node-sass(如果你有的话)
    4. 运行 npm install gulp-sass --save-dev
    5. 根据需要更新您的 Gulp 任务

    找到here。现在我有了"gulp-sass": "^2.0.4",这解决了我所有的问题。

    感谢您的建议和帮助。

    【讨论】:

    • 升级节点后可以让node-sassnpm rebuild node-sass一起工作。
    • 效果很好。谢谢。当您在运行任务时遇到错误时,控制台消息也会要求运行此命令
    • 这确实是NodeJS版本问题,见github.com/sindresorhus/gulp-autoprefixer/issues/45
    • 很好的说明。可以在nodesource.com/services/upgrade-self-service 找到与操作系统无关的说明
    • 我不需要这些步骤:sudo npm cache clean -f sudo npm install -g n sudo n stable 并使用npm rebuild node-sass 来修复 node-sass 问题。 :)
    猜你喜欢
    • 1970-01-01
    • 2019-06-22
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多