nvm - How does it pass custom flags to NPM - node.js

Whenever I run npm install I see that npm has been passed certain flags like NVM_CD_FLAGS how does nvm do this? does it actually have some other script in place of npm?

Looks like npm is not getting replaced
> which npm
/Users/welldan97/.nvm/versions/node/v8.5.0/bin/npm
> cat $(which npm)
#!/usr/bin/env node
;(function () { // wrapper in case we're in module_context mode
// windows: running "npm blah" in this folder will invoke WSH, not node.
/*global WScript*/
...
and the contents is the same as on npm github repo
looks like it sets it as environment variable
https://github.com/creationix/nvm/blob/master/nvm.sh#L226
> env | grep NVM
NVM_DIR=/Users/welldan97/.nvm
NVM_CD_FLAGS=-q
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NVM_IOJS_ORG_MIRROR=https://iojs.org/dist
NVM_BIN=/Users/welldan97/.nvm/versions/node/v8.5.0/bin
Then again, not sure where these flags go to, and if they affect npm

Related

Run Laravel Mix without a global nodejs and npm installation

I have a laravel project and I need to build styles and scripts with laravel-mix, but a testing server (Ubuntu 20.04.4) hasn't a globally installed node. Node and npm are in different folders in the system so I run commands like this:
/path/to/node /path/to/npm install
/path/to/node /path/to/npm run dev
But when I run npm run dev (this command runs laravel-mix build), I see the error:
> mazer#2.0.0 dev
> mix
/usr/bin/env: ‘node’: No such file or directory
In the package.json it looks like this:
"scripts": {
"dev": "mix"
...
}
I checked the laravel-mix package (in node_modules) and found this: #!/usr/bin/env node. The package checks the node var in this file, but there is no node var.
I don't need to change the env file, so how can I change this path or set a temporary system var? Is there any way to simulate that the variable is there?
I have one solution for this problem.
The issue regarding naming misspelling or path symlinks.
so that you need to link symlinks for nodejs with this command
ln -s /usr/bin/nodejs /usr/bin/node
or
sudo ln -s /usr/bin/nodejs /usr/bin/node
I resolved my issue with Docker, so now I run this command on git push:
docker run --rm -v /path/to/project:/var/www/html node:16.16.0-alpine npm run dev --prefix /var/www/html
Perhaps it will be useful to someone.
UPD
I found another way to resolve it, I use PATH incorrectly and for this reason it didn't work:
Wrong
I set paths to node and npm and then add it to PATH like this:
NODE_PATH="/path/to/node_folder/node"
NPM_PATH="/path/to/node_folder/npm"
PATH="${NODE_PATH}:${NPM_PATH}:$PATH"
And the system can't find npm and node anyway.
The right way
Add /path/to/node_folders (node and npm are in it) to PATH:
NODE_DIR="/path/to/node_folder"
PATH="${NODE_DIR}:$PATH"
And then, I can run just npm install and npm run dev without full paths to them.

Run postinstall hook for any local dependency

If we have this:
{
"scripts":{
"postinstall":"./scripts/postinstall.sh"
}
}
Then this postinstall hook will run whenever we do an
$ npm install
At the command line
What I am wondering however, is if there is a way to run a postinstall hook when we install a dependency like this
$ npm install x
Is there some NPM hook that we can use for that?
Short answer There's no functionality built-in to npm which provides this kind of hook that I'm aware of.
Possible solution, albeit a bash one, would be to completely override the npm install x command with your own custom logic. For example:
Create a .sh script as follows. Lets name the file custom-npm-install.sh:
#!/usr/bin/env bash
npm() {
if [[ $* == "install "* || $* == "i "* ]]; then
# When running `$ npm install <name>` (i.e. `$ npm install ...` followed
# by a space char and some other chars such as a package name - run
# the command provided.
command npm "$#"
# Then run a pseudo `postinstall` command, such as another shell script.
command path/to/postinstall.sh
else
# Run the `$ npm install` command and all others as per normal.
command npm "$#"
fi
}
Add the following snippet to your .bash_profile file ( Note: you'll need to define the actual path to custom-npm-install.sh):
# Custom logic applied when the `npm install <name>` or the
# shorthand equivalent `npm i <name>` command is run.
. path/to/custom-npm-install.sh
Notes
After configuring your .bash_proile as per point two above, you'll need to create a new terminal session/window for it to be effective. The logic will be effective in all terminal sessions thereafter.
Now, whenever you run npm install <name> or the many other variations such as:
npm install <name>#<version>
npm install <name> <name> --save-dev
npm i -D <name>
etc, etc...
custom-npm-install.sh will run the command as per normal and then run the command ./scripts/postinstall.sh (i.e. whatever the subsequently given command is set to).
All other npm commands will be run as normal, e.g. npm install
Given custom-npm-install.sh current logic the ./scripts/postinstall.sh will be run whenever npm install <name> ... is entered via the CLI. However, if you wanted it to run ONLY when a specific package is installed then you'll need to change the conditional logic in the if statement. For example if you want ./scripts/postinstall.sh to run ONLY when installing shelljs then change the if statement to:
if [[ $* == "install "*" shelljs"* || $* == "i "*" shelljs"* ]];

run global installed npm programs with npm

So you have npm bin -g that yields the bin directory where npm installs stuff. Is there a way to use npm to run stuff from that directory? An obvious option would be $(npm bin -g)/installednodeprogram but I am calling this from an external program (emacs) and would like to avoid bringing up a shell. Also I could get the path from npm bin -g and then concatenate the program name and do all sorts of checks but I hope there is a cleaner way I couldn't find like npm run-binary installednodeprogram.
If you've installed a program globally, you can run its command from the shell just like any other. In fact, that's what the npm command itself is.
For example:
npm install -g browserify
browserify file.js # browserify command is now available

NPM : how to source ./node_modules/.bin folder?

I have a problem on npm installation
I have created a project say project A
cd ~/projectA
npm install sails
but sails command is not found after installation.
I know that it is successfully install in ~/projectA/node_modules directory.
but the executable cannot be sourced. And i know it is installed ~/projectA/node_modules/.bin
How can I source the .bin automatically whenever I enter into this projectA folder?
Did I did something wrong?
I can give you an inelegant solution, which is working for me. I just exported my path in my .bashrc file.
export PATH="$PATH:./node_modules/.bin"
Edit: It's been a while, but I have changed my strategy to use npm scripts instead. In the above case, setup package.json as follows:
"scripts": {
"sails": "sails"
}
Which you can then run with
npm run sails
or with arguments
npm run sails -- <args>
You should use the npm bin command to get an absolute path to your current node bin directory.
For example:
➤ lessc
bash: lessc: command not found
➤ npm bin
/home/brice/[...]/node_modules/.bin
➤ export PATH=$(npm bin):$PATH
➤ lessc --version
lessc 1.7.3 (Less Compiler) [JavaScript]
This avoids the problem of relative paths, especially if you're going to be using this in a build system that will invoke the command in subdirectories.
A bit more robust is:
export PATH=$(npm bin):$PATH
You can either run it, add it to your shell profile, or create an alias like:
alias snpm='export PATH=$(npm bin):$PATH'
If you do go the alias route, be sure to use single quotes so it delays the execution of the variables!
To use on the command line like sails generate foo you will need to install the npm module globally.
npm install -g sails
You could also use the path to the bin in the command if you don't want to install globally:
./node_modules/sails/bin/sails.js generate foo
The official instructions for sails (https://github.com/balderdashy/sails) advises
To install the latest stable release with the command-line tool:
sudo npm -g install sails
This installs globally and adds to a directory like /usr/local/bin that should be in your $PATH.
But to answer the general question regarding the location of the binaries if you install locally, they should be placed in ./node_modules/.bin directory (so run ./node_modules/.bin/sails ...)
If you don't like to mess up with your PATH for running a npm script that isn't global -- e.g. you are the only one to use it --, I would personally recommend the use of an sh "alias".
npm install (locally) your beloved package (json-diff here, for instance)
cd ~ && npm install json-diff
alias it (save it in your ~/.xxxxrc file):
alias diffj "\`npm bin\`/json-diff !*"
Then, for diffing 2 json's:
diffj old.json new.json
In my ~/.bashrc, I have the following:
function nbin {
local dir;
dir=$(npm bin)
if [ -d "$dir" ]; then
( # subshell to not change this shell's $PATH
PATH=$dir:$PATH
eval "$#"
)
else
echo "\"$dir\" is not an npm binary directory." >&1
return 1
fi
}
I can then run executable foo in the .bin directory as:
nbin foo
Possible workaround with NPM 5.2+ using the npx command.
npx sails new test-project
See this question for a similar use case and elegant solutions.

Global Node modules not installing correctly. Command not found

I am having a problem installing global node modules and everything I find online says the solve is just adding -g. Which is not the problem. I believe it's a linking issue or wrong directory issue.
Here is what I do:
$ npm install -g express
npm http GET https://registry.npmjs.org/express
npm http 304 https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/range-parser/0.0.4
npm http GET https://registry.npmjs.org/mkdirp/0.3.3
...downloads correctly
$ express myapp
bash: express: command not found
However when I run the direct link location to express it works:
$ /usr/local/share/npm/bin/express myapp
create : myapp
create : myapp/package.json
create : myapp/app.js
... Builds app correctly
Where the module is:
$ which node
/usr/local/bin/node
$ node -pe process.execPath
/usr/local/Cellar/node/0.8.20/bin/node
$ npm link express
/Users/bentonrr/Development/Personal/node_modules/express -> /usr/local/share/npm/lib/node_modules/express
In my .bash_profile I have:
export PATH=/usr/local/bin:$PATH
export NODE_PATH=/usr/local/lib/node_modules:/usr/local/lib/node
Do I need to change my Node environment to download to correct folder? Is something not linking correctly? I am lost..
Thanks!
Other Specs:
$ node --version
v0.8.20
$ npm --version
1.2.11
$ brew --version
0.9.4
OSX Version 10.8.2
This may mean your node install prefix isn't what you expect.
You can set it like so:
npm config set prefix /usr/local
then try running npm install -g again, and it should work out. Worked for me on a mac, and the solution comes from this site:
http://webbb.be/blog/command-not-found-node-npm/
EDIT: Note that I just came across this again on a new Mac I'm setting up, and had to do the process detailed here on stackoverflow as well.
Add $(npm get prefix)/bin to your PATH (e.g., in .bashrc), like so:
echo "export PATH=$PATH:$(npm get prefix)/bin" >> ~/.bashrc
For more info, see npm help npm:
global mode:
npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.
You can find the install prefix with npm get prefix or npm config list | grep prefix.
My npm couldn't find global packages as well. I did what Brad Parks suggested:
npm config set prefix /usr/local
Then I got a EACCES permissions error (DON'T USE sudo npm install -g <package>) and fixed it through the official npm docs: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
On the command line, in your home directory, create a directory for global installations:
mkdir ~/.npm-global
Configure npm to use the new directory path:
npm config set prefix '~/.npm-global'
In your preferred text editor, open or create a ~/.profile file and add this line:
export PATH=~/.npm-global/bin:$PATH
On the command line, update your system variables:
source ~/.profile
Then install a package globally and test it! For example:
npm install -g awsmobile-cli
awsmobile configure
In my case, The NODE_PATH environment variable was empty. Check whether it is empty-
echo $NODE_PATH
if the NODE_PATH is empty. Then change ~/.bash_profile and add NODE_PATH
nano ~/.bash_profile
export NODE_PATH=`npm root -g`
source ~/.bash_profile
Now install npm modules again and check whether that is being installed on the path npm root -g
For Windows users
Add this to your path: "%AppData%\npm"
I do not ever install any npm stuff, via sudo! I have my own reasons, but I just try to keep things simple, and user based, since this is a user development world, and not everyone has root access, and root/sudo installing things like this just seems to clutter up things to begin with. After all, all developers should be able to follow these instructions, not just privileged sudo users.
This particular system is a RHEL7 accessed via SSH:
Frequently one needs various versions of node, so I use NVM https://github.com/creationix/nvm
So with that said, I can show you a working example for -g global installs, using NVM, NPM, and node paths not using root.
set your prefix for .npm-packages if it isn't already. (note, thats a hyphen, not an underscore)
nvm config ls
prefix = "/home/<yourusername>/.npm-packages"
Then adjust your ~/.bash_profile or .bashrc if you prefer readup on why and which here, with the following information.
#PATH EXPORTS
NODE_MODULES=$HOME/.npm
NPM_PACKAGES=$HOME/.npm-packages/bin
export PATH=$PATH:$HOME/bin:$NODE_MODULES:$NPM_PACKAGES
#NVM ENABLE
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
That pretty much covers all paths. For e.g., if you install gulp like this npm install -g gulp it symlinks in ~/.npm-packages/bin (note thats a hyphen, not an underscore). (no need for gulp-cli, or gulp-cl)
You can pretty much replace/comment-out all other node path exports. You can put this path info below any other path info you already have, safely, without it overwriting that stuff.
Check your global Node module's binary folder, and add it to your $PATH.
npm list -g | head -1
If you use nodenv, the path will change whenever you install a new global node version. Adding a node path like this solves my problem.
"$HOME/.nodenv/versions/$(nodenv global)/bin"
Shortcut for adding the path to zsh
$ echo 'export PATH="$HOME/.nodenv/versions/$(nodenv global)/bin"' >> ~/.zshrc
Add the following line to your ~/.bash_profile
export PATH="$HOME/.npm/bin:$PATH"
Load bash profile
bash -l
The problem I had was missing the binaries because the user specific .npmrc file in my home directory had bin-links set to false, though the default is true.
Just in case this is your problem check that none of your .npmrc files have it set to false.
Then re-installing all modules will create the binaries at the prefix so your PATH can see them.
It may seem like a hack, but setting up yarn when possible saves you a lot of node environment headaches for various unix distros.

Resources