Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only honor relative NODE_PATH #1194

Merged
merged 1 commit into from
Dec 7, 2016

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented Dec 7, 2016

We used to honor NODE_PATH because it provides a relatively convenient way to allow absolute imports for people who need them: NODE_PATH=src npm start.

However this backfired with #1023 and #815.
The best analysis is in #1023 (comment).

Sometimes, a module (which may be inside a dependency) may depend on Node core modules like events. In this case Webpack attempts to shim it with node-libs-browser. However it only attempts to shim it if it can't resolve the module name using the regular mechanism. The problem is that some Linux distributions include Node sources into NODE_PATH. Therefore, since we honor NODE_PATH, Webpack finds events in Node.js sources on those systems, and thus doesn't attempt to shim it. As a result you get the latest version from Node.js itself which is likely incompatible with the browsers.

To fix this, we need a way to not allow Node.js core modules to resolve to Node.js sources even if they exist in NODE_PATH. I don't see any easy way to do this. However we can sidestep the issue by not respecting absolute paths in NODE_PATH. The only reason we added NODE_PATH support is for absolute imports (NODE_PATH=src npm start), where NODE_PATH itself is relative. Any other uses of it are inherently dangerous (and won't work across systems anyway). So I think it should be safe to just stop honoring absolute NODE_PATH altogether, thus solving both issues.

It is technically a breaking change but for a use case we never intended to support. I have a hard time believing anyone could've intentionally relied on this, and if it breaks anyone, it will more likely uncover a bug. Therefore I think this is safe to go in 0.8.x, especially given that it fixes more egregious bugs that have been affecting people for months, and 0.8 has been out for just four days.

@gaearon
Copy link
Contributor Author

gaearon commented Dec 7, 2016

Before this change with Node.js sources in NODE_PATH:

screen shot 2016-12-07 at 15 34 22

After this change with Node.js sources in NODE_PATH:

screen shot 2016-12-07 at 15 38 03

The example works with using NODE_PATH=src and absolute imports as well.

@debugpoint136
Copy link

I am sorry but I am still experiencing trouble getting a successful build. I went through this thread but could not locate how to fix this.
node - 6.10.0
npm - 3.10.10
OS - MacOS El Capitan
react-scripts: 0.9.3

Problem: Whenever I use ES6 feature like template string or fat arrow function notation, as helper JS functions to fetch data and pass it to React components, npm start works fine but npm run build fails. NODE_PATH is not set to anything. Any pointers would greatly help.

static/js/main.edbb179d.js from UglifyJs
SyntaxError: Unexpected token: operator (>) [./api/consensus_view-api.js:65,0]

static/js/main.d10c594a.js from UglifyJs
SyntaxError: Unexpected character '`' [./api/consensus_view-api.js:25,0]

Thanks!

@Timer
Copy link
Contributor

Timer commented Mar 2, 2017

Looks like you're trying to import a JavaScript file from outside src/; this is not supported. The file will not be compiled.

This behavior will most likely be disallowed in 0.10.

@debugpoint136
Copy link

Oh wow! Thanks for catching that. I moved that folder to src and it compiles successfully!

@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants