PyCharm 2024.3 Help

npm, pnpm, and Yarn

PyCharm integrates with the npm, pnpm, Yarn, Yarn 2, and Yarn 3, so you can install, locate, update, and remove packages of reusable code from inside the IDE, in the built-in Terminal.

PyCharm also lets you run and debug npm, Yarn, and pnpm scripts. PyCharm parses package.json files, recognizing definitions of scripts, shows scripts in a tree view, and lets you navigate between a script in the tree and its definition in the package.json file. For more information, refer to npm, Yarn, and pnpm official websites.

PyCharm detects projects with Yarn workspaces and indexes all the dependencies that are listed in the package.json files of the workspaces but are located in the root node_modules folder.

Before you start

  • Download and install Node.js. Note that npm is also installed, so if you are going to use it, you are through with the preliminary steps.

  • To use Yarn, install it as described on the Yarn official website.

  • To use pnpm, open the embedded Terminal (Alt+F12) and type:

    npm install --g pnpm

    Learn more from the pnpm official website.

Choose a project package manager

With PyCharm, you can choose whether to use npm, Yarn, Yarn 2, or pnpm in a project.

By default, PyCharm suggests npm. However, if you open a project with a yarn.lock file and Yarn is installed on your computer, PyCharm automatically changes the package manager for this project to Yarn.

Accordingly, if you open a project with a pnpm-lock file and pnpm is installed on your computer, PyCharm automatically changes the package manager for this project to pnpm.

You can also set Yarn 1 or pnpm as default PyCharm package manager.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | Node.js. The Node.js page opens.

  2. In the Package manager field, specify the package manager for your project.

    Select project package manager: choose the system default installation
    • Select npm, yarn, or pnpm to use the system default installation.

    • To use a custom installation of a package manager, click Select, and select the installation folder of the relevant package manager.

      Select project package manager: choose a custom installation

    PyCharm automatically uses the chosen package manager when you select the Project alias from the Package manager list in the Run/Debug Configuration: NPM dialog. PyCharm also uses the path to the chosen package manager every time you invoke the Run 'npm install'/Run 'yarn install'/Run 'pnpm install' command or run an npm/Yarn/pnpm script. For more information, refer to Running and debugging scripts.

Set up Yarn 2 in your project

  1. Make sure you have Yarn 1 installed globally and enable Yarn 2 in your project as described on the Yarn official website. Use the embedded Terminal Alt+F12 to type the commands.

  2. In the Settings dialog (Ctrl+Alt+S) , go to Languages & Frameworks | Node.js and select the path to the Yarn 2 package in your project from the Package manager list.

Set Yarn 1 or pnpm as the default PyCharm package manager

  1. Open the Settings for New Projects dialog (File | Settings for New Projects) and go to Languages & Frameworks | Node.js.

  2. On the Node.js and NPM page that opens, select yarn or pnpm from the Package manager list.

After that, PyCharm will suggest the selected package manager (Yarn or pnpm) as default every time you create a new project.

Edit package.json

PyCharm helps you handle your project dependencies in package.json files providing extensive coding assistance.

  • Code completion for package names.

    Code completion for package names
  • Information on the latest available package version.

    Showing the latest available package version
  • Information about the range of versions that can be installed through npm install <package>@<version> or yarn add <package>@<version>. `Note that running npm install or yarn install will install the latest available version from this range.

    Press Ctrl and hover over the version to see the information in a tooltip. See the npm Official documentation for details about semantic versioning.

    Showing the range of available package versions
  • Indication whether the specified version of a package is installed or not and information about the latest available version

    Info whether the version of a package is installed
  • Code completion for previous package versions. When you press Ctrl+Space or start typing a version different from the latest one, PyCharm displays a suggestion list with all the previous versions of the package.

    Code completion for previous package versions
  • Quick documentation look-up for packages.

    Quick documentation look-up for packages

Install and update packages

As you may know, npm can install packages both globally or as project dependencies or development dependencies, learn more from the npm official website.

pnpm also installs packages globally or as project dependencies or development dependencies, learn more from the pnpm official website.

With Yarn, you can install packages globally or as project dependencies. For more information, refer to Yarn official website.

In PyCharm, packages can be installed in the editor, from a package.json file, in the built-in Terminal Alt+F12, or on the Node.js page.

From package.json

Install your project dependencies

  • Click the Run 'npm install', Run 'yarn install', or Run 'pnpm install' link in the popup:

    Install dependencies popup

    PyCharm shows this popup when the dependencies are not installed yet or when they have changed. If you close the popup or turn it off by choosing Don't ask again, you can still install the dependencies using the Run '<package_manager> install' action or in the built-in terminal.

    If PyCharm cannot detect a Node.js interpreter, the error is reported in a popup with a link to the Node.js page where you can configure a Node.js interpreter.

    Node.js not found

    If you have accidentally dismissed this notification and now want to get it back, press Ctrl+Shift+A, start typing Enable notifications, and select Enable Notifications about Installing Dependencies from package.json from the list.

  • Alternatively, open the relevant package.json file in the editor or select it in the Project tool window and choose Run 'npm install' from the context menu.

Update your project dependencies

  • Click Run '<package manager> install' in the popup.

    Update dependencies: popup

    PyCharm shows this popup every time you open a project, update it from the version control, or edit a package.json.

  • PyCharm also runs an inspection that checks whether the packages from dependencies or devDependencies are installed and their versions match the specified range. If the inspection detects any mismatch, it suggests a quick-fix. To apply it, click the Run '<package manager> install' link.

    Installed version of a package is not up to date
  • PyCharm also detects packages for which later versions are available and provides a quick-fix when you hover over the package version.

    Update package to latest version: tooltip

    Alternatively, press Alt+Enter and select Update <package> to the latest version from the list:

    Update package to latest version: quick-fix

In the built-in Terminal

In the embedded Terminal (Alt+F12) , type one of the following commands:

  • For global installation:

    • npm install --global <package_name>

    • yarn global add <package_name>

    • pnpm --global add <package_name>

  • To install a package as a project dependency or a development dependency:

    • npm install --save <package_name> or npm install --save-dev <package_name>

    • yarn add <package_name> --dev

    • pnpm add --save-dev <package_name>

Install all dependencies listed in a package.json file

In the embedded Terminal (Alt+F12) , type one of the following commands:

  • npm install

  • yarn install

  • pnpm install

As a result you get all the dependencies listed in the package.json from the current folder. Learn more from Editing package.json.

Detect vulnerable packages

PyCharm detects vulnerabilities in the packages used in your projects by checking across the Checkmarx SCA Database and the National Vulnerability Database. As you write your code, the IDE highlights packages that are considered vulnerable, provides descriptions for them, and suggests fixes where available. The entire list of detected vulnerable packages is shown in the Problems tool window.

Vulnerability check is handled by the Vulnerable declared dependency code inspections that rely on the Package Checker plugin. The plugin is powered by application security leader Checkmarx. The plugin is bundled with PyCharm and enabled by default. If the relevant features aren't available, make sure that you didn't disable the plugin. For more information, refer to Open plugin settings.

Detect vulnerable packages and fix them in the editor

Solve package vulnerability problems in the editor

  • Hover over the highlighted package in a package.json file. The popup tool window shows a description of the detected vulnerability.

  • To apply a quick-fix, click More actions Alt+Enter and select the relevant suggestion.

View vulnerable packages in the Problems tool window

  1. Click the Problems icon in the inspection widget.

  2. The File tab of the Problems tool window lists all the vulnerable packages from the package.json that is open in the active editor tab. Each item is supplied with a description.

    To solve a problem, select the vulnerable package in the list, click the Yellow bulb icon on the toolbar or press Alt+Enter, and then select the relevant suggestion.

  3. The Vulnerable Dependencies tab lists all the vulnerable packages detected in your project.

    Problems tool window: Vulnerable Dependencies tab

    To fix a problem, select the vulnerable package in the list, click the Copy safe version to clipboard link on the right-hand pane, and then paste the copied version to your package.json.

    Learn more from Get results and fix problems.

Disable package vulnerability check

By default, package vulnerability check is enabled.

  • To turn it off, open the Settings dialog (Ctrl+Alt+S) , go to Editor | Inspection, and clear the checkbox next to the Vulnerable declared dependency inspection under the Security node.

  • Alternatively, open the Settings dialog (Ctrl+Alt+S) , go to Plugins, locate the Package Checker plugin, and click Disable.

Run and debug scripts

You can launch npm, Yarn, or pnpm scripts from a package.json file in the editor, from a tree of scripts in the dedicated npm tool window, according to a dedicated run configuration, or automatically, as a start-up or a before-launch task.

The results of script execution are displayed in the Run tool window. The tool window shows the npm or Yarn script output, reports the errors occurred, lists the packages or plugins that have not been found, etc. The name of the last executed script is displayed on the title bar of the tool window.

If you launch a script debugging session, PyCharm opens the Debug tool window, where you can step through the script, stop and resume the script execution, examine it when suspended, run JavaScript code snippets in the Debugger Console, and so on.

Run and debug scripts from package.json

PyCharm lets you quickly launch single scripts from package.json files. To run or debug several scripts, use a run configuration or the npm tool window.

  • Open the relevant package.json file in the editor, click the Run button in the gutter next to the script, and select Run <script_name> or Debug <script_name> from the context menu.

    Run/debug a script from package.json
  • You can also run a script by pressing Ctrl+Shift+F10.

The script output is shown in the Run tool window.

If you launch a script in the debug mode, PyCharm opens the Debug tool window, where you can step through the script, stop and resume the script execution, examine it when suspended, run JavaScript code snippets in the Debugger Console, and so on.

Run and debug scripts from the npm tool window

The npm tool window opens when you select a package.json file in the Project tool window or open it in the editor and select Show npm Scripts from the context menu.

As soon as you invoke npm, pnpm, or Yarn, the tool starts building a tree of scripts defined within the scripts property of the package.json file on which it was invoked.

If you have several package.json files in your project, you can build a separate script tree for each of them and run scripts without dropping the previously built trees. Each tree is shown under a separate node.

Open the npm tool window if it is not opened yet

  • Select the required file in the Project tool window or open it in the editor and choose Show npm Scripts from the context menu.

    Open the nmp tool window

    The npm tool window opens showing the scripts tree built according to the selected or opened package.json file.

Build a tree of scripts in an already opened npm tool window

  1. In the npm tool window, click the Add button on the toolbar and choose the required package.json file from the list. By default, PyCharm shows the package.json file in the root of your project.

  2. If you have another package.json file, click Choose package.json and select the package.json file you need in the dialog that opens. PyCharm adds a new node with the path to the chosen package.json file on its title and builds a scripts tree under the new node.

Build another script tree

Re-build a tree

  • Switch to the required node and click Reload Scripts on the toolbar.

Sort the scripts in a tree by their names

  • Click Settings on the toolbar, choose Sort by from the menu, and then choose Name.

    By default, a tree shows the scripts in the order in which they are defined in package.json (option Definition order).

Run one script

  • Double-click the script.

  • Select the script in the tree and press Enter or choose Run <script name> from the context menu.

Debug one script

Run or debug several scripts

  • Use the multiselect mode: hold Shift (for adjacent items) or Ctrl (for non-adjacent items) keys and select the required scripts, then choose Run or Debug from the context menu of the selection.

    Run multiple scripts

Run and debug scripts by a run configuration

When you run or debug scripts from the editor or from the npm tool window, PyCharm automatically creates temporary run configurations. Besides using them, you can create and launch your own npm run configurations.

Create an NPM run/debug configuration

  1. Go to Run | Edit Configurations. Alternatively, select Edit Configurations from the Run widget on the toolbar.

    Open the Edit Configurations dialog
  2. In the Edit Configurations dialog that opens, click the Add button (the Add button) on the toolbar and select npm from the list. The Run/Debug Configuration: npm dialog opens.

  3. Specify the CLI command to execute, the scripts to run (use blank spaces as separators), and the location of the package.json file where these scripts are defined. Optionally, type the command-line arguments for executing the scripts.

  4. Specify the Node.js interpreter to use.

    If you choose the Project alias, PyCharm will automatically use the project default interpreter from the Node interpreter field on the Node.js page . In most cases, PyCharm detects the project default interpreter and fills in the field itself.

    You can also choose another configured local or remote interpreter or click the Browse button and configure a new one.

    Optionally specify the Node.js-specific option parameters and the environment variables to be passed to Node.js.

  5. Specify the package manager to use. If you choose the Project alias, PyCharm will use the default project package manager from the Node.js page. You can also choose the relevant package alias (npm or yarn) or specify an explicit path to a custom installation of a package manager.

  6. If you are using a remote Node.js interpreter in a Docker container, check the port and volume bindings values in the Docker container settings field.

    Check Docker container settings

    Learn more from npm, pnpm, and yarn with Docker and Configuring remote Node .js interpreters.

Learn more about the Command Line Interfaces for the package managers from the npm official website and Yarn official website.

Run and debug your scripts

Run scripts with Run Anything

Run Anything is a quick way to start scripts.

  1. Press Ctrl twice or click the Run Anything button on the Navigation bar. The Run Anything popup opens.

  2. Type npm run or yarn run in the search field. As you type, PyCharm shows the matching scripts. Select the required one from the list and press Enter.

    Run Anything: npm script

    To view the command output in the Run tool window, press Ctrl+Enter, to show the output in the Debug tool window, press Shift+Enter.

    Run an npm command in the Run or Debug tool window from the Run Anything popup

Run scripts automatically on start-up

If you have some scripts that you run on a regular basis, you can add the corresponding run configurations to a list of startup tasks. The tasks will be executed automatically on the project start-up.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Tools | Startup Tasks.

  2. On the Startup Tasks page that opens, click the Add button on the toolbar.

  3. From the list, choose the required npm run configuration. The configuration is added to the list.

    If no applicable configuration is available in the project, click the Add button` and choose Edit Configurations. Then define a configuration with the required settings on the Run/Debug configuration page that opens. When you save the new configuration it is automatically added to the list of startup tasks.

Run scripts as before-launch tasks

  1. Open the Run/debug configurations dialog dialog by choosing Run | Edit Configurations from the main menu, and select the required configuration from the list or create it anew by clicking Add New Configuration and choosing the relevant run configuration type.

  2. In the dialog that opens, click Add in the Before launch area and choose Run npm script from the list.

  3. In the NPM Script dialog that opens, specify the npm run/debug configuration settings.

npm, pnpm, and yarn in Docker

With PyCharm, you can edit package.json, install, update, and remove dependencies of a project in a Docker container just in the same way as you do it locally.

  1. Install the Node.js and Node.js Remote Interpreter plugins on the Settings | Plugins page, tab Marketplace, as described in Installing plugins from JetBrains Marketplace.

  2. Make sure the Docker plugin is enabled in the settings. Press Ctrl+Alt+S to open settings and then select Plugins. Click the Installed tab. In the search field, type Docker. For more information about plugins, refer to Managing plugins.

  3. Download, install, and configure Docker as described in Docker

  4. Configure a Node.js remote interpreter in Docker or via Docker Compose and set it as default in your project. Also make sure the package manager associated with this remote interpreter is set as project default.

  5. Proceed as with local development: edit your package.json, manage you project dependencies, and run or debug scripts.

Last modified: 31 October 2024