nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
106.43k stars 29.01k forks source link

TypeError: AsyncLocalStorage is not a constructor #32320

Closed averri closed 4 years ago

averri commented 4 years ago

What steps will reproduce the bug?

Run the demo code provided in https://nodejs.org/api/all.html#async_hooks_class_asynclocalstorage

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior?

Nodejs should recognize all the API functions and run the example code with success.

What do you see instead?

/home/alex/dev/src/other/strenux/strenux-api/test/exploratory/async-storage.js:5
const asyncLocalStorage = new AsyncLocalStorage()
                          ^

TypeError: AsyncLocalStorage is not a constructor
    at Object.<anonymous> (/home/alex/dev/src/other/strenux/strenux-api/test/exploratory/async-storage.js:5:27)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
jasnell commented 4 years ago

@averri, it would be helpful to be able to see the code to understand what is happening. I'm unable to recreate the issue locally.

cjihrig commented 4 years ago

I cannot reproduce on 13.11.0 either. Are you sure that's the version you're running (you can add a console.log(process.version) to your script)?

averri commented 4 years ago

Thanks @jasnell and @cjihrig. There was an issue with nvm which was not setting the environment with Nodejs 13.11.0 as I was expecting. I reinstalled nvm, and got it working correctly.

I was using the code inspired by Nodejs 13 documentation (ref: What steps will reproduce the bug?):

const {AsyncLocalStorage} = require('async_hooks')
const http = require('http')

console.log(process.version)

const kReq = 'CURRENT_REQUEST'
const asyncLocalStorage = new AsyncLocalStorage()

function log(...args) {
  const store = asyncLocalStorage.getStore()
  // Make sure the store exists and it contains a request.
  if (store && store.has(kReq)) {
    const req = store.get(kReq)
    // Prints `GET /items ERR could not do something
    console.log(req.method, req.url, ...args)
  } else {
    console.log(...args)
  }
}

http.createServer((request, response) => {
  asyncLocalStorage.run(new Map(), () => {
    const store = asyncLocalStorage.getStore()
    store.set(kReq, request)
    new Promise(resolve => {
      log('INFO', 'sleeping...')
      setTimeout(() => {
        log('INFO', 'resolving...')
        resolve()
      }, 1000)
    }).then(() => {
      log('INFO', 'finished')
      response.statusCode = 200
      response.write('ok')
      response.end()
    })
  })
})
  .listen(8080)

Running this code with Nodejs 13.11.0 and invoking GET at /index, I'm able to see the logs:

v13.11.0
GET /index INFO sleeping...
GET /index INFO resolving...
GET /index INFO finished
GET /favicon.ico INFO sleeping...
GET /favicon.ico INFO resolving...
GET /favicon.ico INFO finished

... which demonstrates that there are no issues.