import-js / eslint-plugin-import

ESLint plugin with rules that help validate proper imports.
MIT License
5.55k stars 1.57k forks source link

Configure node resolver #506

Closed misteral closed 7 years ago

misteral commented 8 years ago

Please help. I am stuck with node resolver configuration.

Project folder structure as the same: https://github.com/szwacz/electron-boilerplate.

In simple words: app folder include node_project folder and package.json file and static js files src folder include dinamic js files with import modules.

Part of file example:

import $ from 'jquery';
import Guid from 'guid';
import jCanvas from 'jcanvas';

jCanvas($, window);

class Comment {
    constructor($canvas, x = 0, y = 0) {
        this.$canvas = $canvas;
... more

I get eslint errors with this file in src folder:

  1:1   error  'jquery' should be listed in the project's dependencies. Run 'npm i -S jquery' to add it    import/no-extraneous-dependencies
  1:15  error  Unable to resolve path to module 'jquery'                                                   import/no-unresolved
  2:1   error  'guid' should be listed in the project's dependencies. Run 'npm i -S guid' to add it        import/no-extraneous-dependencies
  2:18  error  Unable to resolve path to module 'guid'                                                     import/no-unresolved
  3:1   error  'jcanvas' should be listed in the project's dependencies. Run 'npm i -S jcanvas' to add it  import/no-extraneous-dependencies
  3:21  error  Unable to resolve path to module 'jcanvas'

How I can resolve import node packages like 'jquery' in src folder?

I try use this eslint config, but it not work:

module.exports = {
    "settings": {
        "import/core-modules": [ 'electron' ],
        "import/resolver:": {
            "node": {
                "paths": [
                    "./app"
                ],
                "moduleDirectory": [
                    "node_modules",
                    "./app/node_modules"
                ]
            },
        },
    },
    "env": {
        "browser": true,
        "es6": true,
        "node": true,
    },
    "extends": "airbnb",
    "parserOptions": {
        "sourceType": "module"
    },
    "rules": {
        "indent": [
            "error",
            4
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "always"
        ]
    }
};
benmosher commented 8 years ago

Have you npm installed?

misteral commented 8 years ago

@benmosher yes, does not have the error if I copy file into app folder.

benmosher commented 8 years ago

Where is your eslintrc?

misteral commented 8 years ago

@benmosher eslintrc is above.

benmosher commented 8 years ago

Well, for one thing, nested package.jsons aren't currently supported for no-extraneous-dependencies, see #458 for tracking/details.

As for resolving modules in ./app/node_modules, I think you might need

            "node": {
                "paths": [
                    "./app",
                    "./app/node_modules"
                ],
                "moduleDirectory": [
                    "node_modules"
                ]
            },
        },

as your import/resolver config, but I'm not sure.

misteral commented 8 years ago

@benmosher thanks for answer, but I got same errors:

$ eslint src/desky/Comment.js
The react/require-extension rule is deprecated. Please use the import/extensions rule from eslint-plugin-import instead.

/data/desky-for-gitlab/src/desky/Comment.js
  1:1   error  'jquery' should be listed in the project's dependencies. Run 'npm i -S jquery' to add it    import/no-extraneous-dependencies
  1:15  error  Unable to resolve path to module 'jquery'                                                   import/no-unresolved
  2:1   error  'guid' should be listed in the project's dependencies. Run 'npm i -S guid' to add it        import/no-extraneous-dependencies
  2:18  error  Unable to resolve path to module 'guid'                                                     import/no-unresolved
  3:1   error  'jcanvas' should be listed in the project's dependencies. Run 'npm i -S jcanvas' to add it  import/no-extraneous-dependencies
  3:21  error  Unable to resolve path to module 'jcanvas'                                                  import/no-unresolved

current eslintrc.js config:

module.exports = {
    "settings": {
        "import/core-modules": [ 'electron' ],
        "import/resolver:": {
            "node": {
                "paths": [
                    "./app",
                    "./app/node_modules"
                ],
                "moduleDirectory": [
                    "node_modules"
                ]
            },
        },
    },
    "env": {
        "browser": true,
        "es6": true,
        "node": true,
    },
    "extends": "airbnb",
    "parserOptions": {
        "sourceType": "module"
    },
    "rules": {
        "indent": [
            "error",
            4
        ],
        "linebreak-style": [
            "error",
            "unix"
        ],
        "quotes": [
            "error",
            "single"
        ],
        "semi": [
            "error",
            "always"
        ]
    }
};
toomuchdesign commented 7 years ago

Any update about this issue?

misteral commented 7 years ago

@toomuchdesign thank you. I think we can close issue.

binarymist commented 6 years ago

It's still an issue.

ljharb commented 6 years ago

@binarymist then please file a new issue with repro steps