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

On Node 20: The requested module 'hot-shots' does not provide an export named 'StatsD' #3703

Closed
robaca opened this issue Oct 11, 2023 · 6 comments · Fixed by #3975
Closed
Labels
bug Something isn't working

Comments

@robaca
Copy link

robaca commented Oct 11, 2023

We just try to upgrade our Node/ESM project to Node.js 20(.8.0). Without datadog, our services are running fine, but when adding the loader to the startup command via node --loader dd-trace/loader-hook.mjs ..., we get the following error (ignoring the warnings for now):

(node:186) ExperimentalWarning: `--experimental-loader` may be removed in the future; instead use `register()`:
--import 'data:text/javascript,import { register } from "node:module"; import { pathToFileURL } from "node:url"; register("dd-trace/loader-hook.mjs", pathToFileURL("./"));'
(Use `node --trace-warnings ...` to show where the warning was created)
14:31:41.764 ERROR    Error starting service
The requested module 'hot-shots' does not provide an export named 'StatsD'
file:///usr/src/app/libs/server/lib/core/metrics/createStatsdClient.js:1
import { StatsD } from 'hot-shots';
         ^^^^^^
SyntaxError: The requested module 'hot-shots' does not provide an export named 'StatsD'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:131:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:213:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async Cluster.startupMethod (file:///usr/src/app/lib/main/server/index.js:18:30)
    at async Cluster.initializeWorker ```

**Steps to reproduce**

- Create an empty project via `npm init`
- `npm install hot-shots`
- `npm install dd-trace`
- Create a `test.mjs` file with `import { StatsD } from 'hot-shots'`
- `node --loader dd-trace/loader-hook.mjs test.mjs`
- 
**Environment**

* **Operation system: MacOS / Debian Linux**
* **Node.js version: 20.8.0**
* **Tracer version: 4.16.0**
* **Agent version: -**
* **Relevant library versions: hot-shots 10.0.0**
@robaca robaca added the bug Something isn't working label Oct 11, 2023
@robaca
Copy link
Author

robaca commented Oct 12, 2023

I tested the same with Node 18.18.1 and 19.9.0, but didn't get that error

@svrakitin
Copy link

Managed to work around this issue like this:

import statsd from "hot-shots";
const { StatsD } = statsd;

@tlhunter
Copy link
Member

FYI @khanayan123

@robaca
Copy link
Author

robaca commented Jan 9, 2024

Now the issue also occurs on Node 18.19.0.

I tried your workaround, but it doesn't work for me as-is in TypeScript code:

Referencing the type StatsD:

TS2749: StatsD refers to a value, but is being used as a type here. Did you mean typeof StatsD?

It also affects all kinds of modules, like mobx, express, ...

Testing with mobx 6.12.0:

This library is meant to be imported via import * as mobx from 'mobx' or named imports. There is also a default export, but not defined in types and some bundlers do not provide it. You can test it in a type="module" project via the repl:

$ node
> mobx = await import('mobx')
> Object.keys(mobx)
[
  '$mobx',
  'FlowCancellationError',
  'ObservableMap',
  'ObservableSet',
  'Reaction',
  ...
  'default',
  ...
]

dd-trace here is breaking both the named imports and also the access via mobx.

$ node --loader dd-trace/loader-hook.mjs
> mobx = await import('mobx')
> Object.keys(mobx)
[ 'default' ]

For now we have to use an older Node version until this is fixed.

@gingermusketeer
Copy link

I have run into this issue with @nestjs/common.

It appears that the DD loader is not exposing some of the exports from the module:

# With loader enabled
node --import ./register/datadog.mjs                                 
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> (await import("@nestjs/common")).All
undefined
> 

# Without loader enabled
node                                
Welcome to Node.js v20.10.0.
Type ".help" for more information.
> (await import("@nestjs/common")).All
[Function (anonymous)]
>

register/datadog.mjs contains

import { register } from 'node:module'
import { pathToFileURL } from 'node:url'

register('dd-trace/loader-hook.mjs', pathToFileURL(import.meta.url))

@gingermusketeer
Copy link

This problem is solved for me by nodejs/import-in-the-middle#30. For now I am using patch-package to get it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants