2016-10-22 29 views
0

我正在嘗試在瀏覽器上使用ES2015,然後使用gulp將其轉換爲瀏覽器可以理解的內容。但是,我在運行gulp js任務時遇到了path must be a string當使用gulp,gulp-babel和browserify時,如何修復'path必須是一個字符串'?

gulpfile.babel.js

import Browserify from 'browserify'; 
import Gulp from 'gulp'; 
import Babel from 'gulp-babel'; 
import Buffer from 'vinyl-buffer'; 
import Source from 'vinyl-source-stream'; 
import Uglify from 'gulp-uglify'; 

Gulp.task('js',() => { 
    Browserify({ 
     entries: 'public/scripts/Main.js', 
     debug: true 
    }).transform(Babel({ 
      presets: ['es2015-node6'] 
     })) 
     .bundle() 
     .on('error', error => console.error(error)) 
     .pipe(Source('main.min.js')) 
     .pipe(Buffer()) 
     .pipe(Uglify()) 
     .pipe(Gulp.dest('dist/scripts/')); 
}); 

完整的錯誤消息

{ Error: path must be a string 
    at /Users/user/Documents/test/node_modules/resolve/lib/async.js:16:16 
    at _combinedTickCallback (internal/process/next_tick.js:67:7) 
    at process._tickCallback (internal/process/next_tick.js:98:9) 
    ... 
} 

我一直在使用的babelify代替gulp-babel試過,但我不能使用gulp-uglify,因爲我還得到一個錯誤

message: 'SyntaxError: Unexpected token: punc()) 

如果它很重要,這是什麼,是在我的main.js

import * as Render from './render'; 

(() => { 
    function init() { 
     console.log('here'); 
    } 

    window.addEventListener('load',() => { 
     if (document.readyState === 'complete') { 
      init(); 
     } 
    }); 
})(); 

,並在我的`render.js

export function Render() { 
    console.log('Render'); 
}; 

export function RenderPosts() { 
    console.log('RenderPosts'); 
} 

而且,這不是一些項目作出反應。我只是想在前端使用ES2015。

+0

我不知道吞嚥。但是消息說該參數必須是一個字符串。不是int,double或數組。顯示發生錯誤的行號。 – user3502626

+0

在節點6中添加了一個限制,所以如果你有一些更老的依賴關係,你最好的辦法就是更新你的'package.json',以確保你所有的代碼都是最新的版本。 – loganfsmyth

回答

0

您的「path must be a string」錯誤是因爲將函數傳遞給.transform

import Browserify from 'browserify'; 
import Gulp from 'gulp'; 
import Babel from 'gulp-babel'; 
import Buffer from 'vinyl-buffer'; 
import Source from 'vinyl-source-stream'; 
import Uglify from 'gulp-uglify'; 

Gulp.task('js',() => { 
    Browserify({ 
     entries: 'public/scripts/Main.js', 
     debug: true 
    }).transform('babelify', { 
      presets: ['es2015-node6'] 
     }) 
     .bundle() 
     .on('error', error => console.error(error)) 
     .pipe(Source('main.min.js')) 
     .pipe(Buffer()) 
     .pipe(Uglify()) 
     .pipe(Gulp.dest('dist/scripts/')); 
}); 

以防萬一你不知道,babel-preset-es2015-node6如果你的瀏覽器支持ES2015本身才有效。它也適用於Gulp。

相關問題