简体   繁体   English

Heroku - 具有Gulp和Browserify的节点:错误无法找到模块

[英]Heroku - Node with Gulp & Browserify: Error Cannot find module from

I'm using Gulp for building all my assets. 我正在使用Gulp来构建我的所有资产。 For Javascript, I have a task which uses Browserify to solve all my code dependencies. 对于Javascript,我有一个使用Browserify解决所有代码依赖关系的任务。

When I'm running my project locally everything works perfectly. 当我在本地运行我的项目时,一切都很完美。 But, when deployed in heroku, Gulp fails with the following error: 但是,当在heroku中部署时,Gulp失败并出现以下错误:

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)

This is the Gulp task 这是Gulp的任务

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
}

This is my current project organization (for the client module) 这是我目前的项目组织(针对客户端模块)

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

This is the main module from client/web/public/dev/js/main.js that requires the feed module and fails: 这是来自client / web / public / dev / js / main.js的主要模块,它需要feed模块并失败:

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

This is a snippet for the feed module: 这是Feed模块的代码段:

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

class Feed {
    // Rest of class
}

module.exports = Feed

In short, your devDependencies in package.json don't get loaded when your NODE_ENV is set to production . 总之,你devDependenciespackage.json没有得到,当你加载NODE_ENV设置为production By default, Heroku sets that to production. 默认情况下,Heroku将其设置为生产。 So you have three options, in my opinion, in order from worst to best. 所以在我看来,你有三个选择,从最差到最好。

  1. Set your NODE_ENV env var in Heroku to something else. 将Heroku中的NODE_ENV env var设置为其他内容。 ( staging or anything other than production ) Then your devDependencies will load, grunt will run, but you'll have all those libraries in your Heroku build. stagingproduction以外的任何东西)然后你的devDependencies将加载,grunt将运行,但你将拥有Heroku构建中的所有这些库。 And you shouldn't run a production app in this mode. 并且您不应该在此模式下运行生产应用程序。

  2. Move the necessary devDependencies to the dependencies section of your package.json . 将必要的devDependencies移动到package.jsondependencies部分。 Same as before, those libraries will still be in your production app, but at least your NODE_ENV will be accurate. 与以前一样,这些库仍将在您的生产应用程序中,但至少您的NODE_ENV将是准确的。

  3. Use a postinstall script to load devDependencies, run your build, and remove the devDependencies before launch. 使用postinstall脚本加载devDependencies,运行构建,并在启动之前删除devDependencies。 See my comment here: https://stackoverflow.com/a/42237745/673882 请在此处查看我的评论: https//stackoverflow.com/a/42237745/673882

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

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