浏览器本身已被浏览器化的库:相对路径错误 [英] Browserifying libraries that were themselves browserified: relative paths error

查看:88
本文介绍了浏览器本身已被浏览器化的库:相对路径错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用使用browserify构建的库。该库可以正确构建并且可以单独使用。

I want to use a library that was build using browserify. The library built correctly and works fine when it is used by itself.

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

Now that built library is in my vendors/ directory, and I try to require it in my new application:

var myLib = require('./vendors/myLib');

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

When I try to browserify my application, it complains that it can't find some of the internal require statements inside that library:

Error: Cannot find module '../utils/logger' from '/myApp/vendor'

Browserify似乎正在尝试重建错误目录中的lib。我该如何解决?

Browserify seems to be trying to re-build the lib from the wrong directory. How can I fix this?

更多细节:

lib看起来像这样:

The lib looks like this:

myLib
 │  app.js
 │
 ├──models
 │    model.js
 │
 ├──utils
      logger.js

app 需要模型,而模型需要记录器使用 require('../ utils / logger')

app requires model, and model requires logger using require('../utils/logger').

然后将其内置到myLib.js( browserify app.js --standalone myLib> myLib.js )。

This is then build into myLib.js (browserify app.js --standalone myLib > myLib.js).

到目前为止,很好,myLib可以正常工作。

So far, so good, myLib works fine.

在我的新应用程序中,将myLib.js放在/ vendor目录中, require 如顶部所示,并获得错误,导致Browserify找不到'../ utils / logger'。

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'.

在这种情况下,我执行控制myLib,因此可以更改它如果绝对必要,但这是公司的另一个项目,如果有必要,我宁愿不要。但是,我看到关于SO的至少另一个问题,其中有人显然有相同的问题凉亭安装的第三方库的问题。

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.

推荐答案

这似乎是很无聊

以下是一些选择:

尝试像这样浏览应用程序:

Try browserifying your app like so:

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

如果不起作用,请尝试在 noParse 值中不带扩展名的情况下进行尝试。

If it doesn't work, try it without the extension in the noParse value.

缩小 myLib 消费之前

这篇关于浏览器本身已被浏览器化的库:相对路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆