Skip to content

How can I require a file that is built using browserify? #1773

@saeedseyfi

Description

@saeedseyfi

Hello browserify lords :)

I built a js library using browserify. Working well in the browser and everything looks great if I put it as a separate file in my html:

<!-- my library -->
<script src="./library/index.js"></script>

<!-- my app that uses the library -->
<script src="./app/build.js"></script>

But when I want to require the library file in my app, browserify complains.
Because the requires are still in the file and if I re-bundle it, browserify wants to re-include the files that are already included.

My files:

- library
  - src
    - core.js
    - foo.js
  - build.js
- app
  - src
    - core.js
  - build.js

library/src/foo.js:

module.exports = 'foo';

library/src/core.js:

console.log(require('./foo'));

app/src/core.js:

require('../../library/build');

library/build.js is created using browserify:

$ browserify library/src/core.js > library/build.js

Now I want to build app/src/core.js the same way ($ browserify app/src/core.js > app/build.js) but it complains: Error: Cannot find module './foo' from '/library'

I created a test repo to demo the issue: https://github.com/saeedseyfi/browserify-issue

Please advise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions