I'm trying to create a simple script in my package.json file of my node project

  "scripts": {
    "db:create-migration": "echo 'Enter a filename' && read name && echo $name && npx sequelize-cli migration:generate --name $name --migrations-path db-migrations"

It's to call sequelize-cli to create a db migration script with a provided name. It's called via the terminal like so:

npm run db:create-migration

When I echo $name just after read, it prints fine, but I get an Unbound variable "name" with the sequelize-cli call.

There's obviously something that eludes me, and most of the other questions are this are related to set -u - which I'm guessing gets set somewhere behind the scenes when running this.

I'm a unix n00b, so apologies if this is a basic question. Environment is zsh on mac

  • 2
    If that's the exact error message you get (capitalization and all), I wonder if it might be from npm instead of the shell. If it tries to expand that $name itself. (zsh gives zsh: name: parameter not set, Bash gives bash: name: unbound variable.) Try what happens if you run e.g. export VAR=foo in the shell before runing that, and have VAR=bar; echo $VAR in the script...
    – ilkkachu
    Mar 14 at 17:13
  • Hi @ilkkachu, apologies for the delay in replying. I changed the command to echo 'Enter a filename' && read name && export VAR=$name && echo $VAR && npx sequelize-cli migration:generate --name $VAR --migrations-path db-migrations, but I still get the error Unbound variable "name" - this time before the echo, so it's erroring on the export Mar 20 at 7:38
  • no, I mean have "scripts": { "db:create-migration": "VAR=bar; echo $VAR" } in the file, and then run export VAR=foo; npm run db:create-migration from the shell
    – ilkkachu
    Mar 20 at 9:04
  • Ah ok. It echoes bar Mar 20 at 9:17


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.