Running npm scripts conditionally - node.js

I have 2 main build configurations - dev and prod.
I push updates to a heroku server that run npm install --production to install my app.
In the package.json I have the following segment:
"scripts": {
"postinstall": "make install"
}
that runs a make file that is responsible for uglifying the code and some other minor things.
However, I don't need to run this makefile in development mode. Is there any way to conditionally run scripts with npm?..
Thanks!

You can have something like this defined in your package.json (I'm sure theres a better shorthand for the if statement.)
"scripts": {
"postinstall":"if test \"$NODE_ENV\" = \"production\" ; then make install ; fi "
}
Then when you execute npm with production flag like you stated you already do
npm install --production
it will execute your make install because it will set $NODE_ENV = production
When I need to conditionally execute some task(s), I pass environment variables to the script/program and which takes care of that logic. I execute my scripts like this
NODE_ENV=dev npm run build
and in package.json, you would start a script/program
"scripts": {
"build":"node runner.js"
}
which would check the value of the environment variable to determine what to do. In runner.js I do something like the following
if (process.env.NODE_ENV){
switch(process.env.NODE_ENV){
....
}
}

For conditional npm scripts, you can use cross-platform basic logical operators to create if-like statements ( || and && ).
I've run into needing to run scripts to only generate helpers once on any machine. You can use inline javascript to do this by using process.exit() codes.
"scripts": {
"build":"(node -e \"if (! require('fs').existsSync('./bin/helpers')){process.exit(1)} \" || npm run setup-helpers) && npm run final-build-step"
}
So for testing envs you might do:
"scripts": {
"build":"node -e \"if (process.env.NODE_ENV === 'production'){process.exit(1)} \" || make install"
}

Can't you add another section in your .json under devDependencies? Then if you do npm install it'd install the packages specified under devDependincies and npm install --production would install the regular dependcies.

I would encourage you to take a different tack on uglifying your code. Take a look at connect-browserify or the even more powerful asset-rack.
These can automatically uglify your code on launch of the Express server, rather than on install. And you can configure them to do different things in development and production.

Related

Set environment variables in package.json

I've had a look at quite a number of answers to questions similar to mine but i've not been able to find a working solution for my use case yet.
I've got an environment variable of lets say auth=false. i'd like to set auth=true when i run a particular script in my package.json.
Script in package.json looks like this:
"dev-use-auth": "auth=true && npm run dev"
After running this script, process.env.auth is still set to false. I've also tried using the cross-env package with no luck
This, most likely, would depend on the OS, but for Linux environments you should get rid of the &&.
The && indicates that you wish to run the commands serially.
Try this, instead:
"dev-use-auth": "auth=true npm run dev"
it depends on the operative system you are working on, cross-env will help you running on multiple operative systems by calling it:
"dev-use-auth": "cross-env auth=true npm run dev"
on windows:
"dev-use-auth": "set auth=true && npm run dev"
on unix (mac, linux, etc..c)
"dev-use-auth": "auth=true npm run dev"

Why ts-node index.ts doesn't work, but ts-node through npm run works? [duplicate]

How do I use a local version of a module in node.js. For example, in my app, I installed coffee-script:
npm install coffee-script
This installs it in ./node_modules and the coffee command is in ./node_modules/.bin/coffee. Is there a way to run this command when I'm in my project's main folder? I guess I'm looking for something similar to bundle exec in bundler. Basically, I'd like to specify a version of coffee-script that everyone involved with the project should use.
I know I can add the -g flag to install it globally so coffee works fine anywhere, but what if I wanted to have different versions of coffee per project?
UPDATE: As Seyeong Jeong points out in their answer below, since npm 5.2.0 you can use npx [command], which is more convenient.
OLD ANSWER for versions before 5.2.0:
The problem with putting
./node_modules/.bin
into your PATH is that it only works when your current working directory is the root of your project directory structure (i.e. the location of node_modules)
Independent of what your working directory is, you can get the path of locally installed binaries with
npm bin
To execute a locally installed coffee binary independent of where you are in the project directory hierarchy you can use this bash construct
PATH=$(npm bin):$PATH coffee
I aliased this to npm-exec
alias npm-exec='PATH=$(npm bin):$PATH'
So, now I can
npm-exec coffee
to run the correct copy of coffee no matter of where I am
$ pwd
/Users/regular/project1
$ npm-exec which coffee
/Users/regular/project1/node_modules/.bin/coffee
$ cd lib/
$ npm-exec which coffee
/Users/regular/project1/node_modules/.bin/coffee
$ cd ~/project2
$ npm-exec which coffee
/Users/regular/project2/node_modules/.bin/coffee
You don't have to manipulate $PATH anymore!
From npm#5.2.0, npm ships with npx package which lets you run commands from a local node_modules/.bin or from a central cache.
Simply run:
$ npx [options] <command>[#version] [command-arg]...
By default, npx will check whether <command> exists in $PATH, or in the local project binaries, and execute that.
Calling npx <command> when <command> isn't already in your $PATH will automatically install a package with that name from the NPM registry for you, and invoke it. When it's done, the installed package won’t be anywhere in your globals, so you won’t have to worry about pollution in the long-term. You can prevent this behaviour by providing --no-install option.
For npm < 5.2.0, you can install npx package manually by running the following command:
$ npm install -g npx
Use the npm bin command to get the node modules /bin directory of your project
$ $(npm bin)/<binary-name> [args]
e.g.
$ $(npm bin)/bower install
Use npm run[-script] <script name>
After using npm to install the bin package to your local ./node_modules directory, modify package.json to add <script name> like this:
$ npm install --save learnyounode
$ edit packages.json
>>> in packages.json
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"learnyounode": "learnyounode"
},
...
$ npm run learnyounode
It would be nice if npm install had a --add-script option or something or if npm run would work without adding to the scripts block.
update: If you're on the recent npm (version >5.2)
You can use:
npx <command>
npx looks for command in .bin directory of your node_modules
old answer:
For Windows
Store the following in a file called npm-exec.bat and add it to your %PATH%
#echo off
set cmd="npm bin"
FOR /F "tokens=*" %%i IN (' %cmd% ') DO SET modules=%%i
"%modules%"\%*
Usage
Then you can use it like
npm-exec <command> <arg0> <arg1> ...
For example
To execute wdio installed in local node_modules directory, do:
npm-exec wdio wdio.conf.js
i.e. it will run .\node_modules\.bin\wdio wdio.conf.js
Update: I no longer recommend this method, both for the mentioned security reasons and not the least the newer npm bin command. Original answer below:
As you have found out, any locally installed binaries are in ./node_modules/.bin. In order to always run binaries in this directory rather than globally available binaries, if present, I suggest you put ./node_modules/.bin first in your path:
export PATH="./node_modules/.bin:$PATH"
If you put this in your ~/.profile, coffee will always be ./node_modules/.bin/coffee if available, otherwise /usr/local/bin/coffee (or whatever prefix you are installing node modules under).
Use npm-run.
From the readme:
npm-run
Find & run local executables from node_modules
Any executable available to an npm lifecycle script is available to npm-run.
Usage
$ npm install mocha # mocha installed in ./node_modules
$ npm-run mocha test/* # uses locally installed mocha executable
Installation
$ npm install -g npm-run
TL;DR: Use npm exec with npm#>=7.
The npx command which was mentioned in other answers has been completely rewritten in npm#7 which ships by default with node#15 and can be installed on node#>=10. The implementation is now equal to the newly introduced npm exec command, which is similar but not equal to the previous npx command implementation.
One difference is e.g. that it always interactively asks if a dependency should be downloaded when it is not already installed (can also be overwritten with the params --yes or --no).
Here is an example for npm exec. The double dashes (--) separates the npm exec params from the actual command params:
npm exec --no -- jest --coverage
See also the updated, official documentation to npm exec.
If you want to keep npm, then npx should do what you need.
If switching to yarn (a npm replacement by facebook) is an option for you, then you can call:
yarn yourCmd
scripts inside the package.json will take precedence, if none is found it will look inside the ./node_modules/.bin/ folder.
It also outputs what it ran:
$ yarn tsc
yarn tsc v0.27.5
$ "/home/philipp/rate-pipeline/node_modules/.bin/tsc"
So you don't have to setup scripts for each command in your package.json.
If you had a script defined at .scripts inside your package.json:
"tsc": "tsc" // each command defined in the scripts will be executed from `./node_modules/.bin/` first
yarn tsc would be equivalent to yarn run tsc or npm run tsc:
yarn tsc
yarn tsc v0.27.5
$ tsc
The PATH solution has the issue that if $(npm bin) is placed in your .profile/.bashrc/etc it is evaluated once and is forever set to whichever directory the path was first evaluated in. If instead you modify the current path then every time you run the script your path will grow.
To get around these issues, I create a function and used that. It doesn't modify your environment and is simple to use:
function npm-exec {
$(npm bin)/$#
}
This can then be used like this without making any changes to your environment:
npm-exec r.js <args>
I prefer to not rely on shell aliases or another package.
Adding a simple line to scripts section of your package.json, you can run local npm commands like
npm run webpack
package.json
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"webpack": "webpack"
},
"devDependencies": {
"webpack": "^4.1.1",
"webpack-cli": "^2.0.11"
}
}
If you want your PATH variable to correctly update based on your current working directory, add this to the end of your .bashrc-equivalent (or after anything that defines PATH):
__OLD_PATH=$PATH
function updatePATHForNPM() {
export PATH=$(npm bin):$__OLD_PATH
}
function node-mode() {
PROMPT_COMMAND=updatePATHForNPM
}
function node-mode-off() {
unset PROMPT_COMMAND
PATH=$__OLD_PATH
}
# Uncomment to enable node-mode by default:
# node-mode
This may add a short delay every time the bash prompt gets rendered (depending on the size of your project, most likely), so it's disabled by default.
You can enable and disable it within your terminal by running node-mode and node-mode-off, respectively.
I've always used the same approach as #guneysus to solve this problem, which is creating a script in the package.json file and use it running npm run script-name.
However, in the recent months I've been using npx and I love it.
For example, I downloaded an Angular project and I didn't want to install the Angular CLI globally. So, with npx installed, instead of using the global angular cli command (if I had installed it) like this:
ng serve
I can do this from the console:
npx ng serve
Here's an article I wrote about NPX and that goes deeper into it.
Same #regular 's accepted solution, but Fish shell flavour
if not contains (npm bin) $PATH
set PATH (npm bin) $PATH
end
zxc is like "bundle exec" for nodejs. It is similar to using PATH=$(npm bin):$PATH:
$ npm install -g zxc
$ npm install gulp
$ zxc which gulp
/home/nathan/code/project1/node_modules/.bin/gulp
You can also use direnv and change the $PATH variable only in your working folder.
$ cat .envrc
> export PATH=$(npm bin):$PATH
Add this script to your .bashrc. Then you can call coffee or anyhting locally. This is handy for your laptop, but don't use it on your server.
DEFAULT_PATH=$PATH;
add_local_node_modules_to_path(){
NODE_MODULES='./node_modules/.bin';
if [ -d $NODE_MODULES ]; then
PATH=$DEFAULT_PATH:$NODE_MODULES;
else
PATH=$DEFAULT_PATH;
fi
}
cd () {
builtin cd "$#";
add_local_node_modules_to_path;
}
add_local_node_modules_to_path;
note: this script makes aliase of cd command, and after each call of cd it checks node_modules/.bin and add it to your $PATH.
note2: you can change the third line to NODE_MODULES=$(npm bin);. But that would make cd command too slow.
For Windows use this:
/* cmd into "node_modules" folder */
"%CD%\.bin\grunt" --version
I encountered the same problem and I don't particularly like using aliases (as regular's suggested), and if you don't like them too then here's another workaround that I use, you first have to create a tiny executable bash script, say setenv.sh:
#!/bin/sh
# Add your local node_modules bin to the path
export PATH="$(npm bin):$PATH"
# execute the rest of the command
exec "$#"
and then you can then use any executables in your local /bin using this command:
./setenv.sh <command>
./setenv.sh 6to5-node server.js
./setenv.sh grunt
If you're using scripts in package.json then:
...,
scripts: {
'start': './setenv.sh <command>'
}
I'd love to know if this is an insecure/bad idea, but after thinking about it a bit I don't see an issue here:
Modifying Linus's insecure solution to add it to the end, using npm bin to find the directory, and making the script only call npm bin when a package.json is present in a parent (for speed), this is what I came up with for zsh:
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
precmd() {
if [ "$(find-up package.json)" != "" ]; then
new_bin=$(npm bin)
if [ "$NODE_MODULES_PATH" != "$new_bin" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}:$new_bin
export NODE_MODULES_PATH=$new_bin
fi
else
if [ "$NODE_MODULES_PATH" != "" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}
export NODE_MODULES_PATH=""
fi
fi
}
For bash, instead of using the precmd hook, you can use the $PROMPT_COMMAND variable (I haven't tested this but you get the idea):
__add-node-to-path() {
if [ "$(find-up package.json)" != "" ]; then
new_bin=$(npm bin)
if [ "$NODE_MODULES_PATH" != "$new_bin" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}:$new_bin
export NODE_MODULES_PATH=$new_bin
fi
else
if [ "$NODE_MODULES_PATH" != "" ]; then
export PATH=${PATH%:$NODE_MODULES_PATH}
export NODE_MODULES_PATH=""
fi
fi
}
export PROMPT_COMMAND="__add-node-to-path"
I am a Windows user and this is what worked for me:
// First set some variable - i.e. replace is with "xo"
D:\project\root> set xo="./node_modules/.bin/"
// Next, work with it
D:\project\root> %xo%/bower install
Good Luck.
In case you are using fish shell and do not want to add to $path for security reason. We can add the below function to run local node executables.
### run executables in node_module/.bin directory
function n
set -l npmbin (npm bin)
set -l argvCount (count $argv)
switch $argvCount
case 0
echo please specify the local node executable as 1st argument
case 1
# for one argument, we can eval directly
eval $npmbin/$argv
case '*'
set --local executable $argv[1]
# for 2 or more arguments we cannot append directly after the $npmbin/ since the fish will apply each array element after the the start string: $npmbin/arg1 $npmbin/arg2...
# This is just how fish interoperate array.
set --erase argv[1]
eval $npmbin/$executable $argv
end
end
Now you can run thing like:
n coffee
or more arguments like:
n browser-sync --version
Note, if you are bash user, then #Bob9630 answers is the way to go by leveraging bash's $#, which is not available in fishshell.
I propose a new solution I have developed (05/2021)
You can use lpx https://www.npmjs.com/package/lpx to
run a binary found in the local node_modules/.bin folder
run a binary found in the node_modules/.bin of a workspace root from anywhere in the workspace
lpx does not download any package if the binary is not found locally (ie not like npx)
Example :
lpx tsc -b -w will run tsc -b -w with the local typescript package
Include coffee-script in package.json with the specific version required in each project, typically like this:
"dependencies":{
"coffee-script": ">= 1.2.0"
Then run npm install to install dependencies in each project. This will install the specified version of coffee-script which will be accessible locally to each project.

Use an npm script as "bin"

I have a command line application written in TypeScript with some npm scripts defined in package.json.
"scripts": {
"start": "ts-node src/index.ts",
"start-args": "ts-node src/index.ts -- some args"
},
I would like to link and alias the TypeScript file so that I can call the program easily, so I am looking for something like a "bin" key in the package.json file.
"scripts": {
"start": "ts-node ./src/index.ts",
"start-args": "ts-node src/index.ts -- some args"
},
"bin": {
"foobar": "./src/index.ts",
"bazqux": "./src/index.ts some args"
}
ts-node is installed locally.
However, since TypeScript is not natively supported by node, just putting a shebang on ./src/index.ts won't work.
I would also like to be able to create an aliased command with default arguments, like bazqux above. When I link or install the package as global, I can run "foobar" globally as if I run "npm run start" inside the repository; or run "bazqux" globally as it's "npm run start-args".
How to achieve this?
Update: The issue described below has been fixed in ts-node#8.9.0 thanks to my report. --script-mode now resolves symlinks before looking for the tsconfig.json file. The correct shebang to use is #!/usr/bin/env ts-node-script, which is now also documented. Make sure you have the newest version installed globally with npm -g install ts-node. (I'm not sure whether you also/still need TypeScript installed globally for this. If yes: npm -g install typescript.)
Outdated: For those who also still stumble across this problem, I documented my current approach in detail in this issue. In short, many modules (including those of Node.js itself) require that you use the --esModuleInterop option for the TypeScript compiler. (You can specify this in your tsconfig.json file.) In order to be able to use your command from anywhere and not just from within your package directory, you have to use the undocumented --script-mode option for ts-node or the undocumented ts-node-script command, which does the same.
In other words, your shebang should either be #!/usr/bin/env -S ts-node --script-mode or #!/usr/bin/env ts-node-script. (The former uses -S to pass arguments to the specified interpreter. If this doesn't work on your platform, you can try to hack around this limitation.)
If you want to use the bin functionality of npm (e.g. by running npm link from the package directory), the above doesn't work because --script-mode does not (yet?) follow symbolic links. To work around this, you can use #!/usr/bin/env -S ts-node --project /usr/local/lib/node_modules/<your-project>/tsconfig.json on the first line of your script. This is not ideal, though, as it breaks platform independence. The only other option I see at the moment is to use a bash script instead and call your script from there.
It will work, if ts-node is in path variable use
#!/usr/bin/env ts-node
console.log("Hello World");
Checkout repo:
https://github.com/deepakshrma/ts-cli
Try out:
$ npm i -g https://git#github.com:deepakshrma/ts-cli.git
$ ts-test

Google App Engine Standard Node JS how to run build script?

Does GAE standard for Node support a way to have build scripts? I tried using postinstall within package.json but that did not work.
My codebase has subdirectories with package.json within the subdirectories. In my root package.json there is
scripts: {
postinstall: cd vendor && npm install
....
}
However I'm not seeing any vendor packages installed so I'm inclined to believe the postinstall does not get triggered on GAE Node standard.
Is there any way for me to install subdirectory dependencies without having to copy and paste all my vendor/package.json dependencies to the root?
Note: I've also tried putting an "install" within the package.json scripts but that didn't seem to get triggered either.
In GAE standard, installation of dependencies are automatically managed. You should add them in your package.json.
As Google documentation mentioned :
When you deploy your app, the Node.js runtime automatically installs all dependencies declared in your package.json file using the npm install command.
{
"dependencies": {
"lodash": "^4.0.1"
}
}
Installation will be done during app deployment via :
gcloud app deploy
To add a build step, run the following:
gcloud beta app gen-config --custom
This will generate the default dockerfile and config that is run. In your .dockerfile, add your build step:
RUN npm run build --unsafe-perm || \
((if [ -f npm-debug.log ]; then \
cat npm-debug.log; \
fi) && false)
"prestart": "if [ ! -d build ]; then npm run build; fi",
" -d build" here is the build process generated folder, replace it to whatever you actually use.
Not sure if this will work for your case, but seems like GAE standard has added the ability to run a custom build step.
However it does state:
After executing your custom build step, App Engine removes and regenerates the node_modules folder by only installing the production dependencies declared in the dependencies field of your package.json file.
Maybe since the node_modules are in your vendor/ directory, GAE may not detect and remove them, thus accomplishing your goal. This is a pre-install step, unlike postinstall specified in your script. Not sure if it matters.

How to suppress output when running npm scripts

I have decided to experiment with npm scripts as a build tool and so far I like it. One issue I'd like to solve is when running a script to run jshint when something doesn't pass linting I get a ton of "npm ERR!" lines. I would like to suppress these as the output from the linter is more meaningful.
Is there a good way to set this globally and is there a way to set it for each script run?
All scripts:
You can fix this by suppressing the output of npm overall, by setting the log level to silent in a couple ways:
On each npm run invocation:
npm run --silent <your-script>
Or by creating a .npmrc file (this file can be either in your project directory -local- or your home folder -global-) with the following:
loglevel=silent
Resources:
npm log level config: https://docs.npmjs.com/misc/config#loglevel
npmrc: https://docs.npmjs.com/misc/config#loglevel
Each script, individually:
A simple trick I've used to get around this issue on certain scripts like linting is to append || true at the end of such scripts. This will work without any npm config changes.
This will ensure that the script will always exit with a 0 status. This tricks npm into thinking the script succeed, hence hiding the ERR messages. If you want to be more explicit, you can append || exit 0 instead and it should achieve the same result.
{
"scripts": {
"lint": "jshint || true",
}
}
You should be able to use both the --quiet and --silent options, as in
npm install --quiet
--quiet will show stderr and warnings, --silent should suppress nearly everything
You can also send stdout/stderr to /dev/null, like so:
npm install > '/dev/null' 2>&1
or less versbose
npm install &> '/dev/null'
npm install --quiet --no-progress
Will keep warnings and errors, and suppress the ADHD progress bar on terminals that support it.
for an individual script that you want to keep silent without having to add --silent each time, you can make a new script that calls your previous one and adds --silent.
My example scripts in package.json:
"dev-loud": "npm run build && nodemon -r dotenv/config dist/index.js",
"dev": "npm run dev-loud --silent"
You can do this inside your script by removing the event listeners
#!/usr/bin/env node
process.removeAllListeners('warning');
// Do your thang without triggering warnings

Resources