最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

错误:无法找到模块的NodeJS“async

IT培训 admin 3浏览 0评论

错误:无法找到模块的NodeJS“async

我想从官方的节点API文档异步运行的鱼钩测试代码,我在控制台收到错误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旧版本和节点版本8.x中首先介绍了async_hooks模块与NVM(节点版本管理器)升级版本后问题解决。您可以通过以下链接安装和管理多个版本的NodeJS:https://www.digitalocean/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)

错误:无法找到模块的NodeJS“async

我想从官方的节点API文档异步运行的鱼钩测试代码,我在控制台收到错误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旧版本和节点版本8.x中首先介绍了async_hooks模块与NVM(节点版本管理器)升级版本后问题解决。您可以通过以下链接安装和管理多个版本的NodeJS:https://www.digitalocean/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)
发布评论

评论列表 (0)

  1. 暂无评论