0

When looking to use NPM packages in my Meteor project, some research brings up this package:

https://github.com/arunoda/meteor-npm/

Basically you just npm install packages into the root folder of your meteor project and include then with:

var GithubApi = Meteor.require('github');

This is what I was doing and it was working fine on my localhost. Then I tried to upload to Meteors test server and it kept crashing because it could not load my async module using this method, which was working on my local. The problem was having the node_modules folder in the root directory, kept causing the app to crash on meteors servers. But in order to use Meteor.require to include the packages, that is where they have to be.

So I had to follow the instruction in this thread to fix the issue:

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

This method requires me to npm install the package into the /public folder. Then I have to use the following code to include the package:

  path = Npm.require("path")
  fs = Npm.require("fs")
  base = path.resolve(".")
  isBundle = fs.existsSync(base + "/bundle")
  modulePath = base + ((if isBundle then "/bundle/static" else "/../client/app")) + "/node_modules/"
  async = Npm.require(modulePath + "async")

The thread is from 2012 so I am wondering what is the current best practice for including NPM modules on the server?

I can include npm modules on the client by including a bundle.js browserified file in the client directory, so that problem is solved for me, but what is the best way for npm use server side?

Community
  • 1
  • 1
Nearpoint
  • 7,202
  • 13
  • 46
  • 74

1 Answers1

4

You shouldn't have node_modules folder in your application. In most cases if you do, it won't even work in development mode.

Instead, you should only have the necessary packages listed in packages.json file (as described in npm package documentation) and let npm manage them as its dependencies.

Hubert OG
  • 19,314
  • 7
  • 45
  • 73
  • so you mean the node_modules folder should be in the root directory? K I will list the packages in packages.json, then do I just do npm install in the root directory? Then how do I use the npm module in my app? – Nearpoint Jul 03 '14 at 02:26
  • **DO NOT** run `npm install` manually. `meteor-npm` takes care of that for you. Also, you should **NOT** have `node_modules` folder, nor in the root directory, nor in `/public`. If you've created one already, remove it. – Hubert OG Jul 03 '14 at 02:45
  • ohhh so you just need to define the packages in the packages.json folder. And then how do you require the package in your file so you can use it? – Nearpoint Jul 03 '14 at 04:41
  • oh nv it is just ex. var GithubApi = Meteor.require('github'); – Nearpoint Jul 03 '14 at 04:44
  • ah thanks it is working. So simple just add the package to packages.json! Wow this is great. Thanks! – Nearpoint Jul 03 '14 at 04:46