Best practice advice request: in-package absolute vs relative import paths

Hello. I am writing a Meteor Package named myPackage with relatively deep folder structures:

/client
    /charts
        /standard
            /myChart.js
/lib
    /utilities
        /myUtility.js

Now, I am in /client/charts/standard/myChart.js, and I want to import a symbol from /lib/utilities/myUtility.js. I have two options here:

  • import myUtility from '../../../lib/utilities/myUtility.js'; - relative
  • import myUtility from 'meteor/myPackage/lib/utilities/myUtility.js'; - “absolute”, since a leading / does not work in a package context.

The second option reads much better for me. However, i am not sure of any non-obvious implications of this. Maybe it’s not safe (could be deprecated?) to import a package file from the meteor/* namespace from the same package itself?

I’d appreciate some opinions. @sashko @benjamn maybe you can give some advice based on your technical knowledge?

Thanks for your time!

2 Likes

I’ve you’re importing something from a package, definitely use the import ... from "meteor/package-name/..." style. The absolute form of such module identifiers is /node_modules/meteor/package-name/..., but there’s no reason to include the /node_modules/ prefix, and a relative import is just confusing.

I am talking here specifically about importing files from within the same package, not cross-package and not package-to-app importing. I feel like i did not explain myself right.