0

I would really like intellisense to work on VS Code and it does if I choose a string literal for the module I wish to require. For eg:

// When using this, intellisense works
const myLib = require("../../MyLibraries/myLibrary.js");

When I use this, intellisense does not work

const pathToLib = "../../MyLibraries";
const myLib = require(`${pathToLib}/myLibrary.js`);

Or when I use this, intellisense still does not work.

function requireFromLib(file) {
    return require(`../../MyLibraries/${file}`);
}

const logger = requireFromLib('myLibrary.js');

This last option was recommended by ChatGPT and it also said to restart Typescript if this fails. I have tried them both and it still does not work.

I have also tried process.env. and that fails too.

I can use string literal, but I have a lot of modules from my library that I include from time to time and using a base path to the Library's folder makes it easier to make changes. Any advice on how to get intellisense to work?

Curious101
  • 1,538
  • 2
  • 18
  • 36
  • [vscode-node-module-intellisense](https://github.com/leizongmin/vscode-node-module-intellisense) I think an extension like this one is what you're looking for? Note that installing third-party extensions can be risky. – Magic1647 Aug 01 '23 at 04:17
  • Thank you, @Magic1647. I will play around with it and see if this works for my needs and weigh the pros and cons coz I feel the same way as you about 3rd party extensions. – Curious101 Aug 01 '23 at 05:10
  • Does this answer your question? [How can I use "Go To Definition" for CommonJS \`require\`s in VS Code when the path to resolve is derived using runtime functions and values?](https://stackoverflow.com/questions/75843679/how-can-i-use-go-to-definition-for-commonjs-requires-in-vs-code-when-the-pat) – starball Aug 01 '23 at 06:00
  • Thanks @starball. That does answer the question. It doesn't solve my problem, but it does confirm that a solution to this does not exist at this time. – Curious101 Aug 04 '23 at 03:08

0 Answers0