Troubleshoot: NODE_PATH does not work in create-react-app

CategoriesJavaScript, Node & React

I’ve had way too many problems with this. The use case specifically is plugging a different config object (that has e.g. the api url) for an environment. I want to be able to:

import config from 'config'

that defines things like the location of the backend, and dynamically load it based on environment (“development”) and the path:

NODE_PATH=config/environments/development node scripts/start.js

which I already do in several projects. Every time I’ve ejected the create-react-app to gain low-level control over webpack. It seems NODE_PATH is not even supported by CRA, futhermore multiple locations of node_modules aren’t supported either. To fix that, after ejecting, I make a small change to config/modules.js :

function getAdditionalModulePaths(options = {}) {
  return process.env.NODE_PATH.split(':')

Obviously this is not portable across OS’es, and won’t work on windows without making the filesystem separator more dynamic.

I should submit a pull request to create-react-app with this or a similar change – I see no reason *not* to use NODE_PATH.

The approach described above works very well to configure environments in rails. I oftentimes adapt rails architecture whenever I go – be it node, python, or some other stack.

Leave a Reply