How do I get VSCode to recognize current package Javascript imports?

陌路散爱 提交于 2020-03-04 04:25:32

问题


VSCode intellisense is great, when I import a javascript function like

import func from './file';

vs code will give me a helpful dialog with its arguments from jsdoc. This is because I'm using a relative file path.

However, if I'm working with my current package, lets call it "public" as listed in my package.json, and try to reference a file using its absolute path, the intellisense doesn't pick up on the file.

// import the same file as above, but using absolute path
// public is the name of the current npm package
import func from 'public/subfolder/file'; 

intellisense no longer appears.

Is this a bug or a configuration issue?


回答1:


Step1

Create a jsconfig.json file to indicate a JavaScript project within vs code, just go to the bottom of vs code and click on green bulb icon.

It will ask you to create jsconfig.json. Create jsconfig.json file which will contain the following code.

//jsconfig.json
{
 // See https://go.microsoft.com/fwlink/?LinkId=759670
 // for the documentation about the jsconfig.json format
 "compilerOptions": {
 "target": "es6",
 "module": "commonjs",
 "allowSyntheticDefaultImports": true
 },
 "exclude": [
 "node_modules",
 "bower_components",
 "jspm_packages",
 "tmp",
 "temp"
 ]
}

Step2 Install typings globally for downloading TypeScript Definition files (typings) for node modules like express, mongoose, angular etc.

npm install typings --global

TypeScript definition files are written in TypeScript to provide a VS IntelliSense experience for the functions and it's parameters. Let's install typings for express as given below:

typings insall dt~express --global

Let's try the coding experience for express. You will see the great intelliSense for express.



来源:https://stackoverflow.com/questions/48709594/how-do-i-get-vscode-to-recognize-current-package-javascript-imports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!