Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Proper way of chaining commands when using cross-env #50

Closed
chris-schmitz opened this issue Jan 20, 2017 · 6 comments
Closed

Proper way of chaining commands when using cross-env #50

chris-schmitz opened this issue Jan 20, 2017 · 6 comments

Comments

@chris-schmitz
Copy link

This isn't an issue, just a question.

I was running into an issue when trying to use cross-env to chain several npm scripts together.

    "dev": "npm run build:ui && npm run electron & npm run watch:ui",
    "electron": "cross-env NODE_ENV=development electron --debug .",
    "watch:ui": "cross-env NODE_ENV=development webpack --watch",
    "build:ui": "cross-env NODE_ENV=production webpack --progress --hide-modules",

Each of these commands (aside from dev) work as expected when run individually, but when I try to chain them with dev I get errors when other tools try to access the NODE_ENV variable. e.g.

function save(namespaces) {
  if (null == namespaces) {
    // If you set a process.env field to null or undefined, it gets cast to the
    // string 'null' or 'undefined'. Just delete instead.
    delete {"NODE_ENV":"production"}.DEBUG; // <- undefined property error
  } else {
    {"NODE_ENV":"production"}.DEBUG = namespaces; // <- unexpected token error
  }
}

I came to the repository and read your section on known limitations and understand that I shouldn't expect that environment variables set in one side of chained commands will not necessarily be set in the other side since each link is handled as a separate spawn process, but I thought having the variable declared in each step would negate that issue.

Is there a proper way of doing this kind of command chaining or do I always need to run the scripts from separate terminal calls?

Thanks!

@kentcdodds
Copy link
Owner

Hi there,
I'm really confused by this code:

function save(namespaces) {
  if (null == namespaces) {
    // If you set a process.env field to null or undefined, it gets cast to the
    // string 'null' or 'undefined'. Just delete instead.
    delete {"NODE_ENV":"production"}.DEBUG; // <- undefined property error
  } else {
    {"NODE_ENV":"production"}.DEBUG = namespaces; // <- unexpected token error
  }
}

What is the purpose of this code?

@kentcdodds
Copy link
Owner

The reason I ask is because I'm pretty sure your problems have nothing to do with cross-env

@kentcdodds
Copy link
Owner

closing due to inactivity

@yss14
Copy link

yss14 commented Sep 16, 2017

I ran into the same problem.
Tried to introduce a separate npm script for redis, my postgres db, etc, but if I combine them into a start script by chaining, the env variables are not defined at runtime (npm run start:local).

"scripts": {
		"start": "node src/index.js",
		"start:local": "npm run db:local && npm run redis:local && npm start",
		"db:local": "cross-env PGUSER=postgres PGHOST=localhost PGPASSWORD=**** PGDATABASE=mydb PGPORT=5432",
		"redis:local": "cross-env REDIS_HOST=localhost REDIS_PORT=637919"
	}
console.dir(process.env.REDIS_HOST); //-> undefined
console.dir(process.env.REDIS_PORT); //-> undefined

The two scripts db:local and redis:local are printed on the console, so I guess they are executed.

@kentcdodds
Copy link
Owner

Hi @yss14,
That's happening because each of the scripts is entirely isolated when you put && between them. Try this without cross-env (setting variables the way your shell accepts them) and you'll get the same effect.

I suggest that you just make long scripts:

"scripts": {
		"start": "node src/index.js",
		"start:local": "cross-env PGUSER=postgres PGHOST=localhost PGPASSWORD=**** PGDATABASE=mydb PGPORT=5432 && cross-env REDIS_HOST=localhost REDIS_PORT=637919 npm start"
	}

Either that or you could use something like nps + nps-utils to make your scripts more manageable.

Good luck.

@yss14
Copy link

yss14 commented Sep 16, 2017

It seems I just learned something new about npm scripts ;)
Thanks for your quick response! :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants