How can I deploy node modules in a Meteor app on meteor.com?

后端 未结 7 1479
轻奢々
轻奢々 2020-11-29 02:16

I have an application that uses the node twit module that is available via

npm install twit

I deployed the node module locally from .m

相关标签:
7条回答
  • 2020-11-29 02:27

    This code worked for me with meteor 0.8.x and node_modules being installed in ./public of my app:

    var path = Npm.require('path')
    var fs = Npm.require('fs')
    var base = path.resolve('.')
    var isBundle = fs.existsSync(base + '/bundle')
    var modulePath = base + (isBundle ? '/bundle/static' : '/../client/app') + '/node_modules/'
    
    var twit  = Npm.require(modulePath+'rssparser')
    

    It may also be a good idea to create packages.json file within ./public for easier updates/installs through npm.

    Long live Meteor!

    0 讨论(0)
  • 2020-11-29 02:28

    You

    base = base + "/bundle"
    

    to get this to work.

    0 讨论(0)
  • 2020-11-29 02:34

    finally, I wrote like this. it works both in local and meteor sever. thx Ian :D

    install npm module inside "app/public":

        app/public# npm install MODULE_NAME
    

    inside app/server/server.js:

    Meteor.startup(function () {
        var require = __meteor_bootstrap__.require;
        var path = require('path');
        var base = path.resolve('.');
        var isBundle = path.existsSync(base + '/bundle');
        var modulePath = base + (isBundle ? '/bundle/static' : '/public') + '/node_modules';
    
        var MODULE_NAME = require(modulePath + '/MODULE_NAME');
    });
    
    0 讨论(0)
  • 2020-11-29 02:34

    Just spent a half hour figuring out the "install npm module inside app/public step and thought I'd save the next person some time. From your app's home directory:

    cd public
    mkdir node_modules
    npm install foo
    

    By default, npm install foo installs "locally," but if there's no node_modules folder in your current directory then it moves up the directory tree looking for one. I was ending up with the package installing in $HOME/node_modules/foo instead of the local project. Fine for localhost, but not so much for deployment.

    (Thanks to npm install locally for solving my root problem.)

    0 讨论(0)
  • 2020-11-29 02:35

    Changed:

    var modulePath = base + (isBundle ? '/bundle/static' : '/../client/app') + '/node_modules/'
    

    to:

    var modulePath = base + (isBundle ? '/bundle/static' : '/../web.browser/app') + '/node_modules/'
    
    0 讨论(0)
  • 2020-11-29 02:37

    As of Meteor 6.0, now we need to use Npm.require() instead. Additionally, we need to declare the module as global variables, since Meteor now has file-level scope.

      var path = Npm.require('path');
      var fs = Npm.require('fs');
      var base = path.resolve('.');
      var isBundle = fs.existsSync(base + '/bundle');
      var modulePath = base + (isBundle ? '/bundle/static' : '/public') + '/node_modules';
      MODULE_NAME = Npm.require(modulePath + '/MODULE_NAME'); // NOTE, this is going to be a global variable
    
    0 讨论(0)
提交回复
热议问题