Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: AsyncLocalStorage is not a constructor #32320

Closed
averri opened this issue Mar 17, 2020 · 3 comments
Closed

TypeError: AsyncLocalStorage is not a constructor #32320

averri opened this issue Mar 17, 2020 · 3 comments

Comments

@averri
Copy link

averri commented Mar 17, 2020

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
Copy link
Member

jasnell commented Mar 17, 2020

@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
Copy link
Contributor

cjihrig commented Mar 17, 2020

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
Copy link
Author

averri commented Mar 17, 2020

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.

@averri averri closed this as completed Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants