How to make IntelliJ IDEA resolve webpack requires out of node_modules directory?

非 Y 不嫁゛ 提交于 2019-11-30 00:45:11

问题


IntelliJ cannot resolve javascript modules called with webpack requires which are not inside the node_modules directory

Imagine this project structure:

`- project
   |- node_modules
   |  `- react
   |     `- addons.js
   |- webpack.config.js
   |- util
   |  `- tool.js
   `- src
      |- components
      |  `- uno.jsx
      `- two.jsx

This is my webpack config

// webpack.config.js
var path = require('path'); 
module.exports = {
  resolve: {
    root: [
      path.resolve('./src'),
      path.resolve('./')
    ]
  }
  ...
}

And this is how I use webpack's require

// two.js
var React = require('react/addons');
var One = require('components/one');
var Tool = require('util/tool');
// dosomething

So this works perfectly within my application, and IntelliJ looks happy with 'react/addons', how to make understand the sources for navigation, code completion and Documentation lookup for 'components/one' and 'util/tool'?

I've tried so far:

  • adding a package.json inside src (npm init)
  • adding src as a Javascript library in Settings / Languages & Frameworks following this https://www.jetbrains.com/idea/help/configuring-javascript-libraries.html
  • mimicking anything below .idea related to node_modules

But no luck so far. Thanks.


回答1:


I think this should work (or so it did in my case anyway).

In IntelliJ:

  1. Open the project
  2. File > Project Structure
  3. On the left hand side, select Modules
  4. From your directory structure, select the folders where your sources are (util and src) and mark them as Resources
  5. Click Apply

You should have code completion and documentation available now.



来源:https://stackoverflow.com/questions/31164331/how-to-make-intellij-idea-resolve-webpack-requires-out-of-node-modules-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!