Where do we put node modules we install by npm in a Meteor project?

前端 未结 6 508
攒了一身酷
攒了一身酷 2020-12-08 03:33

I followed the github meteorirc project\'s lead and put them in /public/

I installed my node modules via npm from inside /public/ and therefore I have a /public/node

相关标签:
6条回答
  • 2020-12-08 04:02

    This helped me a lot including a syntax highlighting package! Thanks!

    I use a little helper though, as I think this will not be the last npm package I'll use ;)

    meteorNpm = do() ->
      require = __meteor_bootstrap__.require
    
      path    = require 'path'
      fs      = require 'fs'
    
      base = path.resolve '.'
      if base is '/'
        base = path.dirname global.require.main.filename
    
      meteorNpm =
        # requires npm modules placed in `public/node_modules`
        require: (moduleName) ->
          modulePath = 'node_modules/' + moduleName
    
          publicPath = path.resolve(base + '/public/' + modulePath)
          staticPath = path.resolve(base + '/bundle/static/' + modulePath)
    
          if path.existsSync(publicPath)
            module = require publicPath
          else if path.existsSync(staticPath)
            module = require staticPath
          else
            module = null
    
          return module
    

    Use it like this:

    highlight = meteorNpm.require "highlight.js"
    
    0 讨论(0)
  • 2020-12-08 04:04

    Meteor takes lib/node_modules from the development bundle and makes a symbolic link or copies it to server/node_modules, which is in the hidden .meteor sub folder under your project.

    So, if you cd into the lib directory of the development bundle or into server directory of the .meteor folder (I believe it is in build); you will be able to use the node modules. If you have trouble loading them, you might want to check out this question.

    0 讨论(0)
  • 2020-12-08 04:09

    To use Npm modules in Meteor its adding the npm module in.

    First you need to add a npm package adapter such as meteorhacks:npm

    meteor add meteorhacks:npm
    

    Then start your meteor app by running meteor, you will notice a new packages.json file in your project

    Add in modules like this (you need to explicitly define a version)

    {
        "request" : "2.53.0"
    }
    

    Then you can use the npm modules in your meteor app, use Meteor.npmRequire instead of require

    var request = Meteor.npmRequire("request")
    
    0 讨论(0)
  • 2020-12-08 04:11
    cd /usr/local/meteor/lib/ && npm install <module>
    
    0 讨论(0)
  • 2020-12-08 04:11

    I am using such script which nicely install all node.js dependencies. It behaves similar to official support in Meteor engine branch (it installs dependencies at runtime) but it supports also installing from git repositories and similar goodies.

    0 讨论(0)
  • 2020-12-08 04:14

    You have to add bundle folder to the path:

    var staticTwitPath = path.resolve(base+'/bundle/static/'+twitPath);
    

    Here is my working sample in coffeescript, node_modules are in public folder:

    # loading node_modules from public folder
    require = __meteor_bootstrap__.require
    path    = require("path")
    fs      = require('fs')
    
    cheerioPath = 'node_modules/cheerio'
    
    base = path.resolve('.')
    if base == '/'
      base = path.dirname(global.require.main.filename)
    
    publicPath = path.resolve(base+'/public/'+cheerioPath)
    staticPath = path.resolve(base+'/bundle/static/'+cheerioPath)
    
    if path.existsSync(publicPath)
      cheerio = require(publicPath)
    else if path.existsSync(staticPath)
      cheerio = require(staticPath)
    else
      console.log('node_modules not found')
    

    Good luck!

    0 讨论(0)
提交回复
热议问题