I am setting up a node.js project that uses a native add-on. The native add-on includes a large number of exported functions. I've setup a typings file (.d.ts
) that includes all the function definitions and data etc. that are exported from the native add-on. When I pack all of this up with npm, and install it into the client project, the vscode intellisense picks up all the types and all is well.
When I try to use the typings for a test.js
in the same project as the native add-on, the typings are not being picked up, specifically the exported variables; I suspect it has something to do with the way they are exported in the .d.ts
, or the naming of the module in the .d.ts
.
In the .d.ts
, I have the exports listed as;
interface MyI {
Initiate() : void;
}
module 'modulename' {
export var i : MyI;
}
I require
the module in the client as (.js
file);
var i = require("modulename");
In the test code, I require it as (since I stub it through a index.js
file);
var i = require("./index.js");
The index.js
in turn looks like;
var i = require("./lib/nativeaddon");
module.exports.i = i;
How to I get vscode to use the typings, locally, for the intellisense when I use the add-on (via the index.js
) for the test.js
?
You can trigger IntelliSense in any editor window by typing Ctrl+Space or by typing a trigger character (such as the dot character (.) in JavaScript).
One of the biggest advantages of using Visual Studio Code with node. js has to be Intellisense. If you want to see the available suggestions at any time, simply use the keyboard shortcut Ctrl-Space. You can also leverage Intellisense when working with the types included with node.
To create the typings for vscode intellisense to work for both the "local" case (functional with test.js
) and the "global" case (as a node_module), naming the file after the main/entry .js
does the trick. In this case the "main"
file is index.js
, so the typings become index.d.ts
.
This does seem natural, but I haven't been able to find the documentation for the vscode intellisense that specifies this as such.
I had previously named the typings after the package/node_module name, packagename.d.js
) and kept the "main"
(from package.json
) as index.js
. The "typings" value in the package.json
should also match the .d.ts
file name.
I suppose a neat alternative to the "index.js" or "main.js", would be to name the main entry point, and the corresponding typings, after the package name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With