You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/runtimes/nodejs.md
+9-63Lines changed: 9 additions & 63 deletions
Original file line number
Diff line number
Diff line change
@@ -159,70 +159,11 @@ code .
159
159
160
160
The [Node.js](https://nodejs.org/api/) and [Express](http://expressjs.com/api.html) documentation does a great job explaining how to build rich applications using the platform and framework. Visual Studio Code will make you more productive developing these types of applications by providing great code editing and navigation experiences.
161
161
162
-
Earlier we saw the IntelliSense that the JavaScript language service can infer about your source code. Next we will see that with a little more setup and configuration, Visual Studio Code can provide even richer information and build support.
163
-
164
-
## Adding a jsconfig.json Configuration File
165
-
166
-
When VS Code detects that you are working on a JavaScript file (open the `app.js` file), it looks to see if you have a JavaScript configuration file `jsconfig.json` in your workspace. If it doesn't find one, you will see a green lightbulb on the Status Bar prompting you to create one.
Click the green lightbulb and accept the prompt to create a `jsconfig.json` file:
171
-
172
-
```json
173
-
{
174
-
"compilerOptions": {
175
-
"target": "es6",
176
-
"module": "commonjs",
177
-
"allowSyntheticDefaultImports": true
178
-
},
179
-
"exclude": [
180
-
"node_modules",
181
-
"bower_components",
182
-
"jspm_packages",
183
-
"tmp",
184
-
"temp"
185
-
]
186
-
}
187
-
188
-
```
189
-
190
-
If you do not have [Auto Save](/docs/editor/codebasics.md#saveauto-save) on, save the file by pressing `kb(workbench.action.files.save)`.
191
-
192
-
The presence of this file lets VS Code know that it should treat all the files under this root as part of the same project. We'll see in the next section that this is important for extending IntelliSense by adding typings (Type Definition files) to your workspace. This configuration file also lets you specify settings such as `compilerOptions` and which folders you'd like the JavaScript language service to `exclude` (ignore). The default `jsconfig.json` file we just created tells the JavaScript language service that you are writing ES6 compliant code and you want to ignore dependency and temporary content folders.
193
-
194
-
## IntelliSense and Typings
195
-
196
-
VS Code can use TypeScript definition files (for example [`node.d.ts`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/node.d.ts)) to provide metadata to VS Code about the JavaScript based frameworks you are consuming in your application. Because TypeScript definition files are written in TypeScript, they can express the data types of parameters and functions, allowing VS Code to provide a rich IntelliSense experience.
197
-
198
-
[Typings](https://github.com/typings/typings), the type definition manager for TypeScript, makes it easy to search for and install TypeScript definition files into your workspace. This tool can download the requested definitions from a variety of sources, including the [DefinitelyTyped repository](https://github.com/DefinitelyTyped/DefinitelyTyped). As we did with the Express Generator, we will install the Typings command line tool globally using NPM so that you can use the tool in any application you create.
199
-
200
-
```bash
201
-
npm install -g typings
202
-
```
203
-
204
-
>**Tip:** Typings has a number of options for configuring where and how definition files are downloaded. From the terminal, run `typings --help` for more information.
205
-
206
-
Go back to the file `app.js` and notice that if you hover over the Node.js global object `__dirname`, VS Code does not know the type and displays `any`.
207
-
208
-
Now, using the Typings command line, pull down the Node.js and Express type definition files:
The `dt~` prefix tells the Typings tool to search the DefinitelyTyped repository for the specified type definition files.
216
-
217
-
>**Tip:** You can download multiple definition files by combining them on the command line, as you can see from the Express typings above. We need to install the typings for Express and also it's references.
218
-
219
-
>**Note:** Don't worry if you see `typings INFO reference` messages during installation. The Typings tool is cleaning out unnecessary `///` references in the downloaded typings files.
220
-
221
-
Notice how VS Code now understands what `__dirname` is, based on the metadata from the `node.d.ts` file. Even more exciting, you can get full IntelliSense against the Node.js framework. For example, you can require `http` and get full IntelliSense against the `http` class as you type in Visual Studio Code.
162
+
Open the file `app.js` and hover over the Node.js global object `__dirname`. Notice how VS Code understands what `__dirname` is. Even more exciting, you can get full IntelliSense against the Node.js framework. For example, you can require `http` and get full IntelliSense against the `http` class as you type in Visual Studio Code.
>**Note:** Make sure you have a `jsconfig.json` file in your workspace root as described in the [previous section](/docs/runtimes/nodejs.md#adding-a-jsconfigjson-configuration-file) so VS Code will pick up the installed typings files.
166
+
VS Code uses TypeScript definition files (for example node.d.ts) to provide metadata to VS Code about the JavaScript based frameworks you are consuming in your application. Because TypeScript definition files are written in TypeScript, they can express the data types of parameters and functions, allowing VS Code to provide a rich IntelliSense experience. Thanks to a feature called `Automatic Typing Acquisition` you as user do not have to worry about these typings file. VS Code will install them automatically for you.
226
167
227
168
You can also write code that references modules in other files. For example, in `app.js` we require the `./routes/index` module, which exports an `Express.Router` class. If you bring up IntelliSense on `routes`, you can see the shape of the `Router` class.
228
169
@@ -284,6 +225,11 @@ There is much more to explore with Visual Studio Code, please try the following
284
225
285
226
## Common Questions
286
227
287
-
**Q: IntelliSense isn't working for Node.js and Express after I install their typings?**
228
+
**Q: Do I need to define a `jsconfig.json` file?**
229
+
230
+
**A:** Without a `jsconfig.json` file in the workspace root folder VS Code treats all files and folders as belonging to the same project context. This is sufficient for common setups. There are situations when you want to add a `jsconfig.json` file. For example,
231
+
- when not all JavaScript files should be part of the project context, for example, you want to exclude some files, then you can define which files to exclude in the jsconfig.json file.
232
+
- when a workspace contains more then one project context, then you should add a `jsconfig.json` file at the root folder for each project.
233
+
- when you are using the TypeScript compiler to down-level compile JavaScript source code.
288
234
289
-
**A:** Be sure you have a `jsconfig.json` file in the workspace root folder so that VS Code treats all files and folders as belonging to the same project context. Without a `jsconfig.json`, VS Code considers JavaScript and TypeScript files in isolation and won't associate your source code types with the typings type definitions files.
235
+
More information about the configuration options for a `jsconfig.json` can be found in [jsconfig.json reference](/docs/languages/javascript.md#javascript-project-jsconfigjson).
0 commit comments