Npm.require inside build plugins

If I Npm.require some file by absolute path (not an npm package) from a file running on the server via api.addFiles([file.js], 'server'), then require calls inside that file work.

But then if I Npm.require that file from a compiler (set up with Plugin.registerCompiler), it complains that require is undefined.

Why is this? Why does Meteor block access to require in the first place?

EDIT I was having issues because I failed to include npmDependencies for the build plugin. BUT:

Npm.require is appending the npm folder path to my absolute path. I have seen some projects that don’t suffer from this, but I don’t understand why. I am able to require my file by adding a bunch of ../../../../ etc. to the path I pass in…there’s no sense trying to stop devs from using npm require for anything and everything, we’ll find a way. So it might as well be made convenient to require files by relative/absolute path.

EDIT 2 I was able to work around this issue by creating another package that provides this silly wrapper:

NpmWorkaround = {
  require: Npm.require,
};

Why this works for requiring local files in compile plugins and straight Npm.require doesn’t is pretty strange to me.

1 Like