Skip to content

unable to use from a vscode language server extension #407

Open
@ryepup

Description

@ryepup

Describe the bug

Something about the bundling process isn't working right, and I don't know enough about npm/typescript packaging to be clear and concise.

To reproduce

Steps to reproduce the behavior:

  1. follow https://code.visualstudio.com/api/language-extensions/language-server-extension-guide to make a stub language server in typescript that you can debug in a new VScode instance (aka Extension Development Host)
  2. npm install --save @nginx/reference-lib in the "server" part of the extension
  3. add a import { find } from '@nginx/reference-lib'
  4. run the server by itself, should work fine via tsc && node out/server.js
  5. run the server inside the extension dev host, get an error:

Uncaught Error Error: require() of ES Module .../server/node_modules/@nginx/reference-lib/dist/index.js from .../server/out/server.js not supported.

Futzing around with various arcane tsconfig.json or package.json settings could get different errors, like

Uncaught ReferenceError ReferenceError: exports is not defined in ES module scope This file is being treated as an ES module because it has a '.js' file extension and '...server/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.

or

Uncaught Error Error: Cannot find module '.../server/node_modules/vscode-languageserver/node' imported from .../server/out/server.js Did you mean to import "vscode-languageserver/node.js"?

Expected behavior

Extension dev host can import the library.

Your environment

  • node 23.11
  • macos

Additional context

Asking AI didn't really help. It just repeatedly told me to set things in json files that I already had, or suggested that the lib needed to be published differently to supports modules and commonjs, but all this packaging stuff is not my area of expertise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions