Using External tools: ESLint autofix, React Native and Prettier

Did you know that you can easily run many command line tools from WebStorm with just a shortcut? Well, now you do! No need to go to the Terminal, thanks to the feature called External tools.Let’s see how we can use External tools to run ESLint autofix and React Native.

Let’s see how we can use External tools to run ESLint autofix and React Native. The same way you can configure any other command-line tool.

ESLint

Update: in WebStorm 2016.3 you can use ESLint autofix in the editor without configuring External tools  – hit Alt-Enter on the ESLint warning and select Fix current file with ESLint .

ESLint is a popular linter for JavaScript that has a great number of built-in rules and can also be extended with plugins. WebStorm’s integration with ESLint allows you to see warnings and errors reported by ESLint right in the editor, as you type. (To enable that, go to Preferences | Languages & Frameworks | JavaScript | Code quality tools | ESLint.) Unfortunately, WebStorm does not yet support ESLint autofixing functionality. But we can enable that via External tools!

Assuming that you already have ESLint installed (globally or locally) and configured the rules in your project, that should be rather easy: go to Preferences | Tools | External tools and click + to add a new tool. Let’s name it ESLint Fix.

If ESLint is installed globally, we would normally run this in the command line from the project root to apply fixes (where app.js is the file we want to fix):
eslint app.js --fix

So in the External tools configuration, we need to set Program and Parameters accordingly, but using specific WebStorm macros that would add the filename of the opened file and project root path.

Please note that on Windows you need to specify the full path to the ESLint executable. If ESLint installed globally, run npm config get prefix to get the location of your global node modules and add to it node_modules\eslint.

run-eslint-fix

If on the other hand you have ESLint installed locally, replace the program with ./node_modules/.bin/eslint (on OS X) or .\node_modules\.bin\eslint.cmd (on Windows).

When passing paths, we recommend quoting them.

You can also run eslint --fix for the whole project (but then you would probably need .eslintignore file to exclude node_modules). To do that, tweak the parameters like this: --fix .

Once it’s configured, you can run the tool from menu Tools > External tools. Or, you can search for it by name from the Find action dialog (Cmd-Shift-A on OS X or Ctrl+Shift+A on Windows and Linux):

find-action-eslint

Alternatively, you can bind a keyboard shortcut to run this external tool. To do this, navigate to Preferences | Keymap, and then find ESLint Fix (or whatever you named your external tool configuration) under External Tools. Now you can bind a key to run ESLint fix just like you would with any of the built-in features.

React Native

Update: WebStorm 2016.3 adds support for React Native: you can now run and debug React Native apps in the IDE using a new React Native Run/Debug configurations (you can find out more about it in this blog post).

With React Native you can build modern mobile apps using JavaScript. WebStorm offers advanced support for React and JSX, and can provide you with core coding assistance for React Native apps. React Native comes with the CLI (and we assume you have already installed it and configured your development environment), which allows you to build and run apps for iOS or Android.We can configure External tools to run

We can configure External tools to run react-native run-ios and react-native run-android, the two most used commands. That’s very easy, here’s an example for run-ios on macOS:

run-react-native-ios

Please note that on Windows you need to specify the full path to the executable!

Now type React Native in the Find action dialog to see and run the configurations. That’s it!

find-action-react-native

Using prompt

Using $Prompt$ macro in the Parameters input, you can pass some additional options to the tools, for example, a port number.

Prettier

Prettier allows you to format your code. Here’s how you can configure it using External tools.

On macOS, if Prettier is installed globally, the path to the program should be: prettier; if installed locally – ./node_modules/.bin/prettier.

On Windows run npm config get prefix to get the location of your global node modules and add to it node_modules\prettier. And for local installation on Windows – .\node_modules\.bin\prettier.cmd.

prettier-external-tools

Your WebStorm Team

About Ekaterina Prigara

Ekaterina Prigara is WebStorm product marketing manager at JetBrains. She's passionate about new technologies, UX and coffee.
This entry was posted in Cool Feature and tagged , . Bookmark the permalink.

36 Responses to Using External tools: ESLint autofix, React Native and Prettier

  1. Steve Daly says:

    Thanks for the article. I get the following error when I try to run eslint –fix.

    ESLint is running within WebStorm and it works from the command line within the project.

    /usr/local/bin/eslint –fix server/index.js
    Cannot find module ‘babel-eslint’
    Error: Cannot find module ‘babel-eslint’
    at Object.ModuleResolver.resolve (/usr/local/lib/node_modules/eslint/lib/util/module-resolver.js:75:19)
    at Object.load (/usr/local/lib/node_modules/eslint/lib/config/config-file.js:518:42)
    at loadConfig (/usr/local/lib/node_modules/eslint/lib/config.js:64:33)
    at getLocalConfig (/usr/local/lib/node_modules/eslint/lib/config.js:136:23)
    at Config.getConfig (/usr/local/lib/node_modules/eslint/lib/config.js:263:22)
    at processText (/usr/local/lib/node_modules/eslint/lib/cli-engine.js:231:27)
    at processFile (/usr/local/lib/node_modules/eslint/lib/cli-engine.js:306:18)
    at executeOnFile (/usr/local/lib/node_modules/eslint/lib/cli-engine.js:679:23)
    at /usr/local/lib/node_modules/eslint/lib/cli-engine.js:716:13
    at Array.forEach (native)

    Process finished with exit code 1

    • Ekaterina Prigara says:

      Apparently you don’t have babel-eslint module installed globally (and you’re running globally installed eslint). Please try to either install it globally, or, if you have it and eslint locally, specify the local path to eslint in the External tool configuration.

    • Ekaterina Prigara says:

      Thanks for the link! And thank your reports. Sorry that we are not always able to address all the issues, especially those related to the performance, as quick as we wish we could.

  2. rethus says:

    I try to get react-native run on PHPStorm which is almost simlar to Webstorm.
    I have add a Watcher for babel, but if it is triggerd, I got the following Error:

    First I’ve run this commands successfully:

    npm install babel-preset-react --save
    npm install babel-preset-es2015 --save

    , but if I change something in index.android.js the watcher is triggerd and throw this message:


    import React, { Component } from 'react';
    ^^^^^^
    SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:76:16)
    at Module._compile (module.js:528:28)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

    Process finished with exit code 1

    Did you have any Idea, where the issue is?

    • Ekaterina Prigara says:

      Sorry, not sure how it’s related to this post. But have you added a .babelrc file to your project?

      • rethus says:

        Ok, sorry for this offtopic, But I’m new to react-native, the irc-channel at freenode.net is dead and I don’t know where to get the answer (specially for phpstorm and react-native).

        Maybe you could recommend me an IRC?

        BTW: Yes, there is an .babelrc, inside is this :

        {
        “presets”: [“es2015″,”react-native”]
        }

        Maybe you could make a short blog-theme with first steps with phpstorm and react-native. This would be awesome.

        • Ekaterina Prigara says:

          I would recommend to use react-native init to generate a project template. It has all the required configuration files and you can just start writing the app.

  3. foo says:

    every time i run this command i got env: node: No such file or directory
    but when i copy the command and paste it in terminal it works !

    • Ekaterina Prigara says:

      Can you please provide a bit more details on what program you run and how. Thanks!

    • Fabio Masini says:

      You are probably using node from your Ubuntu/Debian distro which use “nodejs” as command for execute node. Try something like ln -s /usr/bin/nodejs /usr/bin/node or, better, use nvm.

      • Alex says:

        I’m using nvm but still have the same problem

        • Ekaterina Prigara says:

          Can you run node via the OS terminal? What OS do you use?

          • Alex says:

            I’m using Ubuntu 16.04, via terminal it worked well. I added ln -s /usr/bin/nodejs /usr bin node and resolved issue. But I thought that nvmshould do this by default.
            By the way thank you for fast response.

  4. Pierre says:

    Hi,

    To make react-native work in windows the Program command had to point to the react-native.cmd.

    For me it was global:
    c:\Users\{user}\AppData\Roaming\npm\react-native.cmd

    In a Terminal window, react-native run-android works fine.

    Hope this helps

    Pierre

  5. Phillip says:

    In Windows, with a local npm install, I do not see an eslint.cmd in the bin folder (or anywhere). Globally it is there. Can you confirm local install?

  6. Phillip says:

    Right. Globally it is there. What about locally?

    • Ekaterina Prigara says:

      Sorry, I’ve meant npm install eslint in the previous comment. The screenshot is captured in the project’s node_moduels folder after local installation.

  7. Phillip says:

    I see. The folder structure globally is different. I was looking in the eslint folder not at the top level .bin. (I don’t understand Windows search. When I search for the name ‘eslint’ only the one in global comes up. Frustrating.)

    Thanks for your help!

  8. Forrest Akin says:

    I just want to share my painful experience trying to get WebStorm playing right with ESLint and what finally worked for me. My eslint config was originally split across multiple files, not necessarily in the same directory. I knew that whatever eslint plugins/parsers I used needed to be installed in the same directory as the eslint package I was using, so I made sure that everything was installed (my plugins are react and jsx-a11y, and parser babel-eslint). I tried installing everything locally and globally, but nothing was working. At this point, I hadn’t considered if the location of my eslintrc was the culprit because I was assuming that as long as all of my plugins were locally accessible in relationship to it, it wouldn’t matter. It turns out that when I put the eslintrc in my project directory, as well as all of my rule files it was extending and pointed to a local eslint package, it worked! Maybe eventually, WebStorm can add some troubleshooting instructions for ESLint integration to ease the pain for people experiencing similar issues as I did; I wasn’t able to find anything helpful online (even after several days of googling).

    • Ekaterina Prigara says:

      Hi Forrest! Thanks for sharing your experience. Sorry it didn’t well so smoothly. We’ll try to reproduce your setup and fix the way WebStorm looks for ESLint plugins.

  9. Jose Manuel Garcia Romero says:

    Hi,

    I’m trying to use Prettier as an external tool in IntelliJ Idea 2016.3.5 as it is described in this post but even though Prettier is being executed it does not format the file, it does nothing basically, the output is always:

    C:\PGM\IntelliJIdeaProjects\DGEMPAGORA\agora-parent\agora-web\node_modules\.bin\prettier.cmd –write app\components\MemberBasicDetails.js

    Process finished with exit code 0

    Do I need to add some configuration file for prettier or anything else not included in this post?

    Thanks!

    • Ekaterina Prigara says:

      Please make sure that Synchronise files after execution checkbox is checked.
      Please also try to run the same command in the terminal and see whether the file changes.

      • Jose Manuel Garcia Romero says:

        Synchronize files after execution is checked but still the editor content does not change.

        Running the command in the terminal works fine, the output is formatted, but the content of the file does not change either.

        • Ekaterina Prigara says:

          –write option should update the file in-place and should not output the formatted code. The command should return something like:
          app\components\MemberBasicDetails.js 40ms
          Process finished with exit code 0
          Please try updating the version of prettier you’re using.

          • Jose Manuel Garcia Romero says:

            Hi Ekaterina, it’s ok now, the problem was the working directory setting in the external tool dialog, the path to the file was not correct, it was missing a suffix

            Thanks for your help!

  10. Elad says:

    I followed the instruction for prettier, and it works well, thanks :)
    However, I get an exception immediately afterwards. It doesn’t happen when I run the command from the CLI.
    Btw, everything is working as expected, it’s just annoying.

    /Users/elad/.nvm/versions/node/v7.5.0/bin/prettier –trailing-comma es5 –tab-width 4 –write javascripts/angular/directives/authenticity/tabIndex.js
    javascripts/angular/directives/authenticity/tabIndex.js 53ms
    java.io.IOException: Cannot run program “bunyan”: error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:528)
    at cyue.idea.plugins.bunyan.PipeProcessing.convert(PipeProcessing.java:48)
    at cyue.idea.plugins.bunyan.BunyanFilter.applyFilter(BunyanFilter.java:111)
    at com.intellij.execution.filters.CompositeInputFilter$1.applyFilter(CompositeInputFilter.java:70)
    at com.intellij.execution.filters.CompositeInputFilter.applyFilter(CompositeInputFilter.java:48)
    at com.intellij.execution.impl.ConsoleViewImpl.print(ConsoleViewImpl.java:609)
    at com.intellij.execution.impl.ConsoleViewRunningState$1.onTextAvailable(ConsoleViewRunningState.java:43)
    at sun.reflect.GeneratedMethodAccessor54.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.execution.process.ProcessHandler$4.invoke(ProcessHandler.java:226)
    at com.sun.proxy.$Proxy15.onTextAvailable(Unknown Source)
    at com.intellij.execution.process.ProcessHandler.notifyTextAvailable(ProcessHandler.java:200)
    at com.intellij.execution.process.ColoredProcessHandler.textAvailable(ColoredProcessHandler.java:96)
    at com.intellij.execution.process.ColoredProcessHandler.coloredTextAvailable(ColoredProcessHandler.java:71)
    at com.intellij.execution.process.AnsiEscapeDecoder.processTextChunk(AnsiEscapeDecoder.java:179)
    at com.intellij.execution.process.AnsiEscapeDecoder.escapeText(AnsiEscapeDecoder.java:76)
    at com.intellij.execution.process.ColoredProcessHandler.notifyTextAvailable(ColoredProcessHandler.java:60)
    at com.intellij.execution.process.BaseOSProcessHandler$SimpleOutputReader.onTextAvailable(BaseOSProcessHandler.java:290)
    at com.intellij.util.io.BaseOutputReader.sendText(BaseOutputReader.java:202)
    at com.intellij.util.io.BaseOutputReader.processInput(BaseOutputReader.java:186)
    at com.intellij.util.io.BaseOutputReader.readAvailableNonBlocking(BaseOutputReader.java:105)
    at com.intellij.util.io.BaseDataReader.readAvailable(BaseDataReader.java:82)
    at com.intellij.util.io.BaseDataReader.doRun(BaseDataReader.java:160)
    at com.intellij.util.io.BaseDataReader$1.run(BaseDataReader.java:61)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.UNIXProcess.forkAndExec(Native Method)
    at java.lang.UNIXProcess.(UNIXProcess.java:247)
    at java.lang.ProcessImpl.start(ProcessImpl.java:134)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
    … 31 more

    Process finished with exit code 0

  11. Mike Erickson says:

    Hey Gang

    I am trying to get Prettier to work with file watcher (so it fixes on save) but I cant seem to figure out the settings. It works fine when invoked manually?

    Suggestions?

  12. felixl says:

    Sharing my ESLint Fix configuration for Windows 10, using local install of eslint.
    ESLint is installed to /node_modules/eslint/bin/eslint.js
    You have to use ‘node’ to interpret and execute the eslint.js file, so the Edit Tool dialog’s “Tool Settings” will look like this:

    Program: node
    Parameters: .\node_modules\eslint\bin\eslint.js –fix “$FilePathRelativeToProjectRoot$
    Working directory: $ProjectFileDir$

  13. Peter says:

    “Update: in WebStorm 2016.3 you can use ESLint autofix in the editor without configuring External tools – hit Alt-Enter on the ESLint warning and select Fix current file with ESLint .”

    Was this removed? I can autofix individual errors/warnings using Alt+Enter, but I’ve searched extensively and cannot find any option for fixing an entire file.

    • Ekaterina Prigara says:

      There should be an option to fix the current file with ESLint, we haven’t removed that. And there has never been an option to fix only one problem – ESLint doesn’t support that.
      Can you please share a screenshot? What ESLint and WebStorm versions do you use?

Leave a Reply

Your email address will not be published. Required fields are marked *