microsoft / vscode-languageserver-node

Language server protocol implementation for VSCode. This allows implementing language services in JS/TS running on node.js
MIT License
1.45k stars 325 forks source link

Can't import vscode-languageserver/node with nodeNext #1144

Open razzeee opened 1 year ago

razzeee commented 1 year ago

Hey,

I've been exploring using nodeNext, but this import seems to fail.

import { createConnection } from "vscode-languageserver/node";

See https://github.com/elm-tooling/elm-language-server/pull/854/files#diff-a2a171449d862fe29692ce031981047d7ab755ae7f84c707aef80701b3ea0c80R12

Cannot find module 'vscode-languageserver/node' or its corresponding type declarations.

Or just https://github.com/elm-tooling/elm-language-server/pull/854 for the PR

dbaeumer commented 1 year ago

To my understanding in nodeNext imports outside the default import need to be listed in the package.json file which is currently not the case.

dbaeumer commented 1 year ago

Happy to accept a PR that adds this.

razzeee commented 1 year ago

Adding this seems to work:

"exports": {
        ".": {
            "import": "./lib/common/api.js",
            "require": "./lib/common/api.js",
            "types": "./lib/common/api.d.ts"
        },
        "./node": {
            "import": "./lib/node/main.js",
            "require": "./lib/node/main.js",
            "types": "./lib/node/main.d.ts"
        }
    }

but I end up with a followup error:

'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.

image

dbaeumer commented 1 year ago

Actually right now I have no idea what is causing this :-)

razzeee commented 1 year ago

I'm way out of my depth too :shrug:

remcohaszing commented 1 year ago

This doesn’t work, because there is no export nor file named node in the vscode-languageserver package. TypeScript correctly states this import can’t be resolved.

Instead, you should use:

import { createConnection } from "vscode-languageserver/node.js";

Adding the exports as per https://github.com/microsoft/vscode-languageserver-node/issues/1144#issuecomment-1347547590 would break the existing solution, making it a breaking change.

You could use exports to make this work both in Node and the browser, which is the cleanest solution IMO. However, this would also be breaking, although it can me made non-breaking using a verbose exports map.

{
  "exports": {
    ".": {
      "browser": "./lib/browser/main.js",
      "default": "./lib/node/main.js",
    }
  }
}

This would allow using:

import { createConnection } from "vscode-languageserver";

Anyway, it currently works as-is. No action is required to allow consuming this package in ESM.