admin管理员组

文章数量:1287878

I don't much like the standard way to require modules, which goes something like this:

connect = require 'connect'
express = require 'express'
redis = require 'redis'
sys = require 'sys'
coffee = require 'coffee-script'
fs = require 'fs'

It's not exactly DRY. In a modest CoffeeScript server, the require dance takes up a fair chunk of the entire script! I've been toying with the following alternative:

"connect,express,redis,sys,coffee-script,fs"
  .split(',').forEach (lib) -> global[lib] = require lib

Since I haven't seen people try to refactor the standard approach, I thought I'd ask if it seems reasonable to do so, and if so, are there any better ways to do it?

I don't much like the standard way to require modules, which goes something like this:

connect = require 'connect'
express = require 'express'
redis = require 'redis'
sys = require 'sys'
coffee = require 'coffee-script'
fs = require 'fs'

It's not exactly DRY. In a modest CoffeeScript server, the require dance takes up a fair chunk of the entire script! I've been toying with the following alternative:

"connect,express,redis,sys,coffee-script,fs"
  .split(',').forEach (lib) -> global[lib] = require lib

Since I haven't seen people try to refactor the standard approach, I thought I'd ask if it seems reasonable to do so, and if so, are there any better ways to do it?

Share Improve this question edited Aug 31, 2011 at 20:33 mahemoff asked Aug 31, 2011 at 20:16 mahemoffmahemoff 46.5k39 gold badges169 silver badges233 bronze badges 3
  • 3 Intelligent question. Favoriting in the hope of learning something cool :D – Kheldar Commented Aug 31, 2011 at 20:21
  • I'm not convinced about the global scope argument. After all, NPM implies a global namespace anyway. It's certainly a trade-off to consider as there are clear downsides to the in-your-face interruption of business/application logic to perform an infrastructure-level require statement. – mahemoff Commented Aug 31, 2011 at 21:23
  • @mahemoff you should read up on global scope. It's clearly evil for the normal reasons. – Raynos Commented Aug 31, 2011 at 21:50
Add a ment  | 

3 Answers 3

Reset to default 5

Note that coffee-script isn't a valid identifier, so your code isn't really importing it properly. You can use CoffeeScript's flexible object literals to handle this pretty nicely. I'd also use ?= to avoid unnecessarily re-importing modules. Building off of user211399's answer:

global[id] ?= require name for id, name of {
    "connect", "express", "redis", "sys", coffee: "coffee-script", "fs" }

                                                                    [Compile to JS]

Since I'm allowing you to import with different identifiers in different modules, using the global namespace feels particularly unsafe. I'd import them locally instead, as shown below. Be aware that because this uses eval it might not fail gracefully if you specify an illegal identifier.

eval "#{id} = require(#{JSON.stringify name})" name for id, name of {
    "connect", "express", "redis", "sys", coffee: "coffee-script", "fs" }

                                                                    [Compile to JS]

I toyed with the idea some time ago, and ended up with this:

global[mod.replace /\W/g, ''] = require mod for mod in [
    "connect"
    "express"
    "redis"
    "sys"
    "coffee-script"
]

Ended up scratching it and just doing it the usual way - it ends up being a bigger hassle. There are plenty of times where you need to grab a module's property or use a different naming scheme. Also, assigning to global scope is not the same as a "normal" require. Aligning assignments makes it easier to read:

connect  = require 'connect'
express  = require 'express'
mongoose = require 'mongoose'
coffee   = require 'coffee-script'
fs       = require 'fs'
{ exec } = require 'child_process'

You're only doing this once, abstracting it is just unnecessary plexity.

global[lib] = require lib for lib in "connect,express,redis,sys,coffee-script,fs".split ','

本文标签: javascriptBest way to require several modules in NodeJSStack Overflow