create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

.env NODE_PATH is not working in v1.0.0

Open CodinCat opened this issue 9 years ago • 39 comments

Description

I have NODE_PATH=src in .env, it did work great in v0.9.5. After yarn upgrade react-scripts it doesn't work now. And I didn't see any related guide in the v1.0.0 changelog.

(referenced from https://github.com/facebookincubator/create-react-app/issues/2188)

I've also tried in a fresh new project and it doesn't work.

Expected behavior

Should be able to import 'foo/bar' which foo is a folder located in src

Actual behavior

Module not found: Can't resolve 'foo/bar' in '/Users/some/path/src'

Environment

Run these commands in the project folder and fill in their results:

  1. npm ls react-scripts (if you haven’t ejected): v1.0.0
  2. node -v: v7.1.0
  3. npm -v: v3.10.9

Then, specify:

  1. Operating system: macOS Sierra
  2. Browser and version: Chrome 58

Reproducible Demo

https://github.com/CodinCat/cra-env-issue

This is a fresh new project. See /src/index.js:5

CodinCat avatar May 19 '17 07:05 CodinCat

Didn't knew you could add that to .env, I'm using it in a custom npm script and it still works fine after updating to 1.0.0:

"scripts": {
  ...
  "dev": "cross-env NODE_PATH=./src npm run start"
}

I've got a problem with paths in tests by the way but I don't know if that's related to NODE_PATH or to the updated version of jest, I will report as soon as I could find where exactly the problem is.

ingro avatar May 19 '17 08:05 ingro

This is a real deal-breaker on upgrades to apps that use this form of importing. Changing an entire app to use relative paths will be tiresome. I tried @ingro's suggestion but that didn't work for me; still getting module not found errors.

will-stone avatar May 19 '17 09:05 will-stone

Interesting. Anyone looked yet into why this happens?

gaearon avatar May 19 '17 10:05 gaearon

Same for me. Have NODE_PATH=./ in .env and imports like src/components not working anymore.

RIP21 avatar May 19 '17 10:05 RIP21

Thanks for confirming. Would be good if somebody could take a peek in the source into why. I'd expect it to work.

gaearon avatar May 19 '17 10:05 gaearon

Seems unrelated to ModuleScopePlugin. Will investigate more.

Timer avatar May 19 '17 10:05 Timer

As stated above I have only problems with tests, moving the __tests__ folder from the root to src (since now only tests inside src will run) returns me this weird error (my tests also relies on NODE_PATH):


 FAIL  src\__tests__\modules\stats\stats.test.js
  ● Test suite failed to run

    Cannot find module 'package.json'

      at Function.Module._resolveFilename (module.js:470:15)
      at Function.Module._load (module.js:418:25)
      at Module.require (module.js:498:17)
      at require (internal/module.js:20:19)

If I change imports in tests to be relative the error disappear.

This doesn't seem related to jest because I created a small repo to try to reproduce this error without cra but it's working fine there.

ingro avatar May 19 '17 10:05 ingro

Can you show the test code? Does it import package.json?

gaearon avatar May 19 '17 10:05 gaearon

Looks like .env isn't being loaded at all, not that this feature doesn't work. If I run NODE_ENV=src yarn start this works fine.

Timer avatar May 19 '17 10:05 Timer

I could've messed it up when refactoring something although I'd expect our E2E tests to catch that.

gaearon avatar May 19 '17 10:05 gaearon

I don't believe any of our e2e tests use the .env file, only the feature itself.

Timer avatar May 19 '17 10:05 Timer

Sure @gaearon , no package.json imported:

// src/__tests__/modules/statistiche/statistiche.test.js
import { actions } from 'modules/statistiche/actions/statistiche';

describe('Statistiche actions', () => {
    it('refreshAll action', () => {
        expect(actions.refreshAll()).toMatchSnapshot();
    });

    // ...
});

Also the imported file is pretty basic:

// src/modules/statistiche/actions/statistiche.js

const REFRESH_ALL   = 'statistiche/REFRESH_ALL';
// ...

function refreshAll() {
    return {
        type: REFRESH_ALL
    };
}

ingro avatar May 19 '17 10:05 ingro

@ingro We'd need a separate reproducing project to look into this. Not clear why.

@Timer I think I might know why it happens. env.js currently depends on paths.js which reads NODE_PATH before env.js could set it. 😄

gaearon avatar May 19 '17 11:05 gaearon

Thanks @gaearon I'll try to create a repo to show the problem in the afternoon

ingro avatar May 19 '17 11:05 ingro

I agree with @gaearon, that's the reason. First we include paths.js where nodePaths is initialized as [] and then read .env files.

ApacheEx avatar May 19 '17 11:05 ApacheEx

@gaearon this is karma for #2034 😆

Timer avatar May 19 '17 11:05 Timer

Lol. I guess we should just duplicate that resolving code in env.js. Since the normal "path resolving" can't apply until we know NODE_PATH.

gaearon avatar May 19 '17 11:05 gaearon

@ingro BTW I fixed my tests with cross-env so it looks like that now cross-env NODE_PATH=./ react-scripts test --env=jsdom. Ugly hack, but works for now. Waiting for fix :)

RIP21 avatar May 19 '17 11:05 RIP21

in webpack config:

const getClientEnvironment = require('./env');
const paths = require('./paths');

the problem that paths.js will be cached when you include it in the second time. As solution we can remove cache after first reading in env.js

const paths = require('./paths');
delete require.cache[require.resolve('./paths')];

at least works for me.

ApacheEx avatar May 19 '17 11:05 ApacheEx

Thanks for the suggestion @RIP21 but still not working for me, I was already running tests like this:

"scripts": {
   "test:cra": "react-scripts test --env=jsdom",
   "test": "cross-env NODE_PATH=./src npm run test:cra"
}

ingro avatar May 19 '17 12:05 ingro