Meteor Npm.require() cannot find a file in parent

2019-08-04 00:50发布

问题:

I'm trying to get an abandoned nodejs library to work under Meteor.

Why does Npm.require('./crypto-js/crypto') work fine, but Npm.require('../convert') is throwing the error Error: Cannot find module '../convert'?

/packages/myPackage/package.js

Package.on_use(function (api) {

  var path = Npm.require('path');  
  api.add_files(path.join('convert.js'), 'server');
  api.add_files(path.join('crypto-js', 'crypto.js'), 'server');
  api.add_files(path.join('util.js'), 'server');


});

/packages/myPackage/convert.js

myFunc = function() {
    return true
}

/packages/myPackage/util.js

Crypto = Npm.require('./crypto-js/crypto');

/packages/myPackage/crypto-js/crypto.js

var conv = Npm.require('../convert')

回答1:

Already answered to this on IRC but posting here for the reference.

The author wanted to use a fork of npm module hosted on GitHub. To import the npm module from certain repo and certain commit, we can use GitHub's tarball url.

  • Create a smart package in /packages/package-name
  • In /packages/package-name/package.js describe the package, add files, export variables
  • add Npm.depends to package.js file looking like this:

    Npm.depends({'NPM-MODULE-NAME': "https://github.com/REPOAUTHOR/REPONAME/tarball/COMMIT-SHA1"});

  • in one of the smart package's files do ExportSymbol = Npm.require('NPM-MODULE-NAME')

  • export the export symbol

example: https://github.com/Slava/meteor-npm-fork-example