如何评价 node v0.12.76.0.0

昨天用了下,自己的项目编译各种报错,然后就乖乖滚回了5..
首先出 6.0 我们肯定是支持的。&br&&br&最主要的是 v8 的升级。因为 node.js 在一个大版本里是基本不升级 v8 的,所以这次一升级,大家就突然感觉,擦咋 es2015 支持度一下子大跃进了?其实跟 Chrome 的发版节奏比,只是跟上了而已。&br&&br&当然,es2015 支持度的提升是很好的,剩下还未支持的部分,要么是小问题(如 function names 的一些特例),要么是还没什么人用的特性(如 Symbol.species),要么是可能在后续标准里被改掉的(如 tail calls),基本不碍事。&br&&br&有人说还不支持 import/export 啊!所以结果还是要 babel。但实际上如果只需要处理 import/export ,你可以用 rollup 等轻量级方案。&br&&br&不过有个坑爹的事情是,es2015特性虽然支持度非常高,但是性能优化还没有跟上,比如略复杂一点的 let 就会让函数无法被 optimize,而函数无法优化对性能影响巨大,简单循环可以差10倍效率。这一点上,V8 做得比 Chakra(MS Edge 的 JS 引擎)差多了。Chakra 现在连 try-catch 也不会阻止函数优化了。&br&&br&所以绕了一圈回来,如果你对性能要求很高,还是要用 babel 做一些编译的。&br&&br&&br&再有,每次升级大版本,总是会有一堆 breaking changes。大多数情况下,你不太会遇到问题。但是一旦遇到就比较麻烦了。&br&&br&这次甚至还有比较严重的问题,比如 &a href=&///?target=https%3A///nodejs/node/issues/3402& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Don't resolve symlinks when requiring · Issue #3402 · nodejs/node · GitHub&i class=&icon-external&&&/i&&/a& (从我的第一个 comment 开始看即可)。我已经提请 revert 掉相关的 PR 了。(update:终于决定revert了:&a href=&///?target=https%3A///nodejs/node/pull/6536& class=& wrap external& target=&_blank& rel=&nofollow noreferrer&&Revert &module: preserve symlinks when requiring& by saper · Pull Request #6536 · nodejs/node · GitHub&i class=&icon-external&&&/i&&/a&)&br&&br&&br&所以么,第一个吃螃蟹的总是需要勇气的。如果是关键服务,建议还是等几个月进入 LTS 为好。(即使如此,跟其他平台动不动等3、5年才升级已经是胆大包天了。)
首先出 6.0 我们肯定是支持的。最主要的是 v8 的升级。因为 node.js 在一个大版本里是基本不升级 v8 的,所以这次一升级,大家就突然感觉,擦咋 es2015 支持度一下子大跃进了?其实跟 Chrome 的发版节奏比,只是跟上了而已。当然,es2015 支持度的提升是很…
今天在微博看到好多人在转发node6.0,本来觉得没什么,不就是版本更新嘛。出于好奇我运行了一下 node -v,发现我用的版本是0.12,当时我就震惊了。
今天在微博看到好多人在转发node6.0,本来觉得没什么,不就是版本更新嘛。出于好奇我运行了一下 node -v,发现我用的版本是0.12,当时我就震惊了。
已有帐号?
无法登录?
社交帐号登录
随便看看,点赞狂魔<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&Node.js has a simple module loading system.
In Node.js, files and modules are
in one-to-one correspondence.
As an example, foo.js loads the module
circle.js in the same directory.
The contents of foo.js:
const circle = require(&#39;./circle.js&#39;);
console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);
The contents of circle.js:
const PI = Math.PI;
exports.area = (r) =& PI * r *
exports.circumference = (r) =& 2 * PI *
The module circle.js has exported the functions area() and
circumference().
To add functions and objects to the root of your module,
you can add them to the special exports object.
Variables local to the module will be private, as though the module was wrapped
in a function. In this example the variable PI is private to circle.js.
If you want the root of your module&#39;s export to be a function (such as a
constructor) or if you want to export a complete object in one assignment
instead of building it one property at a time, assign it to module.exports
instead of exports.
Below, bar.js makes use of the square module, which exports a constructor:
const square = require(&#39;./square.js&#39;);
var mySquare = square(2);
console.log(`The area of my square is ${mySquare.area()}`);
The square module is defined in square.js:
// assigning to exports will not modify module, must use module.exports
module.exports = (width) =& {
area: () =& width * width
The module system is implemented in the require(&module&) module.
Accessing the main module
When a file is run directly from Node.js, require.main is set to its
module. That means that you can determine whether a file has been run
directly by testing
require.main === module
For a file foo.js, this will be true if run via node foo.js, but
false if run by require(&#39;./foo&#39;).
Because module provides a filename property (normally equivalent to
__filename), the entry point of the current application can be obtained
by checking require.main.filename.
Addenda: Package Manager Tips
The semantics of Node.js&#39;s require() function were designed to be general
enough to support a number of reasonable directory structures. Package manager
programs such as dpkg, rpm, and npm will hopefully find it possible to
build native packages from Node.js modules without modification.
Below we give a suggested directory structure that could work:
Let&#39;s say that we wanted to have the folder at
/usr/lib/node/&some-package&/&some-version& hold the contents of a
specific version of a package.
Packages can depend on one another. In order to install package foo, you
may have to install a specific version of package bar.
The bar package
may itself have dependencies, and in some cases, these dependencies may even
collide or form cycles.
Since Node.js looks up the realpath of any modules it loads (that is,
resolves symlinks), and then looks for their dependencies in the node_modules
folders as described , this
situation is very simple to resolve with the following architecture:
/usr/lib/node/foo/1.2.3/ - Contents of the foo package, version 1.2.3.
/usr/lib/node/bar/4.3.2/ - Contents of the bar package that foo
depends on.
/usr/lib/node/foo/1.2.3/node_modules/bar - Symbolic link to
/usr/lib/node/bar/4.3.2/.
/usr/lib/node/bar/4.3.2/node_modules/* - Symbolic links to the packages
that bar depends on.
Thus, even if a cycle is encountered, or if there are dependency
conflicts, every module will be able to get a version of its dependency
that it can use.
When the code in the foo package does require(&#39;bar&#39;), it will get the
version that is symlinked into /usr/lib/node/foo/1.2.3/node_modules/bar.
Then, when the code in the bar package calls require(&#39;quux&#39;), it&#39;ll get
the version that is symlinked into
/usr/lib/node/bar/4.3.2/node_modules/quux.
Furthermore, to make the module lookup process even more optimal, rather
than putting packages directly in /usr/lib/node, we could put them in
/usr/lib/node_modules/&name&/&version&.
Then Node.js will not bother
looking for missing dependencies in /usr/node_modules or /node_modules.
In order to make modules available to the Node.js REPL, it might be useful to
also add the /usr/lib/node_modules folder to the $NODE_PATH environment
Since the module lookups using node_modules folders are all
relative, and based on the real path of the files making the calls to
require(), the packages themselves can be anywhere.
All Together...
To get the exact filename that will be loaded when require() is called, use
the require.resolve() function.
Putting together all of the above, here is the high-level algorithm
in pseudocode of what require.resolve does:
require(X) from module at path Y
1. If X is a core module,
a. return the core module
2. If X begins with &#39;./&#39; or &#39;/&#39; or &#39;../&#39;
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW &not found&
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text.
2. If X.js is a file, load X.js as JavaScript text.
3. If X.json is a file, parse X.json to a JavaScript Object.
4. If X.node is a file, load X.node as binary addon.
LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
a. Parse X/package.json, and look for &main& field.
b. let M = X + (json main field)
c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text.
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon.
LOAD_NODE_MODULES(X, START)
1. let DIRS=NODE_MODULES_PATHS(START)
2. for each DIR in DIRS:
a. LOAD_AS_FILE(DIR/X)
b. LOAD_AS_DIRECTORY(DIR/X)
NODE_MODULES_PATHS(START)
1. let PARTS = path split(START)
2. let I = count of PARTS - 1
3. let DIRS = []
4. while I &= 0,
a. if PARTS[I] = &node_modules& CONTINUE
c. DIR = path join(PARTS[0 .. I] + &node_modules&)
b. DIRS = DIRS + DIR
c. let I = I - 1
5. return DIRS
Modules are cached after the first time they are loaded.
This means
(among other things) that every call to require(&#39;foo&#39;) will get
exactly the same object returned, if it would resolve to the same file.
Multiple calls to require(&#39;foo&#39;) may not cause the module code to be
executed multiple times.
This is an important feature.
&partially done& objects can be returned, thus allowing transitive
dependencies to be loaded even when they would cause cycles.
If you want to have a module execute code multiple times, then export a
function, and call that function.
Module Caching Caveats
Modules are cached based on their resolved filename.
Since modules may
resolve to a different filename based on the location of the calling
module (loading from node_modules folders), it is not a guarantee
that require(&#39;foo&#39;) will always return the exact same object, if it
would resolve to different files.
Additionally, on case-insensitive file systems or operating systems, different
resolved filenames can point to the same file, but the cache will still treat
them as different modules and will reload the file multiple times. For example,
require(&#39;./foo&#39;) and require(&#39;./FOO&#39;) return two different objects,
irrespective of whether or not ./foo and ./FOO are the same file.
Core Modules
Node.js has several modules compiled into the binary.
These modules are
described in greater detail elsewhere in this documentation.
The core modules are defined within Node.js&#39;s source and are located in the
lib/ folder.
Core modules are always preferentially loaded if their identifier is
passed to require().
For instance, require(&#39;http&#39;) will always
return the built in HTTP module, even if there is a file by that name.
When there are circular require() calls, a module might not have finished
executing when it is returned.
Consider this situation:
console.log(&#39;a starting&#39;);
exports.done =
const b = require(&#39;./b.js&#39;);
console.log(&#39;in a, b.done = %j&#39;, b.done);
exports.done =
console.log(&#39;a done&#39;);
console.log(&#39;b starting&#39;);
exports.done =
const a = require(&#39;./a.js&#39;);
console.log(&#39;in b, a.done = %j&#39;, a.done);
exports.done =
console.log(&#39;b done&#39;);
console.log(&#39;main starting&#39;);
const a = require(&#39;./a.js&#39;);
const b = require(&#39;./b.js&#39;);
console.log(&#39;in main, a.done=%j, b.done=%j&#39;, a.done, b.done);
When main.js loads a.js, then a.js in turn loads b.js.
point, b.js tries to load a.js.
In order to prevent an infinite
loop, an unfinished copy of the a.js exports object is returned to the
b.js module.
b.js then finishes loading, and its exports object is
provided to the a.js module.
By the time main.js has loaded both modules, they&#39;re both finished.
The output of this program would thus be:
$ node main.js
main starting
a starting
b starting
in b, a.done = false
in a, b.done = true
in main, a.done=true, b.done=true
If you have cyclic module dependencies in your program, make sure to
plan accordingly.
File Modules
If the exact filename is not found, then Node.js will attempt to load the
required filename with the added extensions: .js, .json, and finally
.js files are interpreted as JavaScript text files, and .json files are
parsed as JSON text files. .node files are interpreted as compiled addon
modules loaded with dlopen.
A required module prefixed with &#39;/&#39; is an absolute path to the file.
example, require(&#39;/home/marco/foo.js&#39;) will load the file at
/home/marco/foo.js.
A required module prefixed with &#39;./&#39; is relative to the file calling
require(). That is, circle.js must be in the same directory as foo.js for
require(&#39;./circle&#39;) to find it.
Without a leading &#39;/&#39;, &#39;./&#39;, or &#39;../&#39; to indicate a file, the module must
either be a core module or is loaded from a node_modules folder.
If the given path does not exist, require() will throw an
code property set to &#39;MODULE_NOT_FOUND&#39;.
Folders as Modules
It is convenient to organize programs and libraries into self-contained
directories, and then provide a single entry point to that library.
There are three ways in which a folder may be passed to require() as
an argument.
The first is to create a package.json file in the root of the folder,
which specifies a main module.
An example package.json file might
look like this:
{ &name& : &some-library&,
&main& : &./lib/some-library.js& }
If this was in a folder at ./some-library, then
require(&#39;./some-library&#39;) would attempt to load
./some-library/lib/some-library.js.
This is the extent of Node.js&#39;s awareness of package.json files.
Note: If the file specified by the &main& entry of package.json is missing
and can not be resolved, Node.js will report the entire module as missing with
the default error:
Error: Cannot find module &#39;some-library&#39;
If there is no package.json file present in the directory, then Node.js
will attempt to load an index.js or index.node file out of that
directory.
For example, if there was no package.json file in the above
example, then require(&#39;./some-library&#39;) would attempt to load:
./some-library/index.js
./some-library/index.node
Loading from node_modules Folders
If the module identifier passed to require() is not a native module,
and does not begin with &#39;/&#39;, &#39;../&#39;, or &#39;./&#39;, then Node.js starts at the
parent directory of the current module, and adds /node_modules, and
attempts to load the module from that location. Node will not append
node_modules to a path already ending in node_modules.
If it is not found there, then it moves to the parent directory, and so
on, until the root of the file system is reached.
For example, if the file at &#39;/home/ry/projects/foo.js&#39; called
require(&#39;bar.js&#39;), then Node.js would look in the following locations, in
this order:
/home/ry/projects/node_modules/bar.js
/home/ry/node_modules/bar.js
/home/node_modules/bar.js
/node_modules/bar.js
This allows programs to localize their dependencies, so that they do not
You can require specific files or sub modules distributed with a module by
including a path suffix after the module name. For instance
require(&#39;example-module/path/to/file&#39;) would resolve path/to/file
relative to where example-module is located. The suffixed path follows the
same module resolution semantics.
Loading from the global folders
If the NODE_PATH environment variable is set to a colon-delimited list
of absolute paths, then Node.js will search those paths for modules if they
are not found elsewhere.
(Note: On Windows, NODE_PATH is delimited by
semicolons instead of colons.)
NODE_PATH was originally created to support loading modules from
varying paths before the current
algorithm was frozen.
NODE_PATH is still supported, but is less necessary now that the Node.js
ecosystem has settled on a convention for locating dependent modules.
Sometimes deployments that rely on NODE_PATH show surprising behavior
when people are unaware that NODE_PATH must be set.
Sometimes a
module&#39;s dependencies change, causing a different version (or even a
different module) to be loaded as the NODE_PATH is searched.
Additionally, Node.js will search in the following locations:
1: $HOME/.node_modules
2: $HOME/.node_libraries
3: $PREFIX/lib/node
Where $HOME is the user&#39;s home directory, and $PREFIX is Node.js&#39;s
configured node_prefix.
These are mostly for historic reasons.
You are highly encouraged
to place your dependencies locally in node_modules folders.
will be loaded faster, and more reliably.
The module Object
In each module, the module free variable is a reference to the object
representing the current module.
For convenience, module.exports is
also accessible via the exports module-global. module isn&#39;t actually
a global but rather local to each module.
module.children
The module objects required by this one.
module.exports
The module.exports object is created by the Module system. Sometimes this is
many want their module to be an instance of some class. To do
this, assign the desired export object to module.exports. Note that assigning
the desired object to exports will simply rebind the local exports variable,
which is probably not what you want to do.
For example suppose we were making a module called a.js
const EventEmitter = require(&#39;events&#39;);
module.exports = new EventEmitter();
// Do some work, and after some time emit
// the &#39;ready&#39; event from the module itself.
setTimeout(() =& {
module.exports.emit(&#39;ready&#39;);
Then in another file we could do
const a = require(&#39;./a&#39;);
a.on(&#39;ready&#39;, () =& {
console.log(&#39;module a is ready&#39;);
Note that assignment to module.exports must be done immediately. It cannot be
done in any callbacks.
This does not work:
setTimeout(() =& {
module.exports = { a: &#39;hello&#39; };
const x = require(&#39;./x&#39;);
console.log(x.a);
exports alias
The exports variable that is available within a module starts as a reference
to module.exports. As with any variable, if you assign a new value to it, it
is no longer bound to the previous value.
To illustrate the behavior, imagine this hypothetical implementation of
require():
function require(...) {
((module, exports) =& {
// Your module code here
exports = some_
// re-assigns exports, exports is no longer
// a shortcut, and nothing is exported.
module.exports = some_ // makes your module export 0
})(module, module.exports);
As a guideline, if the relationship between exports and module.exports
seems like magic to you, ignore exports and only use module.exports.
module.filename
The fully resolved filename to the module.
The identifier for the module.
Typically this is the fully resolved
module.loaded
Whether or not the module is done loading, or is in the process of
module.parent
Module object
The module that first required this one.
module.require(id)
module.exports from the resolved module
The module.require method provides a way to load a module as if
require() was called from the original module.
Note that in order to do this, you must get a reference to the module
Since require() returns the module.exports, and the module is
typically only available within a specific module&#39;s code, it must be
explicitly exported in order to be used.

我要回帖

更多关于 nodejs v4 v0.12 区别 的文章

 

随机推荐