Error: Cannot find module 'async_hooks' in NodeJs
Error: Cannot find module 'async_hooks' in NodeJs
我正在尝试 运行 测试来自官方节点 api 文档的 Async Hooks 代码,但我在控制台中收到错误 Error: Cannot find module 'async_hooks'
。我已经在脚本的顶部包含了 const async_hooks = require('async_hooks');
。这是我的代码:
const async_hooks = require('async_hooks');
// Return the ID of the current execution context.
const eid = async_hooks.executionAsyncId();
// Return the ID of the handle responsible for triggering the callback of the
// current execution scope to call.
const tid = async_hooks.triggerAsyncId();
// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve });
// Allow callbacks of this AsyncHook instance to call. This is not an implicit
// action after running the constructor, and must be explicitly run to begin
// executing callbacks.
asyncHook.enable();
// Disable listening for new asynchronous events.
asyncHook.disable();
//
// The following are the callbacks that can be passed to createHook().
//
// init is called during object construction. The resource may not have
// completed construction when this callback runs, therefore all fields of the
// resource referenced by "asyncId" may not have been populated.
function init(asyncId, type, triggerAsyncId, resource) { }
// before is called just before the resource's callback is called. It can be
// called 0-N times for handles (e.g. TCPWrap), and will be called exactly 1
// time for requests (e.g. FSReqWrap).
function before(asyncId) { }
// after is called just after the resource's callback has finished.
function after(asyncId) { }
// destroy is called when an AsyncWrap instance is destroyed.
function destroy(asyncId) { }
// promiseResolve is called only for promise resources, when the
// `resolve` function passed to the `Promise` constructor is invoked
// (either directly or through other means of resolving a promise).
function promiseResolve(asyncId) { }
我很困惑我需要安装 npm install async_hooks
但我认为它是 nodeJs 内置模块所以我们只需要在顶部要求。
经过大量搜索后,我发现我使用的是旧版本的 nodejs 和 async_hooks 模块首先在 Node 版本 8.x 中引入。使用 nvm(节点版本管理器)升级版本后问题已解决。
您可以通过以下link安装和管理多个nodejs版本:https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps
如果 nvm 已经安装,您可以按如下方式打开版本 8.x:
列出所有已安装的版本:
nvm ls
v4.4.4
-> v4.4.7
v8.8.0
system
default -> 4.4.7 (-> v4.4.7)
node -> stable (-> v8.8.0) (default)
stable -> 8.8 (-> v8.8.0) (default)
要打开特定的一个:
nvm use v8.8.0
Now using node v8.8.0 (npm v5.4.2)
我正在尝试 运行 测试来自官方节点 api 文档的 Async Hooks 代码,但我在控制台中收到错误 Error: Cannot find module 'async_hooks'
。我已经在脚本的顶部包含了 const async_hooks = require('async_hooks');
。这是我的代码:
const async_hooks = require('async_hooks');
// Return the ID of the current execution context.
const eid = async_hooks.executionAsyncId();
// Return the ID of the handle responsible for triggering the callback of the
// current execution scope to call.
const tid = async_hooks.triggerAsyncId();
// Create a new AsyncHook instance. All of these callbacks are optional.
const asyncHook =
async_hooks.createHook({ init, before, after, destroy, promiseResolve });
// Allow callbacks of this AsyncHook instance to call. This is not an implicit
// action after running the constructor, and must be explicitly run to begin
// executing callbacks.
asyncHook.enable();
// Disable listening for new asynchronous events.
asyncHook.disable();
//
// The following are the callbacks that can be passed to createHook().
//
// init is called during object construction. The resource may not have
// completed construction when this callback runs, therefore all fields of the
// resource referenced by "asyncId" may not have been populated.
function init(asyncId, type, triggerAsyncId, resource) { }
// before is called just before the resource's callback is called. It can be
// called 0-N times for handles (e.g. TCPWrap), and will be called exactly 1
// time for requests (e.g. FSReqWrap).
function before(asyncId) { }
// after is called just after the resource's callback has finished.
function after(asyncId) { }
// destroy is called when an AsyncWrap instance is destroyed.
function destroy(asyncId) { }
// promiseResolve is called only for promise resources, when the
// `resolve` function passed to the `Promise` constructor is invoked
// (either directly or through other means of resolving a promise).
function promiseResolve(asyncId) { }
我很困惑我需要安装 npm install async_hooks
但我认为它是 nodeJs 内置模块所以我们只需要在顶部要求。
经过大量搜索后,我发现我使用的是旧版本的 nodejs 和 async_hooks 模块首先在 Node 版本 8.x 中引入。使用 nvm(节点版本管理器)升级版本后问题已解决。 您可以通过以下link安装和管理多个nodejs版本:https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps
如果 nvm 已经安装,您可以按如下方式打开版本 8.x:
列出所有已安装的版本:
nvm ls
v4.4.4
-> v4.4.7
v8.8.0
system
default -> 4.4.7 (-> v4.4.7)
node -> stable (-> v8.8.0) (default)
stable -> 8.8 (-> v8.8.0) (default)
要打开特定的一个:
nvm use v8.8.0
Now using node v8.8.0 (npm v5.4.2)