denoland / deno

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

Bug: ESM module in `@stencil/core` incorrectly detected as CJS #23059

Open marvinhagemeister opened 7 months ago

marvinhagemeister commented 7 months ago

Steps to reproduce:

  1. Run this snippet:
    import Bookmarks from "npm:@arcgis/core@4.29/widgets/Bookmarks.js";
    console.log(Bookmarks);

Error:

error: 'import', and 'export' cannot be used outside of module code at file:///Users/marvinhagemeister/Library/Caches/deno/npm/registry.npmjs.org/@stencil/core/4.9.0/internal/client/index.js:24:1

  import { BUILD, NAMESPACE } from '@stencil/core/internal/app-data';
  ~~~~~~

Version: Deno 1.41.3

dsherret commented 7 months ago

That's expected because the module doesn't seem to define itself as Node ESM. The same thing happens in Node:

> node main.mjs
file:///V:/scratch/node_modules/@esri/calcite-components/dist/components/index.js:7
export { getAssetPath, setAssetPath, setNonce, setPlatformOptions } from '@stencil/core/internal/client/index.js';
         ^^^^^^^^^^^^
SyntaxError: Named export 'getAssetPath' not found. The requested module '@stencil/core/internal/client/index.js' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@stencil/core/internal/client/index.js';
const { getAssetPath, setAssetPath, setNonce, setPlatformOptions } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:132:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:214:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:120:12)

Node.js v21.6.1

That said, in the next version of Deno we're changing this to load CJS modules that uses ES module syntax to work, but the following error occurs due to the Node ESM code doing a directory import:

> deno run main.mjs
error: [ERR_UNSUPPORTED_DIR_IMPORT] Directory import 'file:///V:/scratch/node_modules/@esri/calcite-components/node_modules/@stencil/core/internal/app-data' is not supported resolving ES modules imported from file:///V:/scratch/node_modules/@esri/calcite-components/node_modules/@stencil/core/internal/client/index.js
marvinhagemeister commented 7 months ago

Good point, maybe that's one of those modules that assume to be put through a bundler by default.

dsherret commented 7 months ago

I think so. Btw, for the ERR_UNSUPPORTED_DIR_IMPORT, I wonder if we should apply sloppy import resolution within npm packages when sloppy imports is enabled.