Can you set aliases for npm scripts inside package.json? - node.js

I have a project written in Typescript
When developing Locally:
ts-node is installed as a dev-dependency, The commands are
to start: "ts-node src/index"
to init: "ts-node bin/init"
to init db: "ts-node bin/database-init"
to migrate db: "ts-node bin/database-migrate"
to add users: "ts-node bin/add-users"
When Deployed:
dev-dependencies are dropped, app is transpiled, the commands are
to start: "node src/index"
to init: "node bin/init"
to init db: "node bin/database-init"
to migrate db: "node bin/database-migrate"
to add users: "node bin/add-users"
Thus I am forced to maintain this in my package.json which will keep growing
"scripts": {
"start": "ts-node src/index",
"start:js": "node src/index",
"init": "ts-node bin/init",
"init:js": "node bin/init",
"db:init": "ts-node bin/db-init",
"db:init:js": "node bin/db-init",
"db:migrate": "ts-node bin/db-migrate",
"db:migrate:js": "node bin/db-migrate",
"add:users": "ts-node bin/add-users",
"add:users:js": "node bin/add-users"
},
I would much rather have a single command that works in both
to do this I have set the following alias in the deployment server
alias ts-node=/usr/bin/node
as such all these now works for both..
"scripts": {
"start": "ts-node src/index",
"init": "ts-node bin/init",
"db:init": "ts-node bin/db-init",
"db:migrate": "ts-node bin/db-migrate",
"add:users": "ts-node bin/add-users",
}
But this is not a great solution and prevents me from deploying it anywhere else.. I prefer to set the name space for right-node to ts-node || node inside the package.json this way it would be portable..
I know that the namespace for all dependencies gets added when running npm scripts, So this does happen behind the scenes, but is there any built in functionality to do this manually?

I think the best solution is to work with NODE_ENV but first, you need to install:
npm install if-env --save
and then in the script:
"scripts": {
"start": "if-env NODE_ENV=production ?? npm run start:prod || npm run start:dev",
"start:dev": "ts-node src/index",
"start:prod": "node src/index"
}
And on the server you need to set NODE_ENV to production
On Linux:
export NODE_ENV=production
And for production, I suggest using something like pm2 or foverer insted node to start an application

Related

how to change the path in dotenv with various script commands (in package.json) such as : start , test , etc

well my problem is when I want to switch my script command in package.json like from "start" to "test" for running my Jest test which its commands like :
"scripts": {
"start": "nodemon express/***",
"serve": "node express/***",
"dev": "node express/***",
"test": "jest --watch"
},
and I call dotenv in my project like this
require("dotenv").config({
path: "express/config/.env",
});
The code above, help my to using my environment file like .env
but the problem is that when I want to test my project and I want to switch my script command (in package.json) from like "start" to "test" and change the main path of dotenv environment to something like test.env
You could pass the environment type as an environment variable into your program like so. Note: you will need to use cross-env if you require multi-platform support.
Unix version:
"scripts": {
"start": "NODE_ENV=production nodemon express/***",
"serve": "NODE_ENV=production node express/***",
"dev": "NODE_ENV=dev node express/***",
"test": "NODE_ENV=test jest --watch"
}
cross-env version:
"scripts": {
"start": "cross-env NODE_ENV=production nodemon express/***",
"serve": "cross-env NODE_ENV=production node express/***",
"dev": "cross-env NODE_ENV=dev node express/***",
"test": "cross-env NODE_ENV=test jest --watch"
}
And then access them using the normal method of process.env.NODE_ENV
const envVariablePaths = {
"production": "/path/here",
"dev": "path/here",
"test": "path/here",
}
require("dotenv").config({
path: envVariablePaths[process.env.NODE_ENV],
})
More documentation can be found here

nodemon in npm script triggered multiple times

My npm scripts:
"build": "tsc -w -p ./src/server",
"run": "nodemon --watch ./dist/server ./dist/server/app.js",
"start": "concurrently --kill-others \"npm:build\" \"npm:run\""
From VSCode's terminal I can start the app using the start task.
But nodemon starts twice, and restarts multiple times when a file is saved. I assumed it's because the build task hasn't completed yet.
How can I make these work in series, so the one waits for the other? I do not want to use polling.
I'm using Ubuntu 18, node 10.15.0, npm 6.5.0.
Example of my configuration which works well:
package.json:
"start:dev": "nodemon --config nodemon.json ./dist/src/index.js",
nodemon.json:
{
"watch": ["src"],
"ext": "ts",
"ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/index.ts"
}

Nodemon ''npm' is not recognized as an internal or external command

I realize that this is most likely a duplicate question. I'm new to nodemon and I'm trying to establish a server for a Vue JS project with nodemon. I'm trying to run eslint with nodemon and can't figure out why I keep getting the error message. If I remove npm after --exec it will tell me ''run' is not recognized, and if I remove that I will get ''lint' is not recognized and so on.
My package.json file:
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/app.js --exec 'npm run lint && node'",
"lint": "eslint **/*.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^4.16.0",
"nodemon": "^1.14.12"
}
}
I have also tried this code in my start scripts:
"scripts" : {
"start": "./node_modules/nodemon/bin/nodemon.js src/app.js --exec 'npm run lint && node'",
"lint": "./nodemodules/.bin/eslint **/*.js"
}
Where is tells me that "." is not recognized as an internal external command.
I've installed nodemon into my server folder and in the project directory as well as globally. I have done the same with eslint as well.
i had the same problem today. did some google stuff and found that this is not working anymore. so i tried this
"scripts": {
"prestart": "npm run lint ",
"start": "nodemon src/app.js ",
"lint": "./node_modules/.bin/eslint src/*.js"
},
when you npm start node will run the pre-start script before the start script.Once a file being updated this pre-start wont run by the nodemon.So for that we have to call the nodemon events.So create a nodemon.json on root folder and paste following.
{
"events": {
"restart": "npm run lint"
}
}
you can read more nodemon config options from here nodemon config .There are more nodemon events.you can read them from here event restart
PS:im very new to this. :)
EDIT1:
You can use as follows. this dont need a nodemon config;
"scripts": {
"start": "node src/app.js",
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon src/app.js --exec \"npm run lint --fix && node\"",
"lint": "eslint --fix **/*.js "
}
for run use npm run dev it will run es lint + nodemon. this is for windows cmd command.if you are using bash terminal, remove \ in "dev" ;
"dev": "nodemon src/app.js --exec "npm run lint --fix && node""
I had the same problem.
For some reason you can't use simple quotes in npm scripts.
Use escaped double quotes instead. This should work:
"start": "nodemon src/app.js --exec \"npm run lint && node\""
install it globally for making it available on path.
npm i -g nodemon
or if using yarn
yarn global add nodemon
and if you tried this approach and it didn't work.
you should try running it locally..
you have to create a script in your package.json like this
"script": {
"server" : "nodemon scriptFile.js" //name of the file you want to run
}
then use,
npm run server
but before it,
install nodemon locally.
check it, if it is available on package.json

Concurrently does not modify NODE_ENV variable

So I'm working on a project using webpack and wanted to create a script on my package.json to run both dev and production mode from there. I'm a windows user and always use Concurrently to run multiple terminal tasks at the same time.
I set my package.json scripts like this:
"scripts": {
"start": "concurrently \"set NODE_ENV=\" \"webpack --watch\"",
"build": "concurrently \"set NODE_ENV=production\" \"webpack\""
},
The output of this in the terminal is:
set NODE_ENV= exited with code 0
Webpack is watching the files…
...
So basically webpack is working properly, but the variable is not being created/deleted. Both commands are failing.
If I run directly
set NODE_ENV=production
it works, so I'm a bit confused...
Any ideas?
Thanks a lot!
Change:
"start": "concurrently \"set NODE_ENV=\" \"webpack --watch\"",
"build": "concurrently \"set NODE_ENV=production\" \"webpack\""
to:
"start": "NODE_ENV= webpack --watch",
"build": "NODE_ENV=production webpack"
You cannot change the environment in one process and expect it to be changed in another started in parallel. You can only change the env of child processes and only on their startup. Child process always inherits the environment from the parent.
If the above doesn't work on Windows then use cross-env:
npm install --save-dev cross-env
and in package.json use:
"start": "cross-env NODE_ENV= webpack --watch",
"build": "cross-env NODE_ENV=production webpack"

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"
.....

Resources