我对CommonJS,AMD和RequireJS仍感到非常困惑.即使阅读了很多.
我知道CommonJS(以前称为ServerJS)是一个用于在浏览器外部使用语言时定义一些JavaScript规范(即模块)的组.CommonJS模块规范有一些像Node.js或RingoJS的实现,对吧?
CommonJS,异步模块定义(AMD)和RequireJS之间有什么关系?RequireJS是CommonJS模块定义的实现吗?如果是的话,那么什么是AMD呢?
我在Node.js模块中找到了以下合同:
module.exports = exports = nano = function database_module(cfg) {...}
Run Code Online (Sandbox Code Playgroud)
我不知道什么之间的不同module.exports和exports为什么都被用在这里.
在此页面(http://docs.nodejitsu.com/articles/getting-started/what-is-require)上,它声明"如果要将导出对象设置为函数或新对象,则必须使用module.exports对象."
我的问题是为什么.
// right
module.exports = function () {
console.log("hello world")
}
// wrong
exports = function () {
console.log("hello world")
}
Run Code Online (Sandbox Code Playgroud)
我console.log结果(result=require(example.js)),第一个是[Function]第二个{}.
你能解释一下背后的原因吗?我在这里阅读帖子:在Node.js中的module.exports vs exports.它很有帮助,但没有解释它以这种方式设计的原因.如果直接退回出口参考会有问题吗?
之前,巴贝尔会添加这条线module.exports = exports["default"].它不再这样做了.在我能做之前,这意味着什么:
var foo = require('./foo');
// use foo
Run Code Online (Sandbox Code Playgroud)
现在我必须这样做:
var foo = require('./foo').default;
// use foo
Run Code Online (Sandbox Code Playgroud)
这不是什么大不了的事(我猜它应该一直都是这样).问题是我有很多代码依赖于以前的工作方式(我可以将大部分代码转换为ES6导入,但不是全部转换).任何人都可以给我提示如何使旧的方式工作,而不必通过我的项目并解决这个问题(甚至一些关于如何编写codemod来做这个的说明将是非常光滑的).
谢谢!
例:
输入:
const foo = {}
export default foo
Run Code Online (Sandbox Code Playgroud)
使用Babel输出5
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = {};
exports["default"] = foo;
module.exports = exports["default"];
Run Code Online (Sandbox Code Playgroud)
使用Babel 6(和es2015插件)输出:
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var foo = {};
exports["default"] = foo;
Run Code Online (Sandbox Code Playgroud)
请注意,输出的唯一区别是module.exports = exports["default"].
您可能对我在解决我的具体问题后写的博客文章感兴趣:误解ES6模块,升级Babel,眼泪和解决方案
我有一个我需要从node.js脚本的脚本,我想保持javascript引擎独立.
所以,例如,我想做:
__PRE__
只有它在node.js下运行 我该怎么办这个测试?
编辑:发布此问题时,我不知道node.js模块功能是基于commonjs.
对于具体的例子,我提出了一个更准确的问题:
脚本如何判断它是否需要作为commonjs模块?
我已经开始使用webpack2(准确地说v2.3.2),在重新创建配置后,我一直遇到一个我似乎无法解决的问题.我得到了(提前抱歉丑陋的转储):
ERROR in ./src/main.js
Module not found: Error: Can't resolve 'components/DoISuportIt' in '[absolute path to my repo]/src'
resolve 'components/DoISuportIt' in '[absolute path to my repo]/src'
Parsed request is a module
using description file: [absolute path to my repo]/package.json (relative path: ./src)
Field 'browser' doesn't contain a valid alias configuration
aliased with mapping 'components': '[absolute path to my repo]/src/components' to '[absolute path to my repo]/src/components/DoISuportIt'
using description file: [absolute path to my repo]/package.json (relative path: ./src)
Field 'browser' doesn't …Run Code Online (Sandbox Code Playgroud) 根据Node.js手册:
如果希望模块导出的根是函数(例如构造函数),或者如果要在一个赋值中导出完整对象而不是一次构建一个属性,请将其分配给module.exports而不是export .
给出的例子是:
// file: square.js
module.exports = function(width) {
return {
area: function() {
return width * width;
}
};
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用:
var square = require('./square.js');
var mySquare = square(2);
console.log('The area of my square is ' + mySquare.area());
Run Code Online (Sandbox Code Playgroud)
我的问题:为什么示例不使用square作为对象?以下是否有效,是否使示例更"面向对象"?
var Square = require('./square.js');
var mySquare = new Square(2);
console.log('The area of my square is ' + mySquare.area());
Run Code Online (Sandbox Code Playgroud) 有一些第三方Javascript库具有我想在Node.js服务器中使用的一些功能.(具体来说,我想使用我发现的QuadTree javascript库.)但这些库只是简单的.js文件而不是"Node.js库".
因此,这些库不遵循exports.var_nameNode.js对其模块所期望的语法.据我了解,这意味着当你这样做module = require('module_name');或者module = require('./path/to/file.js');你最终会得到一个没有公共可访问功能的模块等.
我的问题是"我如何将任意javascript文件加载到Node.js中,以便我可以利用它的功能,而不必重写它,以便它可以做到exports?"
我对Node.js很新,所以如果我对它的工作原理有一些明显的漏洞,请告诉我.
编辑:研究更多东西,我现在看到Node.js使用的模块加载模式实际上是最近开发的加载Javascript库的标准的一部分,称为CommonJS.它在Node.js的模块文档页面上说明了这一点,但直到现在我才错过了.
可能最终我的问题的答案是"等到你的图书馆的作者开始写一个CommonJS界面或者做你自己该死的自己."
在我的系统中,我在浏览器中加载了许多"类",每个文件在开发过程中都是单独的文件,并连接在一起进行生产.在加载它们时,它们会在全局对象上初始化一个属性G,如下例所示:
var G = {};
G.Employee = function(name) {
this.name = name;
this.company = new G.Company(name + "'s own company");
};
G.Company = function(name) {
this.name = name;
this.employees = [];
};
G.Company.prototype.addEmployee = function(name) {
var employee = new G.Employee(name);
this.employees.push(employee);
employee.company = this;
};
var john = new G.Employee("John");
var bigCorp = new G.Company("Big Corp");
bigCorp.addEmployee("Mary");
Run Code Online (Sandbox Code Playgroud)
我没有使用自己的全局对象,而是根据James Burke的建议,考虑让每个类都有自己的AMD模块:
define("Employee", ["Company"], function(Company) {
return function (name) {
this.name = name;
this.company = new …Run Code Online (Sandbox Code Playgroud) 有人知道怎么做module.exports?
我尝试了一些不同的方式
export class Greeter {}
Run Code Online (Sandbox Code Playgroud)
将编译为
exports.Greeter = Greeter;
Run Code Online (Sandbox Code Playgroud)
但我真正想要的是:
exports = Greeter;
Run Code Online (Sandbox Code Playgroud)
所以我可以像这样使用它:
import { Greeter } from "greeter";
const greeter = new Greeter();
Run Code Online (Sandbox Code Playgroud)
并不是
import { Greeter } from "greeter";
const greeter = new Greeter.Greeter();
Run Code Online (Sandbox Code Playgroud)
这可能与Typescript有关吗?
commonjs ×10
javascript ×7
node.js ×5
requirejs ×2
amd ×1
babeljs ×1
ecmascript-6 ×1
import ×1
module ×1
npm ×1
package.json ×1
typescript ×1
webpack ×1
webpack-2 ×1