3

Meteor application directory layout:

imports/
  api/
    collections/
      MyCollectionFile.js

packages/
  mypackage/
      mypackageMain.js

I can export anything from the package file and use it inside the application, that's ok. But how can I use "import" in the package, like the other way around?

// mypackageMain.js
if (Meteor.isServer) {
    require ('/imports/api/collections/MyCollectionFile.js');
};

OR

import '/imports/api/collections/MyCollectionFile.js';

I tried using the path '../../imports/api/collections/MyCollectionFile.js' but it simply does not work. I can not access this file from a package.

I get the following Error for both the import and the require:

W20160618-23:25:59.486(3)? (STDERR) Error: Cannot find module '../../imports/api/collections/MyCollectionFile.js'
W20160618-23:25:59.487(3)? (STDERR)     at require (packages/modules-runtime/.npm/package/node_modules/install/install.js:85:1)
cenk
  • 1,389
  • 14
  • 27
  • 1
    This kind of goes against the principle of packages being self-contained. You can export from your package a function that accepts some symbol from you application instead of trying to import it from the package. It makes it more portable. – MasterAM Jun 19 '16 at 05:00
  • @MasterAM you are right. It's a safety measure. Thanks. – cenk Jun 19 '16 at 08:58

2 Answers2

2

Figured out that this was not possible.

However, moving the collections to a package and exporting them would make the collections available to other packages and the application.

cenk
  • 1,389
  • 14
  • 27
1

I've also been running in to this issue and have found two solutions, both are sub-par though.

  1. Install the thing you want to import as a npm package. $npm install --save ./imports/<the thing>.
  2. Use npm link to create a link to the thing you want to import.

Both solutions require that you have a package.json in the directory you want to import and both won't be transpile the code and just run it against the provided version of node.

A possible solution for the transpile issue would be using a loader plugin, or somehow provide a additional configuration to System.js to tell it to transpile the code on import.

Andre Steenveld
  • 148
  • 1
  • 11