Phpstorm Javascript



This video is a part of JetBrains PhpStorm Video Tutorials by JetBrains Technical Evangelist Maarten Balliauw. PHPStorm and WebStorm are two IDEs that are developed for PHP and JavaScript, respectively. Depending on the features each provides and the performance during the trial version can help the user determine the IDE they can choose. Key features, differences, and similarities are addressed in the above sections in detail. Add a debugger; line to your JavaScript code. For example: alert ('First.' ); debugger; // Trigger a breakpoint. Alert ('Second.' ); If you want to debug the Javascript in PHPStorm (and not the Browser), you can do so by using the remote javascript debugging option.

With PhpStorm, you can develop modern web, mobile, and desktop applications with JavaScript and Node.js.

PhpStorm also supports React, Angular, Vue.js, and other frameworks and provides tight integration with various tools for web development.

JavaScript-aware coding assistance includes completion for keywords, labels, variables, parameters, and functions, error and syntax highlighting, formatting, numerous code inspections and quick-fixes, as well as common and JavaScript-specific refactoring. PhpStorm also integrates with JavaScript linters and the Flow type checker.

With PhpStorm built-in debugger, you can debug both your client-side and server-side code and even run JavaScript code snippets in the interactive debugger console. Learn more from Debug JavaScript in Chrome.

PhpStorm integrates with Jest, Karma, Protractor, Cucumber, and Mocha testing frameworks. PhpStorm supports running and debugging tests as well as navigation between tests and subjects or between a failed test and the fragment of code that caused the problem.

On this page you will find a short Getting Started Guide that will walk you step by step from creating a web application to debugging and testing it.

Before you start

Make sure the JavaScript and TypeScript bundled plugin is enabled on the Settings/Preferences | Plugins page, see Managing plugins for details.

Creating a new application

Phpstorm Javascript Formatting

  1. Click Create New Project on the Welcome screen or select File | New | Project from the main menu. The Create New Project Dialog opens.

  2. In the left-hand pane, choose Empty Project.

  3. In the right-hand pane, specify the path to the folder where the project-related files will be stored. Click Create.

Starting with an existing JavaScript application

If you are going to continue developing an existing JavaScript application, open it in PhpStorm, choose the JavaScript version to use, and configure the libraries in it. Optionally download the required npm dependencies.

Open the application sources that are already on your machine

  • Click Open on the Welcome screen or select File | Open Directory from the main menu. In the dialog that opens, select the folder where your sources are stored.

Check out the application sources from your version control

  1. Click Get from VCS on the Welcome screen or select VCS | Get from Version Control from the main menu.

  2. In the invoked dialog, select your version control system from the list and specify the repository to check out the application sources from.

Choosing the JavaScript language version

To get reliable and efficient coding assistance, you need to specify the language version that will be used in all JavaScript files of your project by default.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Languages and Frameworks | JavaScript. The JavaScript page opens.

  2. From the list, choose one of the supported JavaScript language versions:

    • ECMAScript 6+: This version adds support for the features introduced in ECMAScript 2015-2020 and for JSX syntax as well as some current proposals to the standard.

    • Flow: This version adds support for the Flow syntax.

Using multiple JavaScript versions

If you are working on an application that uses both ECMAScript 5.1 and a newer version of ECMAScript, the easiest way is to choose the highest language version for the whole project from the list on the JavaScript page.

  1. On the JavaScript page, click next to the JavaScript language version list. The JavaScript Language Versions dialog opens.

  2. Click and in the dialog that opens select the folder where you need a custom language version. PhpStorm brings you back to the JavaScript Language Versions dialog where the selected folder is shown in the Path field.

  3. From the Language list, choose the language version for the files in the selected folder. In all the other JavaScript files in the project PhpStorm will use the version chosen on the JavaScript page.

JSX syntax in JavaScript code

If you are using JSX syntax in your JavaScript code, enable ECMAScript 6+.

  • In the Settings/Preferences dialog Ctrl+Alt+S, go to Languages and Frameworks | JavaScript, and select ECMAScript 6+ from the JavaScript language version list.

  • To be warned when JSX syntax is used by mistake, open the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | Inspections, and enable the React JSX syntax inspection under JavaScript and TypeScript | General node. Use the search field to locate the inspection.

    Optionally, configure the inspection severity and scope. Learn more from Code inspections.

Downloading project dependencies

If your application uses some tools, libraries, or frameworks, download the required packages. To manage your project dependencies, you can use npm, Yarn 1, or Yarn 2, see npm and Yarn for details.

Install a package in an empty project

  • In the embedded Terminal (Alt+F12), type:

    npm install <package name>.

If you already have a package.json file in your project

  • Right-click the package.json file in the editor or in the Project tool window and choose Run 'npm install' from the context menu.

  • Alternatively, run npm install in the TerminalAlt+F12.

Before you start, make sure you have Node.js on your computer.

Configuring code completion for project dependencies

To provide code completion for project dependencies, PhpStorm automatically creates a node_modules library. In PhpStorm, a library is a file or a set of files whose functions and methods are added to PhpStorm's internal knowledge in addition to the functions and methods that PhpStorm retrieves from the project code that you edit. See Configure JavaScript libraries and Code completion for details and examples.

Viewing parameter hints

Parameter hints show the names of parameters in methods and functions to make your code easier to read. By default, parameter hints are shown only for values that are literals or function expressions but not for named objects.

Configure parameter hints

  1. Open the Settings/Preferences dialog Ctrl+Alt+S and go to Editor | Inlay Hints | JavaScript.

  2. Select Parameter hints from the list, make sure the Show parameter hints checkbox is selected, and then specify the context where you want parameter hints shown.

  3. For some methods and functions, PhpStorm does not show parameter hints in any context. Click Exclude list... to view these methods and functions, possibly enable parameter hints for them, or add new items to the list.

To hide parameter hints for all types of values, clear the Show parameter name hints checkbox. Learn more from Parameter info.

Auto-import in JavaScript

PhpStorm can generate import statements for modules, classes, components, and any other symbols that are exported. If you are using ES6 in your project, PhpStorm can add missing import statements on the fly, when you complete ES6 symbols.

For earlier JavaScript versions or when auto-import on completion is disabled, PhpStorm marks the symbol as unresolved and shows a tooltip with the suggested quick-fix:

Alternatively, press Alt+Enter:

Phpstorm

If there's more than one possible source of import, PhpStorm shows a list of suggestions:

Learn more from Choosing the JavaScript language version.

PhpStorm can also generate import statements for symbols defined in the project’s dependencies. Auto-import works for symbols from packages that contain TypeScript definition files (like in moment or redux) or sources written as ES modules.

Configure auto-import

  1. To automatically add ES6 import statements on code completion, open the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | General | Auto Import, and select the Add ES6 imports on code completion checkbox in the TypeScript/JavaScript area.

  2. To configure the appearance of import statements, open the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | Code Style | JavaScript, and use the controls in the Imports tab, see Imports tab for details.

Configuring syntax highlighting

You can configure JavaScript-aware syntax highlighting according to your preferences and habits.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Editor | Color Scheme | JavaScript.

  2. Select the color scheme, accept the highlighting settings inherited from defaults or customize them as described in Configuring colors and fonts.

Running JavaScript in browser

Phpstorm Javascript Type Hinting

  1. In the editor, open the HTML file with the JavaScript reference. This HTML file does not necessarily have to be the one that implements the starting page of the application.

  2. Do one of the following:
    • Choose View | Open in Browser from the main menu or press Alt+F2. Then select the desired browser from the list.

    • Hover your mouse pointer over the code to show the browser icons bar: . Click the icon that indicates the desired browser.

Debugging JavaScript

PhpStorm provides a built-in debugger for your client-side JavaScript code that works with Chrome.

You can also debug your client-side JavaScript in Firefox, version 36 and later. However it is strongly recommended that you use Chrome or any other browser of the Chrome family. With PhpStorm, you can debug JavaScript applications running on the built-in server, on an external server, or on a remote server. For details, see Debug JavaScript in Chrome and Debug JavaScript in Firefox.

Features

Those of us who work a lot with JavaScript often face a small issue with Autocompletion which turns to be not a small issue but a big mess.
By default, completion works for all JavaScript files located under your project root. This may lead to a long and almost unusable completion list. Even if you use just a few JavaScript libraries in your project like jQuery, Ext JS and MooFX, you can get up to 4,000 autocompletion hints for Document object coming from all those libraries! And even when you type e.g. “set”, you’ll still end up with about 200 functions starting with “set” . Not a lot of help, is it? 😉

Starting from WebStorm/PhpStorm 2.0 we include a new feature that deals with this very issue. Now, in Settings | Project settings you can specify the JavaScript Libraries that should be used for each file/folder or for the whole project — in other words, their Usage Scope. In the example below we have two libraries defined for our project.

Let’s say we have jCrop and jQuery in our project.

example1.js uses only jCrop because we specifed at project-level that we use this library for the whole project (and thus we may need autocompletion for jCrop in any file).
example2.html will use jQuery and jCrop so autocompletion will be available for both libraries.

These settings will be stored in the project file, so you can share it via your VCS to other members of your project, so that they will be ready-to-go just by checking out project files.

How to set up JavaScript library in WebStorm/PhpStorm

Phpstorm Xdebug Javascript

Open IDE Settings | Project Settings. Select JavaScript | Libraries | Add… and add the files or folders you need.

There is an additional benefit — you will be able to create global JavaScript libraries shared between different projects.

Phpstorm Javascript Minify

“Download Library” Intention

There is an even easier way to create a library containing just one .js file, if this file is located on the Internet and can be accessed over HTTP. For example, if a JavaScript library is available online. Here is a small example of an HTML file containing a link to jQuery on jquery.com:


Assuming that jQuery library hasn’t been defined yet, you can move the caret to “http://code.jquery.com/…” string, press Alt+Enter/Option+Enter and choose “Download Library” from the list of available intention actions:

It may take a while before the library is downloaded but eventually you will see the message “Successfully downloaded http://code.jquery.com/jquery-1.4.3.js”. The downloaded file will be stored in your local file system for code completion and navigation to work.

Download the latest WebStorm/PhpStorm EAP and try this.