10

Is there a way to concat $PWD with a string in package.json

I am trying:

"config": {
  "mypath" : "$(pwd)/assets/dist/js"
}

But it doesn't seem to work. Is it a way to access the current working path?

It works if I use it in a script. e.g.

"scripts": {
  "echo" : "echo $(pwd)/assets/dist/js"
}
Avraam Mavridis
  • 8,698
  • 19
  • 79
  • 133
  • 1
    Just out of the blue, but did you try with [template literal](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) , like so ```"echo": "echo `{$(pwd)}`/assets/dist/js"``` – mutantkeyboard Oct 06 '17 at 12:11
  • _it doesn't seem to work_ That works for me. What do you get ? On which platform ? – TGrif Oct 06 '17 at 12:21
  • @TGrif sorry, this works with scripts, but not in config on package.json. I will update my question – Avraam Mavridis Oct 06 '17 at 12:25
  • I don't think using that will be cross-platform compatible. Why not use `__dirname` built in? – zero298 Oct 24 '17 at 19:39
  • Hi, it doesn't work either for me. You cannot use __dirname in a package.json script, you need to write a command. Commands that work in the PowerShell doesn't seem to work in package.json, which is pretty annoying when wanting to share scripts. Example: `docker run --rm -p 27017:27017 -v \"$pwd\\.mongo:/data/db\" --label vulcan-mongodb mongo:4.0.4`. This won't work yet the command is correct. – Eric Burel Feb 21 '22 at 11:34
  • What is the actual problem you are looking for a solution for? $PWD is never a good idea when attempting to describe a consistent file or directory – leitning Feb 23 '22 at 13:33

2 Answers2

3

Don't know your exact use-case, but you could use $npm_package_config_path in your script to pass it as an argument:

  "config" : {
    "path": "/assets/dist/js"
  },
  "scripts" : {
    "something":"CONFIG_PATH=${PWD}$npm_package_config_path node -e \"console.warn(process.env.CONFIG_PATH)\"",
  }
}

Then:

$> npm run something

/path/to/your/dir/assets/dist/js
malix
  • 3,566
  • 1
  • 31
  • 41
1

I don't know where do You want to use config.mypath value but if you want to use this value in a script You could use this approach:

Before to start We have to know that: npm uses several programs to run the scripts. As default it uses bash in gnu/linux and cmd in windows (We can set the shell as in this question ). Therefore every script should be created to run over bash or cmd and often They are not compatibles.

Now let's get to work

  1. You could get config.mypath value as follows:

    "config": {
      "mypath" : "${PWD}/assets/dist/js"
    },
    "scripts": {
     "show-path": "echo $npm_package_config_mypath"
    }
    

    and run the command

    npm run show-path
    

    and the output would show us the config.path value

    ${PWD}/assets/dist/js
    

    of course this is not the value You want... but We can work with these value in a shell to get what We want.

  2. In bash We can use the next syntax to execute commands:

    echo [comand] | bash
    

    for example:

    echo echo \${PWD}/assets/dist/js | bash
    

    is the same as:

    echo ${PWD}/assets/dist/js
    

    and the output is:

    /home/user/assets/dist/js
    

    And I think these output is the value You want to read and use in your scripts...

  3. Now We can implement this trick to our package.json

    a) linux(bash):

     "config": {
       "mypath" : "${PWD}/git_repo"
     },
     "scripts": {
       "config": "echo  echo $npm_package_config_mypath  |bash",
       "git-clone":  "echo git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY ${npm_package_config_mypath}-foo | bash"
     }
    

    b) windows(cmd): in windows pwd works in powershell but in CMD pwd does not exist. Then we have to use %cd% and write our scripts with CMD syntax...

      "config": {
        "mypath": "%cd%\\git-repo"
      },
      "scripts": {
        "config": "echo echo %npm_package_config_mypath%  | cmd",
        "git-clone": "echo git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY %npm_package_config_mypath%-foo | cmd"
      }
    

In the examples config.mypath is used to create two scripts:

config: prints config.mypath value

git-clone: clones a repository in the config.mypath folder

clay
  • 1,015
  • 5
  • 12
  • Perfect, the "echo | cmd" trick is what I was looking for. Here is my command for starting a Mongo database: `echo docker run --rm -p 27017:27017 -v \"%cd%\\.mongo:/data/db\" --label vulcan-mongodb mongo:4.0.4 | cmd` (without using a config field, but you can also use one) – Eric Burel Feb 25 '22 at 15:10
  • 1
    As you are not using config.properties value, you don't need to use the trick (in scripts section %cd% works natively ). Try to write the command directly with CMD rules. somthing like this will works `docker run --rm -p 27017:27017 -v "%cd%\\.mongo:/data/db\" --label vulcan-mongodb mongo:4.0.4` – clay Feb 25 '22 at 17:08
  • 1
    Ok weird, I probably did something wrong earlier, because I couldn't find an alternative that worked for both Powershell and cmd. Running the command directly in Powershell doesn't work as it won't use cmd, but "yarn run" will, that's probably what tricked me. – Eric Burel Feb 28 '22 at 11:18