denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.02k stars 5.36k forks source link

ERR_UNSUPPORTED_DIR_IMPORT when importing npm package #26477

Open tsalsa919 opened 6 days ago

tsalsa919 commented 6 days ago

Version: Deno 2.0.2

Just try to use node-hue-api in Deno. And I see that it yield in ERR_UNSUPPORTED_DIR_IMPORT. Seems like it is due to the the way how .js/.ts/.mjs extension are handled in the file. Is there a way to pass --experimental-specifier-resolution=node to import to fix this.

deno install npm:node-hue-api

main.ts file: import * as hue from 'npm:node-hue-api'

deno run main.ts Got the following errors from the console.

error: [ERR_UNSUPPORTED_DIR_IMPORT] Directory import 'file:///<project_dir>/node_modules/.deno/node-hue-api@5.0.0-beta.16/node_modules/node-hue-api/dist/esm/api/discovery' is not supported resolving ES modules imported from 'file:///<project_dir>/node_modules/.deno/node-hue-api@5.0.0-beta.16/node_modules/node-hue-api/dist/esm/index.js'

marvinhagemeister commented 4 days ago

Looking at the code they ship in the node-hue-api package it seems like they're distributing files in ESM syntax, but expect CommonJS resolution. The same error is thrown when you try to import that package in Node with ESM.

node:internal/modules/esm/resolve:259
    throw new ERR_UNSUPPORTED_DIR_IMPORT(path, fileURLToPath(base), String(resolved));
          ^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/<project>/node_modules/node-hue-api/dist/esm/api/discovery' is not supported resolving ES modules imported from /<project>/node_modules/node-hue-api/dist/esm/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:259:11)
    at moduleResolve (node:internal/modules/esm/resolve:933:10)
    at defaultResolve (node:internal/modules/esm/resolve:1169:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:87:39)
    at link (node:internal/modules/esm/module_job:86:36) {
  code: 'ERR_UNSUPPORTED_DIR_IMPORT',
  url: 'file:///<project>/node_modules/node-hue-api/dist/esm/api/discovery'
}

It's an unfortunate situation, because the package say that they support ESM, but it turns out that they do not. They have an issue for that in their tracker already https://github.com/peter-murray/node-hue-api/issues/220