Configure JavaScript libraries | PyCharm (original) (raw)

In context of the language and the IDE, a library is a file or a set of files. Functions and methods of these files are added to PyCharm's internal knowledge in addition to the functions and methods that PyCharm retrieves from the project code that you edit. In the scope of a project, its libraries by default are write-protected.

PyCharm reserves two predefined auto-generated library folders:

Download TypeScript community stubs (TypeScript definition files)

In PyCharm, DefinitelyTyped stubs can be configured and used as libraries, which is in particular helpful in the following cases:

The example below shows a piece of code from an Express application where the post() function is not resolved:

Symbol not resolved without d.ts

PyCharm successfully resolves post() after you install a TypeScript definition file:

Symbol resolved after d.ts is installed

With PyCharm lets you download TypeScript definition files right from the editor, using an intention action, or you can do it on the Settings: JavaScript Libraries page.

Download TypeScript definitions using an intention action

Download TypeScript definitions on the JavaScript Libraries page

  1. Press Ctrl+Alt+S to open settings and then select .
  2. On the Libraries page that opens, click Download and in the Download Library dialog that opens, select the required library, and click Download and Install.
    Add TypeScript definition file

PyCharm shows the downloaded type definitions in the Project tool window Alt+1, under the External Libraries node.

Downloaded type definitions are shown under the External libraries node

Optionally

Configure Node.js Core library

To get code completion and reference resolution for fs, path, http, and other core modules that are compiled into the Node.js binary, you need to configure the Node.js Core module sources as a JavaScript library.

  1. Press Ctrl+Alt+S to open settings and then select .
  2. Select the Coding assistance for Node.js checkbox.
    Note that the Node.js Core library is version-specific. So if you change the version of your Node.js on the Node.js page, you need to select the checkbox again. After that PyCharm creates a new library for this new version.

Configure node_modules library

To provide code completion for project dependencies, PyCharm creates a node_modules library automatically so Node.js modules are kept in your project but no inspections are run against them, which improves performance.

node_modules library added to the list

In the Project tool window Alt+1, the node_modules is also marked as a library:

node_modules folder marked as library root.png

However, the node_modules library contains only the modules that are listed in the dependencies object of your project package.json file. PyCharm does not include the dependencies of dependencies into the node_modules library but actually excludes them from the project.

Configure the scope of a library

From time to time you may notice that PyCharm suggests irrelevant completion, for example, Node.js APIs in your client-side code. This happens because by default PyCharm uses a library for completion in the entire project folder. PyCharm lets you tune code completion by configuring scopes for libraries.

  1. Press Ctrl+Alt+S to open settings and then select .
    The Libraries page shows a list of available libraries.
  2. Clear the Enabled checkbox next to the required library, and click Manage Scopes. The JavaScript Libraries. Usage Scope dialog opens.
  3. In the JavaScript Libraries Usage Scopes dialog, click the Add button () and select the files or folders that you want to include in the library scope. In the JavaScript Libraries Usage Scopes dialog, the Path field shows selected files or folders.
  4. For each added file or a folder, from the Library list, select the library that you are configuring.

Example: Configure the scope for HTML and Node.js Core libraries

When working on a full-stack JavaScript application in PyCharm, you often notice that code completion suggests some Node.js API in your client-side code and DOM API in the Node.js code. This happens because the HTML library with DOM API and the Node.js Core library with Node.js API are by default enabled in the whole project. To get rid of irrelevant completion suggestions, you need to configure the scope for these libraries.

  1. Press Ctrl+Alt+S to open settings and then select .
    The Libraries page shows a list of available libraries.
    Configure JavaScript libraries: a list of available libraries
  2. Clear the Enabled checkboxes next to HTML and Node.js Core items.
    Clear Node.js Core and HTML
  3. Click Manage Scopes. The JavaScript Libraries Usage Scopes dialog opens.
  4. To configure the scope for the HTML library, click app.general.add.png.
    Add scope for HTML
    In the dialog that opens, select the folders with the client-side code and click Open.
    Add scope for HTML: select folders
    You return to the JavaScript Libraries Usage Scopes dialog where the selected folders are added to the list.
    Manage Scopes dialog: folders for HTML library added
    From the Library list next to each folder, select HTML.
    Specify scope: HTML library selected
  5. To configure the scope for the Node.js Core library, click again, add the folders with the server-side code, and for each of them select Node.js Core from the Libraries list.
    Specify scope: Node.js Core library selected
    Now PyCharm resolves items from the HTML and Node.js Core libraries and suggests them in completion only in files from these chosen project folders.

In the same way, you can configure the scope of the automatically created node_modules library, refer to Configuring node_modules library.

When a library .js file is referenced through a CDN link, it is available for the runtime but is invisible for PyCharm. To add the objects from such library .js file to completion lists, download the file and configure it as an external library.

Download a library

Change the visibility of a library

Configure a custom third-party JavaScript library

Suppose you have a JavaScript framework file in your project or elsewhere on your machine, and you want PyCharm to treat it as a library and not just as your project code that you edit.

  1. Download the required framework file.
  2. Press Ctrl+Alt+S to open settings and then select .
  3. On the Libraries page, click Add.
  4. In the New Library dialog, specify the name of the external JavaScript library.
  5. Click the Add button (app.general.add.png), and select Attach Files or Attach Directories from the list.
    Configure custom library: Add files/folders
    In the file browser, select the file or folder with the downloaded framework.
    Configure custom library: library added
    When you click OK, you return to the Libraries page where the new library is added to the list.
    The new custom library is added to the list
  6. In the Project tool window Alt+1, the library is shown under the External Libraries node.
    A custom library is shown under the external Libraries node

Optionally

View the libraries associated with a file

Delete a library

  1. Press Ctrl+Alt+S to open settings and then select .
    The Libraries page displays a list of available libraries.
  2. Select a library and click Remove.

Last modified: 22 November 2024