2017-04-21 22 views
1
找到模塊

我正在使用Gulp來構建我的所有資產。對於JavaScript,我有一個使用Browserify解決我所有代碼依賴項的任務。Heroku - 使用Gulp&Browserify節點:錯誤無法從

當我在本地運行我的項目時,一切正常。但是,在Heroku的部署時,咕嘟咕嘟失敗,出現以下錯誤:

2017-04-21T20:35:28.370935+00:00 app[web.1]: Error: Cannot find module './components/feed' from '/app/client/web/public/dev/js' 
2017-04-21T20:35:28.370935+00:00 app[web.1]:  at /app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:55:21 
2017-04-21T20:35:28.370936+00:00 app[web.1]:  at load (/app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:69:43) 
2017-04-21T20:35:28.370937+00:00 app[web.1]:  at onex (/app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:92:31) 
2017-04-21T20:35:28.370937+00:00 app[web.1]:  at /app/node_modules/browser-resolve/node_modules/resolve/lib/async.js:22:47 
2017-04-21T20:35:28.370938+00:00 app[web.1]:  at FSReqWrap.oncomplete (fs.js:123:15) 

這是咕嘟咕嘟任務

gulp.task('bundle',() => { 
    const javascriptFiles = [ 
    { 
     src: './client/web/public/dev/js/main.js', 
     outDir: './client/web/public/production/js', 
     outFile: 'main.bundle.js' 
    } 
    ] 
    javascriptFiles.forEach((file) => { 
    const bundler = browserify({ 
     entries: [ file.src ], 
     extensions: ['.js'], 
     paths: ['./node_modules','./client/web/public/dev/js'] 
    }) 
    .transform(coffeeify) 
    .transform('babelify', { presets: ['es2015'] }) 

    createBundle(bundler, file) 
    }) 
}) 

function createBundle (bundler, file) { 
    const src = path.join(__dirname, file.src) 
    const outFile = path.join(__dirname, file.outFile) 
    const outDir = path.join(__dirname, file.outDir) 
    const sourceMapDir = 'maps' 

    bundler.bundle() 
    .pipe(source(src)) 
    .pipe(buffer()) // Convert to gulp pipeline 
    .pipe(rename(outFile)) 
    // Sourc Map 
    .pipe(sourceMaps.init({ loadMaps : true })) 
    .pipe(sourceMaps.write(sourceMapDir)) // save 
    // Write result to output directory 
    .pipe(gulp.dest(outDir)) 
    .pipe(livereload()) // Reload browser if relevant 
} 

這是我目前的項目組織(客戶端模塊)

. 
├── app.js 
├── gulpfile.js 
└── client 
    └── web 
     ├── public 
     │   ├── dev 
     │   │   ├── js 
     │   │   │   ├── main.js 
     │   │   │   │   ├── utils 
     │   │   │   │   │  ├── random.js 
     │   │   │   │   ├── components 
     │   │   │   │   │  ├── feed 
     │   │   │   │   │  │  ├── index.js 

這是來自client/web/public/dev/js/main.js的主要模塊,它需要饋送模塊並且失敗:

const Feed = require('./components/feed') 
Feed.doWhatever(...) 

這是進紙模塊的代碼段:

const Random = require('../../utils/random) 

class Feed { 
    // Rest of class 
} 

module.exports = Feed 

回答

0

總之,你在package.jsondevDependencies當你NODE_ENV設置爲production沒有得到加載。默認情況下,Heroku將其設置爲生產。所以在我看來,你有三種選擇,從最壞到最好。

  1. 將你的NODE_ENV env var設置爲Heroku。 (stagingproduction以外的任何東西)然後你的devDependencies會加載,grunt會運行,但是你的Heroku版本中會包含所有這些庫。而且您不應該在此模式下運行生產應用程序。

  2. 將必要的devDependencies移動到您的package.jsondependencies部分。和以前一樣,這些庫仍然會在您的製作應用程序中,但至少您的NODE_ENV將是準確的。

  3. 使用postinstall腳本在啓動前加載devDependencies,運行構建並刪除devDependencies。在此處看到我的評論:https://stackoverflow.com/a/42237745/673882