Difference between start and dev script in package.json file - node.js

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.

Related

Typescript package.json scripts run build and start concurrently: port already in use

I am having an interesting problem building my typescript server using nodemon. I have a script for building out the ts files, and then starting the server. However, when I run these two concurrently, it starts at first fine, then after it is done building, it restarts, but gives me an error that the port is already in use. Is there a way to somehow kill the port each time before it starts?
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start:dev": "nodemon dist/index.js",
"build:dev": "tsc --watch --preserveWatchOutput",
"dev": "concurrently \"npm:build:dev\" \"npm:start:dev\""
},
I have tried adding "npx kill-port 8080 && nodemon dist/index.js" to the start:dev, but I am still getting that error. I have also tried "npx kill-port 8080; nodemon dist/index.js" Is there a solution to this issue? Thanks.
Edit: It seems that this is actually working as I expected it too however, for some reason the terminal is still showing an error message and therefore, anything my server logs to the console is hidden. Is there any way to fix this? Thanks.
I am not sure why exactly you get a port error, but you can improve your setup. Nodemon can run typescript with ts-node help.
Just install ts-node and run nodemon with --exec 'ts-node' property.
Example from my package.json:
{
"dev": "nodemon --watch 'src/**/*' -e 'ts' --exec 'ts-node' src/index.ts"
}

Yarn "posttest" script does not work if "test" script fails

Well, I have these scripts in my package.json for testing some code in NodeJS.
"scripts": {
"pretest": "env NODE_ENV=test sequelize db:migrate",
"test": "jest",
"posttest": "env NODE_ENV=test sequelize db:migrate:undo:all"
}
When the tests go clear, the "posttest" runs, but when the tests fail, I receive a
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
in VS Code. There is nothing usefull on the link about the problem, neither on the internet.
So I found this link about NPM:
https://github.com/npm/npm/issues/5493
The guy said:
In the vast majority of cases, users are going to be unpleasantly
surprised if posttest runs after test failures (you don't want your
test environment being cleaned up or new versions being published if
there were test failures, for instance). As such, this behavior isn't
going to change. Putting something like "test":"npm run-script
test-failing || npm run-script mandatory-cleanup" into your
package.json will give you what you want.
This did not solve my problem. With more research I found this:
npm posttest doesn't trigger if npm test fails
The solutions did not work for me either.
So how can I run the "posttest" script even if the tests fail?
Well, with the conversation above, I got to this solution:
This is my scripts in package.json:
"scripts": {
"dev": "nodemon src/server.js",
"pretest": "env NODE_ENV=test sequelize db:migrate",
"run-tests": "jest",
"run-after-tests": "env NODE_ENV=test sequelize db:migrate:undo:all",
"test": "npm-run-all run-tests run-after-tests --continue-on-error"
},
I installed the npm-run-all package and it runs the run-after-test script even if the tests fail. Now I get the errors
error Command failed with exit code 1.
from the test script, and
ERROR: "test" exited with 1.
error Command failed with exit code 1.
from run-after-test, but in the end my problem got solved. If someone has a better solution with no errors at the end of the scripts, please share with us.

NPM: How can I hook SET NODE_ENV in package.json's prestart script?

I have a basic App created using npm init -y. In package.json I have a main entry which points to server.js.
{
"name": "rest-api",
"version": "1.0.0",
"description": "",
"main": "server.js",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prestart": "SET NODE_ENV=dev"
}
I am trying to set the NODE_ENV variable in prestart and let npm to call main to invoke npm start. But environment variable set in the prestart is not carry forwarded and is undefined. When I run 'npm start', console outputs that both commands are executed in order.
PS D:\test\RestAPI> npm start
> rest-api#1.0.0 prestart D:\test\RestAPI
> set NODE_ENV=dev
> rest-api#1.0.0 start D:\test\RestAPI
> node server.js
undefined
[undefined] Listening on http://localhost:3000
but when I print the variable from the app, it is undefined. Is there anything that I am doing wrong here, or is this how it is supposed to behave? Is there a way to invoke and set env variable using 'SET NODE_ENV=dev' without chaining it to 'node server.js'
When I combine both in the 'start' as below, then the environment variable is set.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "set NODE_ENV=dev && node server.js"
}
I am testing this on Windows 10, npm version 3.10.10. Appreciate your help.
I know how this can be done in package.json using 'start'. This question is specific to how this can be achieved through 'prestart'.
Thanks.
The short answer is NOT possible.
1. Why :
this is not be possible because each script executed by different processes that npm spawns for this purpose which has its own environment variables.
To realise that, create test project and configure both scripts to be like
"start": "pause&&set VAR1",
"prestart" : "pause&&set VAR1=value&&set VAR1&&pause",
On windows open the task manager and pay close attention how many cmd process es are listed before running the script.
run the command "npm start" and at each request "press any key to continue..." just notice how processes created are created. I attached screenshots for this in order
2. Unless :
you change how npm executes different scripts to use one cmd for all the scripts which I think is complicated and probably will create bugs.
If you want to chain scripts and add env variables along the way then checkout the example given in the cross-env package:
{
"scripts": {
"parentScript": "cross-env GREET=\"Joe\" npm run childScript",
"childScript": "cross-env-shell \"echo Hello $GREET\""
}
}

npm: how to run test & lint on each change?

I am using a bare npm ( no grunt/gulp) approach to develop my new MEAN project.
My config is like the following:
File package.json:
...
"start": "nodemon ./bin/www",
"lint": "jshint **/*.js",
"test": "mocha --recursive",
"dependencies": {
...
},
"devDependencies": {
...
},
Before starting, I run an npm start and nodemon starts monitoring my project tree for changes, triggering a restart after each source file change.
So far so good.
But what if I'd like to include - say - the lint and/or the test stages on each restart?
I didn't find any clue nor in the nodemon page nor in the npm one...
So you should have a definition of the start in your package.json to first run lint, then test then the actual run server.
You can find an example in following post:
http://substack.net/task_automation_with_npm_run
you should run the 'npm run monitor' command to start the monitoring and the restart should call the npm run start script.
but basically you want to have (based on your package.json)
"scripts": {
"start": "npm run lint & npm run test & node ./myfile.js",
"lint": "jshint **/*.js",
"test": "mocha --recursive",
"monitor": "nodemon ./bin/www"
.....

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