baptisteArno / node-youtube-music

Unofficial YouTube Music API for Node.js
GNU General Public License v3.0
113 stars 21 forks source link

Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules\node-youtube-music\dist\index.js not supported. #20

Closed brunohafonso closed 1 year ago

brunohafonso commented 1 year ago

Using the latest version of the package 0.10.2

baptisteArno commented 1 year ago

What code are you trying to execute?

kayibea commented 1 year ago

i got the same error just trying a simple code like this in typescript

import { searchMusics } from "node-youtube-music";

searchMusics("Hello").then((musics) => {
  console.log(musics);
});
baptisteArno commented 1 year ago

What's your set up? How can I reproduce?

kayibea commented 1 year ago

nothing crazy out here, just a fresh project, i uploaded to github here https://github.com/gloirekiba/groove-tube

kayibea commented 1 year ago

just run npm run dev

baptisteArno commented 1 year ago

This lib is an ES Module, so try to run it with tsnd-esm instead of tsnd. Would that fix the issue?

dkregen commented 1 year ago

Same to me. I'm using CommonJS in my tsconfig.json configuration:

{
  "compilerOptions": {
    "suppressImplicitAnyIndexErrors": true,
    "noImplicitAny": false,
    "noImplicitThis": false,
    "strictNullChecks": false,
    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "module": "CommonJS",
    "target": "es5",
    "sourceMap": true,
    "outDir": "dist",
    "esModuleInterop": true
  },
  "include": [
    "src/**/*"
  ]
}

Try to call a method that will produce the error.

import { searchMusics } from "node-youtube-music";

searchMusics("Hello").then((musics) => {
  console.log(musics);
});

Solution: To import an ES module in CommonJS, you can use the import() function with a dynamic import statement.

const music = await import('node-youtube-music')
const r = await music.searchMusics(q)

Another workaround I tried is to use version 0.7.0

supersu-man commented 1 year ago

Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Projects\sangeet-discordbot\node_modules\got\dist\source\index.js from D:\Projects\sangeet-discordbot\node_modules\node-youtube-music\dist\index.js not supported.

I am trying to run .ts file and I get this error.

For now I am switching back to older version as a workaround.

baptisteArno commented 1 year ago

Hey guys, read this: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c#pure-esm-package

It 100% applies to node-youtube-music.