Is it possible to set a dotenv variable from package.json script? - node.js

I am using dotenv in my project to get variables from a .env file. I would like to be able to set a value in the .env(dotenv) file from package.json when running npm test. Something similar to this:
"scripts": {
"test": "set ENV=test mocha './test/**/*.spec.js'",
"start": "node app"
}
my .env file is simply this:
ENV = development
I'm using windows.
Thanks in advance for any help!

You can use
"test": "ENV=test mocha './test/**/*.spec.js'"
in your npm script on Linux, and use
"test": "set ENV=test&&mocha './test/**/*.spec.js'"
on Windows.
BTW, if you need to cross platform, use cross-env so that your code can run on both Windows and Linux.
You can also use require.
For example, you have a ./test/test.env.js
// test/test.env.js
process.env.ENV = 'test';
and use require in your npm script like this:
"test": "mocha --require 'test/test.env.js' './test/**/*.spec.js'"

Yes you can do that, you can pass environment variables through commands too.
i.e. with your scripts:-
"scripts": {
"test": "set ENV=test mocha './test/**/*.spec.js'",
"start": "ENV=test node app.js" //NOTICE I HAVE CHANGED THIS.
}
Just by using, I don't know how to help about the test script, I don't understand that, but I believe it must be something like this
"test": "ENV=test mocha './test/**/*.spec.js'"

Related

Difference between start and dev script in package.json file

In almost all of the docs I've come across so far, most of the time I have seen the start and the dev script being used for a similar kind of functionality. following are 2 examples:
1.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index",
"dev": "nodemon index"
},
"scripts": {
"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
"start": "npm run dev",
So, please help me in understanding, what exactly is the difference between the two in package.json file used for NodeJs. Under what circumstances does mentioning the 2 at the same time make sense.
P.S: I'm new to javascript and node.js. Hence please forgive in case of a silly mistake. Thanks in advance :)
Start is a script handled by default by npm. You can use it without the keyword run:
npm start
dev is a custom script, the name has no significance fr npm. You need to use the keyword run:
# npm run <script name>
npm run dev
documentation for start: https://docs.npmjs.com/cli/v6/commands/npm-start
documentation for run-script: https://docs.npmjs.com/cli/v6/commands/npm-run-script
In other words, start will override the default npm command. By default, npm will run node index.js on start. The start script always exists even if you don't declare it. That is not the case for dev.

Invalid testPattern *-long.spec.js supplied. Running all tests instead [duplicate]

I have in packadge.json
"scripts": {
"test": "cross-env NODE_ENV=test jest"
},
I want to test all files match pattern
front/**/*.test.js
but if
"scripts": {
"test": "cross-env NODE_ENV=test jest \"front/**/*.test.js\""
},
I've got an error
Invalid testPattern front/**/*.test.js supplied. Running all tests instead.
So, how could I pass file names pattern to jest?
Thanks.
According to the documentation you are supposed to use a regex for your pattern. So in your case you probably would write something like this:
"scripts": {
"test": "cross-env NODE_ENV=test jest \"front/.*\\.test\\.js\""
}
Use the testRegex parameter in your jest config to set a filename pattern:
testRegex: 'front/.*\.test\.js$'
Have a look at the documentation here.

How to set multiple environment variables in a single line on Windows?

I'm working on a Node.js project and using Jest as the test framework. This project runs on Windows as it happens, and I'm having a heck of a time setting more than one environment variable on the command line.
Here's the relevant line in package.json
"scripts": {
"test": "SET NODE_ENV=test & SET DB_URI=postgresql://<database stuff>> & jest -t Suite1 --watch --verbose false"
},
As can be seen above, I'm setting both a NODE_ENV and DB_URI environment variable prior to running jest via npm run test.
My problem is the that DB_URI environment variable doesn't appear to be set when jest runs. The error I get back from jest makes it obvious it can't find it. I do know that the first, NODE_ENV environment variable is set ok, but am not sure what's wrong with the second one, did I get the syntax wrong somehow? Is anyone with jest experience on Windows doing something similar to what I'm trying?
Just make the following change:
Use &&, also you need to remove the white space before and after the "&&".
"scripts": {
"test": "SET NODE_ENV=test&&SET DB_URI=postgresql://<database stuff>>&&jest -t Suite1 --watch --verbose false"
},
I'd suggest you to add cross-env. It should be able to set multiple environment variables for Windows and POSIX
package.json
{
// ...
"scripts": {
"test": "cross-env NODE_ENV=test DB_URI=postgresql://<database stuff>> jest -t Suite1 --watch --verbose false"
},
"devDependencies": {
"cross-env": "^6.0.0"
}
}
If you are setting the environment variables from powershell you can do like so:
cmd /K "set f=df & echo %f%"
The output will be "df"

How to get environment variable in npm script?

I am trying to access an enviroment variable in the npm script itself like so:
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha --root ../SERVER/routes -- --recursive"
},
And start this script like so:
SERVER=somewhere npm test
How can I get the resolved value of SERVER variable in the npm script in the package.json itself?
For the windows users, you may use your variables like this: %SERVER% instead of $SERVER.
Or better approach to use cross-env module which will allow you to do it like linux on all platforms:
npm i cross-env
And use it :
"scripts": {
"test": "cross-env-shell \"istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive\""
}
Will using $SERVER work for you?
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha --root ../$SERVER/routes -- --recursive"
}

Debug is not a recognized command (express)

Im running express on windows 8. I ran the command
>express app
after i ran the command to install dependencies
>cd app && npm install
after i attempted to run the app using the given command
>DEBUG=my-application ./bin/www
but I received the error message
'Debug' is not recognized as an internal or external command,
operable program or batch file.
any ideas on how to fix this?
Some background info, I successfully installed node.js from their website. I attempted to install express usings the commands
>npm install
when that didnt work i followed the instuctions on this website https://coderwall.com/p/mbov6w. when that didnt work i used the following command and it worked
npm install -g express-generator#3
i also made my own package.json and app.js based off the express website and i am now stuck.
For Windows : change your start command in the package.json file to
"scripts": {
"start": "set DEBUG=my-application & node ./bin/www"
}
then you can run npm start.
There's no need to set the DEBUG environment variable in your cmd window first.
First, you must set DEBUG as a environment variable:
set DEBUG=my-application
then, you can run the app:
node bin/www
In your root folder of your project you have to run this command
For Windows:
set DEBUG=express:* & node bin/www
For windows environments you need to use SET VARIABLE for example
"scripts" : {
"start" : "SET DEBUG=app & node ./bin/www"
}
That will help you with windows env but if you want to use cross platform I recommend to install this library cross-env that library will help you to set variables for windows and linux environments.
And the json should look like this:
"scripts" : {
"start" : "cross-env DEBUG=app & node ./bin/www"
}
I was having same issue and this help me!
use this settings on your package.json
For window Users..
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "set DEBUG=app & node app.js"
},
For Mac Users
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "DEBUG=app node app.js"
},
app is your-app-name such as [app.js][server.js] etc.
Follow the following steps for windows:
Go to your application i.e. cd app
npm install
set DEBUG=app
npm start
It will start listening on port 3000 by default.
This fixed my issue :
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node src/index.js"
}
The below command is for the windows users running on Git Bash with nodemon.
"scripts": {
"start": "node index.js",
"dev": "SET DEBUG=app:* & nodemon index.js"
}

Resources