denoland / deno

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

npm packages with json entrypoint fail to load #19521

Closed dsherret closed 1 year ago

dsherret commented 1 year ago
import names from "npm:all-the-package-names"

const chars = new Set();
for (const name of names) {
  for (const c of name) {
    chars.add(c)
  }
}

console.log(Array.from(chars).sort());
> deno run main.ts
error: Expected a "JavaScriptOrWasm" module but loaded a "JSON" module.
// package.json
{
  "name": "all-the-package-names",
  "version": "2.0.838",
  "description": "A list of all the public package names on npm. Updated daily.",
  "main": "names.json",
  "bin": "cli.js",
  "scripts": {
    "build": "node script/build.js",
    "test": "node test/index.js | tap-spec"
  },
  "repository": "https://github.com/nice-registry/all-the-package-names",
  "keywords": [
    "npm",
    "registry",
    "packages",
    "names",
    "list",
    "words",
    "filter",
    "search",
    "offline"
  ],
  "author": "zeke",
  "license": "MIT",
  "devDependencies": {
    "all-the-package-repos": "^2",
    "is-number": "^7.0.0",
    "tap-spec": "^5.0.0",
    "tape": "^5.5.0"
  }
}
dsherret commented 1 year ago

Oh, maybe not a bug. Node esm also errors:

> node main.mjs
node:internal/errors:484
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///V:/scratch/node_modules/.deno/all-the-package-names@2.0.838/node_modules/all-the-package-names/names.json" needs an import assertion of type "json"
    at new NodeError (node:internal/errors:393:5)
    at validateAssertions (node:internal/modules/esm/assert:82:15)
    at defaultLoad (node:internal/modules/esm/load:84:3)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at #createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34)
    at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:78:21) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}