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

前端 未结 6 533
攒了一身酷
攒了一身酷 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"
    

提交回复
热议问题