浏览自己浏览的库:相对路径错误

Sam*_*Fen 11 browserify

我想使用一个使用browserify构建的库.该库正确构建,并且在单独使用时工作正常.

现在构建的库在我的vendor /目录中,我尝试在我的新应用程序中要求它:

var myLib = require('./vendors/myLib');
Run Code Online (Sandbox Code Playgroud)

当我尝试浏览我的应用程序时,它抱怨它找不到require该库中的一些内部语句:

Error: Cannot find module '../utils/logger' from '/myApp/vendor'
Run Code Online (Sandbox Code Playgroud)

Browserify似乎试图从错误的目录重新构建lib.我怎样才能解决这个问题?


更多细节:

lib看起来像这样:

myLib
 ?  app.js
 ?
 ???models
 ?    model.js
 ?
 ???utils
      logger.js
Run Code Online (Sandbox Code Playgroud)

应用程序requires模型和模型requires记录器使用require('../utils/logger').

然后将其构建到myLib.js(browserify app.js --standalone myLib > myLib.js)中.

到目前为止,非常好,myLib工作正常.

In my new application, I put myLib.js in the /vendor directory, require it as listed at top, and get the error that Browserify can't find '../utils/logger'.

In this situation I do control myLib, so could change it if absolutely necessary, but it's another project in the company and I'd prefer not to if necessary. However, I see at least one other question on SO where someone is clearly having the same problem with a bower-installed third-party library.

JMM*_*JMM 8

这看起来很糟糕.

以下是一些选项:

  • 运行derequiremyLib消费之前.

  • 尝试浏览您的应用,如下所示:

    browserify({
      entries: ['./entry'],
      noParse: ['/abs/path/to/vendors/myLib.js'],
    })
    
    Run Code Online (Sandbox Code Playgroud)

    如果它不起作用,请在没有noParse值的扩展名的情况下尝试它.

  • myLib在消费之前缩小.