Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

meteor

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

npm install twit

I deployed the node module locally from .meteor/local/build/server/

So, it is visible at .meteor/local/build/server/node_modules/twit

I tried installing it at project root but the project did not find the module. Which led me to the above solution which works.

My application now runs fine locally. I am able to run and do everything and can interact with Twitter from my Meteor server side or client side depending on what I want to do. No crashes.

When I deploy to meteor.com through the command

meteor deploy [appname] --password

The application deploys successfully.

When I attempt to access the (app at anonistream.meteor.com)[anonistream.meteor.com] from a browser it fails and the logs contain this error.

[Mon May 07 2012 01:59:53 GMT+0000 (UTC)] WARNING
node.js:201
   throw e; // process.nextTick error, or 'error' event on first tick
         ^
Error: Cannot find module 'twit'
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at app/server/server.js:2:12
at /meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/server.js:111:21
at Array.forEach (native)
at Function.<anonymous>
 (/meteor/containers/84162a7c-24e8-bf26-6fd8-     e4ec13b2a935/bundle/server/underscore.js:76:11)
at /meteor/containers/84162a7c-24e8-bf26-6fd8-e4ec13b2a935/bundle/server/server.js:97:7
[Mon May 07 2012 01:59:53 GMT+0000 (UTC)] INFO STATUS running -> waiting
[Mon May 07 2012 01:59:53 GMT+0000 (UTC)] ERROR Application crashed with code: 1
[Mon May 07 2012 02:29:55 GMT+0000 (UTC)] INFO HIT / 24.94.158.145
[Mon May 07 2012 02:29:59 GMT+0000 (UTC)] INFO HIT /favicon.ico 24.94.158.145
[Mon May 07 2012 02:30:46 GMT+0000 (UTC)] INFO HIT / 24.94.158.145
[Mon May 07 2012 02:30:50 GMT+0000 (UTC)] INFO HIT /favicon.ico 24.94.158.145

Does anyone have any suggestions on how this might be accomplished?

like image 430
Steeve Cannon Avatar asked May 07 '12 02:05

Steeve Cannon


People also ask

Does meteor install node?

Meteor works with Node. js version >= 10 and <= 14, for Windows you need to have Node. js installed for running the npm installer (tip: you can use nvm for managing node versions). Meteor supports Windows 7/Windows Server 2008 R2 and up.

How use NPM package meteor?

To use an npm package from a file in your application you import the name of the package: import moment from 'moment'; // this is equivalent to the standard node require: const moment = require('moment'); This imports the default export from the package into the symbol moment .

How do I run a node module?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.

Does meteor client have auto build?

After executing the meteor command to start the build tool you should leave it running while further developing your app. The build tool automatically detects any relevant file changes using a file watching system and recompiles the necessary changes, restarting your client or server environment as needed.


2 Answers

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
like image 107
shao Avatar answered Oct 24 '22 21:10

shao


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');
});
like image 23
spectrum Avatar answered Oct 24 '22 22:10

spectrum