swc-project / swc-project/swc-node

`package.json` "imports" field support

Open
#724 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
2k
Forks
95
Avg merge
14d 11h
Merged PRs (30d)
1

Description

There is https://nodejs.org/api/packages.html#imports and resolvePackageJsonImports in tsconfig.json.
My configuration:

import eql from '#eql'

tsconfig.json:

"compilerOptions": {
  "strict": true,
  "baseUrl": ".",

  "target": "ESNext",
  "module": "NodeNext",
  "moduleResolution": "NodeNext",
  "resolvePackageJsonImports": true,

  "noEmit": true,
  "isolatedModules": true,

  "skipDefaultLibCheck": true,
  "skipLibCheck": true,
},
// ...

package.json:

"type": "module",
"imports": {
  "#eql": "./eql/index.mts"
},
// ...

Running node --loader=@swc-node/register/esm main.ts results in:

node:internal/process/esm_loader:46
      internalBinding('errors').triggerUncaughtException(
                                ^
RangeError [ERR_UNKNOWN_MODULE_FORMAT]: Unknown module format: undefined for URL file:///home/foxpro/craft/sferadel/dev/api/eql/index.mts
    at new NodeError (node:internal/errors:405:5)
    at Object.throwUnknownModuleFormat (node:internal/modules/esm/load:155:9)
    at Hooks.load (node:internal/modules/esm/hooks:417:44)
    at async handleMessage (node:internal/modules/esm/worker:168:18) {
  code: 'ERR_UNKNOWN_MODULE_FORMAT'
}

Node.js v20.5.1

I tried to change to:

"imports": {
  "#eql": "./eql/index.mjs"
},

Then, error is

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/foxpro/craft/sferadel/dev/api/eql/index.mjs' imported from /home/foxpro/craft/sferadel/dev/api/user/user.mts

It's closed circle. Am I doing it in a wrong way?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Node ESM loader entry point used by node --loader=@swc-node/register/esm main.ts, reproducing the configuration in tsconfig.json and package.json with the #eql import from user/user.mts. Done means the package imports mapping resolves and loads eql/index.mts without ERR_UNKNOWN_MODULE_FORMAT; also check the reported .mjs failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.