1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

package.jsonから.envを参照したい場合

Posted at

状況

package.jsonに記述したスクリプトからトークンを参照したい場合があり、以下のように直接記述するのではなく.envで管理したい。

// package.json

...
  "scripts": {
    "chromatic": "npx chromatic --project-token ABCDEFG1234"
  }
...

対応

package.jsonに埋め込めなさそうだったので、大人しく別のスクリプトファイルに切り出すことにした。

# .env

CHROMATIC_TOKEN=ABCDEFG1234
// scripts/chromatic-upload.js

const path = require("path");
const dotenv = require("dotenv");
const env = dotenv.config({path: path.join(__dirname, "../.env") }).parsed;
const exec = require("child_process").exec;

const res = exec(`npx chromatic --project-token ${env.CHROMATIC_TOKEN}`);
console.log(res);

package.jsonから呼び出すように修正。

// package.json

...
  "scripts": {
    "chromatic": "node scripts/chromatic-upload.js"
  }
...
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?