Parris
Parris

Reputation: 18456

Requiring files from dependencies via RequireJS

I'm using requirejs and have a file I'd like to import from one of my dependencies.

Here is my folder structure

  • src
    • my_app
    • require_config
  • node_modules
    • other_repo
      • apps
      • components
        • camera_component
        • camera_view
      • require_config

Let's say in my_app I load Camera Component.

node_modules/other_repo/components/camera_component.js:

define(function(require) {

    var CameraView = require('components/camera_view');

    ....

    return MyClass;

});

The problem is that components/camera_view doesn't exist in my path. It does however exist in that project. If this were one or 2 dependencies I needed to shim or tweak I would just modify paths within require config; however, there are a lot of dependencies. There is an entire require config for that project which I should probably respect.

Is there a solution for this sort of problem within requirejs? Should I fork that repo or only use some package thats been prepared for external use.

Upvotes: 0

Views: 44

Answers (1)

Louis
Louis

Reputation: 151511

Require it using a relative path:

var CameraView = require('./camera_view');

Since the two files are in the same directory, the require call will resolve the path as the camera_view module which is in the same path as camera_component.

This will work for all cases where your modules exist in specific locations relative to one another.

Upvotes: 2

Related Questions