Let me say I have the following lint-staged configuration.
"lint-staged": {
"*.{css,scss}": [
"prettier --fix",
"git add"
],
"*.{js,vue}": [
"eslint --fix --ext js,vue",
"jest --findRelatedTests",
"git add"
]
},
But, I would like to set extra environment variable for "jest" task.
I would like to set the task as NODE_ENV=production BABEL_ENV=node jest --findRelatedTests.
Right now lint-staged will report error that NODE_ENV is not a command.
You can setup the environment variables for the lint-staged command itself. Here's a convoluted demo to pass a environment variable to the command which will be called by lint-staged:
package.json:
{
"name": "test-lint-staged-env-var",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"checkmyvar": "node index.js", // The script to be called by `lint-staged`
"precommit": "cross-env MYVAR=hello lint-staged --debug"
},
"keywords": [],
"lint-staged": {
"*": "checkmyvar"
},
"author": "",
"license": "ISC",
"devDependencies": {
"cross-env": "^5.1.1",
"husky": "^0.14.3",
"lint-staged": "^6.0.0"
}
}
index.js:
'use strict';
if (process.env.MYVAR === undefined) {
throw new Error('environment variable was not set!')
}
λ node .
E:\Projects\experiments\test-lint-staged-env-var\index.js:4
throw new Error('environment variable was not set!')
^
Error: environment variable was not set!
at Object.<anonymous> (E:\Projects\experiments\test-lint-staged-env-var\index.js:4:11)
λ yarn run precommit
yarn run v1.3.2
warning ..\package.json: No license field
$ cross-env MYVAR=hello lint-staged --debug
lint-staged:bin Running `[email protected]` +0ms
lint-staged Loading config using `cosmiconfig` +0ms
lint-staged Successfully loaded config from `E:\Projects\experiments\test-lint-staged-env-var\package.json`:
lint-staged { '*': 'checkmyvar' } +5ms
lint-staged:cfg Normalizing config +0ms
lint-staged:cfg Validating config +4ms
Running lint-staged with the following config:
{
linters: {
'*': 'checkmyvar'
},
concurrent: true,
chunkSize: 9007199254740991,
globOptions: {
matchBase: true,
dot: true
},
subTaskConcurrency: 1,
renderer: 'verbose'
}
lint-staged Loaded scripts from package.json:
lint-staged { checkmyvar: 'node index.js',
lint-staged precommit: 'cross-env MYVAR=hello lint-staged --debug' } +18ms
lint-staged:run Running all linter scripts +0ms
lint-staged:run Resolved git directory to be `E:\Projects\experiments\test-lint-staged-env-var` +2ms
lint-staged:run Loaded list of staged files in git:
lint-staged:run [ 'yarn.lock', 'package.json', '.gitignore' ] +265ms
lint-staged:gen-tasks Generating linter tasks +0ms
lint-staged:cfg Normalizing config +284ms
lint-staged:gen-tasks Generated task:
lint-staged:gen-tasks { pattern: '*',
lint-staged:gen-tasks commands: 'checkmyvar',
lint-staged:gen-tasks fileList:
lint-staged:gen-tasks [ 'E:\\Projects\\experiments\\test-lint-staged-env-var\\yarn.lock',
lint-staged:gen-tasks 'E:\\Projects\\experiments\\test-lint-staged-env-var\\package.json',
lint-staged:gen-tasks 'E:\\Projects\\experiments\\test-lint-staged-env-var\\.gitignore' ] } +5ms
Running tasks for * [started]
lint-staged:run-script Running script with commands 'checkmyvar' +0ms
lint-staged:cfg Normalizing config +13ms
checkmyvar [started]
lint-staged:find-bin Resolving binary for command `checkmyvar` +0ms
lint-staged:find-bin `checkmyvar` resolved to npm script - `node index.js` +1ms
lint-staged:run-script bin: npm +7ms
lint-staged:run-script args: [ 'run',
lint-staged:run-script 'checkmyvar',
lint-staged:run-script '--',
lint-staged:run-script 'E:\\Projects\\experiments\\test-lint-staged-env-var\\yarn.lock',
lint-staged:run-script 'E:\\Projects\\experiments\\test-lint-staged-env-var\\package.json',
lint-staged:run-script 'E:\\Projects\\experiments\\test-lint-staged-env-var\\.gitignore' ] +1ms
lint-staged:run-script opts: {} +3ms
checkmyvar [completed]
Running tasks for * [completed]
lint-staged linters were executed successfully! +2s
Done in 3.00s.
[~~Executed successfully~~]
Note that I am calling a npm script to be able to verify the env var. This should work even if you are invoking any installed binaries.
Another solution would be to define a custom npm script which includes the environement variables:
{
"...": "...",
"scripts": {
"checkmyvar": "cross-env MYVAR=hello node index.js",
"precommit": "lint-staged"
},
"lint-staged": {
"*": "checkmyvar"
},
}
Thanks, The Solution 2 is great. I think this issue can be closed.
@oliverzy I see a documentation PR opportunity here.