Error: Cannot find module 'async_hooks' in NodeJs

烈酒焚心 提交于 2020-01-25 09:41:07

问题


I am trying to run test code of Async Hooks from official node api documentation and i am getting error Error: Cannot find module 'async_hooks' in console. I already include const async_hooks = require('async_hooks'); at the top of my script. Here is my code:

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) { }

I am confused that i need to install with npm install async_hooks but i think it's nodeJs in-build module so we just need to require at the top.


回答1:


After searching a lot i found that i was using older version of nodejs and the async_hooks module introduced first in Node Version 8.x. After upgrading version with nvm (node version manager) issue resolved. You can install and manage multiple nodejs version by following link: https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps

If nvm is install already than you can switch on version 8.x as follow:

To list all installed versions:

 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)

To switch on specific one:

nvm use v8.8.0
Now using node v8.8.0 (npm v5.4.2)


来源:https://stackoverflow.com/questions/51702158/error-cannot-find-module-async-hooks-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!