javascript 配置从 node_modules 读取的 requirejs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10500073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
configuring requirejs reading from node_modules
提问by GTDev
I'm trying to setup a nodejs project to use requirejs. I call my program with node r.js ./config/main.js
and my main.js looks like the following:
我正在尝试设置一个 nodejs 项目以使用 requirejs。我调用我的程序,node r.js ./config/main.js
我的 main.js 如下所示:
var cs = require("coffee-script");
var requirejs = require("requirejs");
requirejs.config({
nodeRequire: require,
baseUrl: ".",
paths: {
cs: "cs",
CoffeeScript: "CoffeeScript",
csBuild: "csBuild",
express: "express",
nohm: "nohm",
redback: "redback",
_: "underscore",
"connect-redis": "connect-redis",
freebase: "freebase"
}
});
console.log("hetet");
requirejs(["cs!./config/app"], function(app){
console.log("closing")
});
and inside app.coffee:
在 app.coffee 里面:
define((require) ->
express = require("express")
RedisStore = require("connect-redis")(express)
app = express.createServer()
config = require('cs!./config')
require('cs!./setup')(app, express, RedisStore)
require('cs!./routes')(app)
require('cs!../src/server')
app.listen(config.server.port)
)
I seem to fail in main.js with the error:
我似乎在 main.js 中失败并出现错误:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Calling node's require("config") failed with error: Error: Calling node's require("config") failed with error: Error: Cannot find module 'config'
and What I have noticed is when I comment out the line var requirejs = require("requirejs");
(in main.js), I get further and fail at the line RedisStore = require("connect-redis")(express)
(in app.coffee) with the error:
我注意到的是,当我注释掉该行var requirejs = require("requirejs");
(在 main.js 中)时,我走得更远,并在该行RedisStore = require("connect-redis")(express)
(在 app.coffee 中)失败并出现错误:
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: undefined is not a function
at ./config/app.coffee:10:41
I have been having a lot of trouble configuring requirejs in node any help would be appreciated.
我在 node 中配置 requirejs 时遇到了很多麻烦,任何帮助将不胜感激。
thanks
谢谢
采纳答案by jrburke
It is best to not configure requirejs to look in node_modules, since modules in that area are modules formatted for node. There is a little bit more info in the requirejs node page.
最好不要将 requirejs 配置为在 node_modules 中查找,因为该区域中的模块是为 node 格式化的模块。requirejs 节点页面中有更多信息。