Create-react-app: Disable 'eslint' in all (nested) 'node_modules'.

Created on 3 Apr 2018  Β·  12Comments  Β·  Source: facebook/create-react-app

environment

OS - macOS: HighSierra - 10.13.3 (17D102)
Node - v8.7.0
Npm - v5.6.0
create-react-app - v1.4.1
react-scripts - v.1.1.2

Steps to reproduce:

1)

create-react-app test_app
cd test_app/src
mkdir x && cd "$_"
npm init -y
npm install moment
echo "import moment from 'moment'" > y.js

2) import y.js file from src/App.js. Like this

import React, { Component } from 'react';
import logo from './logo.svg';

import './x/y';

3)

npm start

You should see 'eslint' errors like this

./src/x/node_modules/moment/moment.js
 Line 5:  'define' is not defined  no-undef
 Line 5:  'define' is not defined  no-undef

Search for the keywords to learn more about each error.

When You add this line

/* eslint-disable */

to the top of './src/x/node_modules/moment/moment.js' file, the issue will be solved.

Thank you.

question stale

Most helpful comment

I'll try to explain more detailed and in another way.

When you create an app with CRA, you get a structure like this.

β”œβ”€β”€ appName
β”‚   β”œβ”€β”€  node_modules
β”‚   β”‚   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  public
β”‚   β”‚   β”œβ”€β”€  index.html
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  src
β”‚   β”‚   β”œβ”€β”€  App.js
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ ...

So, let’s imagine that during development I have a structure like this one.

β”œβ”€β”€ appName
β”‚   β”œβ”€β”€  node_modules
β”‚   β”‚   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  public
β”‚   β”‚   β”œβ”€β”€  index.html
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  src
β”‚   β”‚   β”œβ”€β”€   core
β”‚   β”‚   |   β”œβ”€β”€  atoms
β”‚   β”‚   |   β”œβ”€β”€  molecules
β”‚   β”‚   |   β”œβ”€β”€  organisms
β”‚   β”‚   |   β”œβ”€β”€  ...
β”‚   β”‚   |   β”œβ”€β”€  package.json
β”‚   β”‚   |   β”œβ”€β”€  node_modules
β”‚   β”‚   β”œβ”€β”€  App.js
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ ...

In this case, I will get many errors, because your eslint will start checking sources under core/node_modules. (We can reproduce this, by trying steps mentioned in the first message of this discussion).

My question is still the same. Is it okay, that eslint checks sources under **/node_modules?

In my subjective opinion the way by which this node_modules appears under /src has secondary importance. It can be git submodule or something else. And I can’t find a situation, when disabling of eslint in all node_modules can be a bad thing.

Can’t we ever have some folder in which exists package.json and node_modules under src in the app created by CRA?

All 12 comments

You shouldn't be nesting apps (and node_modules) in this way. The 2.0 alpha version has support for monorepos which are a more appropriate way to combine multiple projects. You can find details about installing it here: https://github.com/facebook/create-react-app/issues/3815

Judging by the report @SurenAt93 didn't want to create a monorepo. He just wanted to install moment.

The mistake was installing moment inside of src. Instead it should have been installed at the top level (at project root).

Hmm, the nesting of the install might've been intentional (note the "nested" in the title), but I'm not sure if this is a use-case we even support (especially now that we compile node_modules in v2).
I'm not sure when installing node_modules at not top-level would be handy. I imagine if this is a hard requirement they're trying to emulate something along the lines of a monorepo.

More detailed about my case.

I have an app, which has a submodule, something like this

  • appName // created by CRA

    • public

    • src



      • core





        • atoms



        • molecules



          ….



          ….






The core folder represents submodule, which has own package.json and node_modules.

@iansu In general I understood your point of view and thank you for suggestion. I can organize this structure by monorepos, e.g by Lerna, which will be supported in CRA v.2. But in my opinion, in any case eslint shouldn't check anything in node_modules, whether it is created by monorepos structure or by structure like mine. So, I was wondering if you could explain eslint work in this case: is it correct to check source in node_modules?

Thanks for the context -- it really helps.

Some users will use node_modules for app code as a means of performing absolute file resolving, so I'm not sure we can just exclude this from linting/etc.
I feel like your use-case is the less-common occurring one, so unfortunately we probably wont favor it -- our best advice would be to structure your application like a monorepo, where each package under packages/ may be a git submodule.

@Timer Thank you very much for your response, but sorry - I don’t agree with you.

I am trying to rewrite about 20 react SPAs to CRA. And first problem which I faced was aliases. And I did some changes in structures for solving problems related with absolute path resolving. I have created some exit points. E.g in index.js of this very core folder I imported and exported all from atoms, molecules and organisms. And the same is in index.js files in atoms/, molecules/ and organism/. And also added NODE_PATH = src line in .env file. I want to say, that there are some ways to avoid absolute path resolving, but making artificial node_modules for solving this problem, for me is not acceptable. So, if it is possible, please revise your opinion about this problem.

Your use case sounds like it would be best served by a monorepo.

@gaearon I agree with @SurenAt93. I faced this issue some times ago and the only way was ejecting and adding custom eslint config.
So, the question is it is correct when eslint checks node_modules .

The main problem is not whether to use monorepos or not, but in incorrect exclude/include values in eslint config. So, in my opinion eslint shouldn't check anything inside node_modules in any case.

I'll reopen this because it's worth hearing more about exactly what you're trying to achieve.

To me, it sounds like what you guys are doing isn't a proper use-case, but I may be wrong.

@iansu what're your thoughts?

I'll try to explain more detailed and in another way.

When you create an app with CRA, you get a structure like this.

β”œβ”€β”€ appName
β”‚   β”œβ”€β”€  node_modules
β”‚   β”‚   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  public
β”‚   β”‚   β”œβ”€β”€  index.html
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  src
β”‚   β”‚   β”œβ”€β”€  App.js
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ ...

So, let’s imagine that during development I have a structure like this one.

β”œβ”€β”€ appName
β”‚   β”œβ”€β”€  node_modules
β”‚   β”‚   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  public
β”‚   β”‚   β”œβ”€β”€  index.html
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€  src
β”‚   β”‚   β”œβ”€β”€   core
β”‚   β”‚   |   β”œβ”€β”€  atoms
β”‚   β”‚   |   β”œβ”€β”€  molecules
β”‚   β”‚   |   β”œβ”€β”€  organisms
β”‚   β”‚   |   β”œβ”€β”€  ...
β”‚   β”‚   |   β”œβ”€β”€  package.json
β”‚   β”‚   |   β”œβ”€β”€  node_modules
β”‚   β”‚   β”œβ”€β”€  App.js
β”‚   |   β”œβ”€β”€  ...
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ ...

In this case, I will get many errors, because your eslint will start checking sources under core/node_modules. (We can reproduce this, by trying steps mentioned in the first message of this discussion).

My question is still the same. Is it okay, that eslint checks sources under **/node_modules?

In my subjective opinion the way by which this node_modules appears under /src has secondary importance. It can be git submodule or something else. And I can’t find a situation, when disabling of eslint in all node_modules can be a bad thing.

Can’t we ever have some folder in which exists package.json and node_modules under src in the app created by CRA?

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 7 days if no further activity occurs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fson picture fson  Β·  3Comments

oltsa picture oltsa  Β·  3Comments

fson picture fson  Β·  3Comments

Aranir picture Aranir  Β·  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  Β·  3Comments