25

In WebStorm / PhpStorm / IDEA, there are quite a lot of code intelligence feature for JavaScript built in. However, when developing a Node.js application, including everything from node_modules complicates matters with things like "Navigate to file" (I don't want the IDE to offer me tens of package.jsons from node_modules folder, for example). But excluding the node_modules folder entirely has its own problems - there is no code completion etc.

So what's the "right" way to approach this in WebStorm? Should I include only some of the files from node_modules? Or exclude everything from there and somehow use the concept of project or global libraries?

Thanks.

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240

4 Answers4

23

To do this in the UI (tested from PyCharm 2.7.3, result shown in PhpStorm 6.0.3):

Add JavaScript Libraries

Add JavaScript Libraries

File → Settings
(Project Settings) → JavaScript → Libraries

[Add...] →

Name: node_modules
Visiblilty: [x] Current project
[Attach...]
{select project's node_modules folder}

{repeat for bower}

Exclude Folder from Project
Exclude Folder From Project

(Project Settings) → Project Structure
Right-click on folder → [Excluded]

To do this from project's .iml file:
Changing the project's .iml to the following:

<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <excludeFolder url="file://$MODULE_DIR$/bower_components" />
      <excludeFolder url="file://$MODULE_DIR$/node_modules" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="node_modules" level="application" />
    <orderEntry type="library" name="bower_components" level="application" />
  </component>
</module>

Caused the editor to exclude node_modules and bower_components, but make them available for completion and library search

Tested in PyCharm, IntelliJ, and WebStorm.

Jake Berger
  • 5,237
  • 1
  • 28
  • 22
fncomp
  • 6,040
  • 3
  • 33
  • 42
4

This has been fixed in WebStorm 7, see http://youtrack.jetbrains.com/issue/WEB-1927.

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
  • Note that after testing this out in WebStorm 7 EAP, I found that this did not work. I filed [a bug report](http://youtrack.jetbrains.com/issue/WEB-8926) and JetBrains says that it will be working with WebStorm 7 EAP build 130.1710. – Jeff Whelpley Aug 27 '13 at 02:17
  • Not fixed in my copy as well. – Shane Feb 14 '14 at 21:41
2

Unfortunately there is no easy "right" way. You can exclude all node_modules directories that are located inside top level node_modules directory. It will make completion better, but not ideal.
Further discussion is here: http://devnet.jetbrains.net/message/5468926 .

Sergey.Simonchik
  • 1,031
  • 8
  • 9
2

You can download a plugin for Node.js integration: http://plugins.intellij.net/plugin/?webide&pluginId=6098

DanMan
  • 11,323
  • 4
  • 40
  • 61
  • This is the answer. Check the event log and it will show this message: `Node.js project detected: To separate your sources from the dependencies, create "Node.js Dependencies" JavaScript library from node_modules folder (show balloon)`. – vaughan Oct 17 '13 at 13:06
  • If you don't have that event still, its manually done like this: open preferences->Project Settings->JavaScript->Libraries. Check "Node.js Globals". Then highlight Node.js Globals, and click "Configure". In the window that opens, click Project Settings->Libraries. Click + at the top of the middle section. Select JavaScript from the drop down. Browse to the node_modules directory and select it. Then the "choose roots" window opens. Select only node_modules. If your node_modules dir contains modules already, you will have to manually deselect all of them. Click ok until all windows are closed. – Anthony Hildoer Feb 06 '14 at 17:04