How to change npm path - npm

All my npm packages work, but my npm package list shows empty. I am sure this is issue with a path but not sure how to fix it.
Which gulp gives me >
[~] ruby-2.2.3 $ which gulp
/usr/local/bin/gulp
Which npm gives me >
[~] ruby-2.2.3 $ which npm
/usr/local/bin/npm
npm list gives me >
[~] ruby-2.2.3 $ npm list
/Users/kimmo
└── (empty)

It looks like you are confusing packages that are installed globally with those locally. The paths for gulp and npm look like the global install locations. Packages you install locally will be found under a node_modules folder in the root of your project.
You can confirm this by comparing the results from:
npm ls -g --depth=0
npm ls --depth=0
The first command will show the globally installed packages. The second will show the local packages.
At the top of the resulting output, each shows the directory where the global or local install is located.
Finally, the --depth=0 flag only shows the packages that were required and not the dependencies of those packages (and those packages, etc). I find that most of the time, that's all I care about. If you agree, you can easily make this a default with npm config set depth 0 or by editing your .npmrc file in your home directory.
So! My guess is that you installed gulp with the -g flag but you haven't installed anything locally (with no flag, so to speak). That's why there's a difference between what which is showing and npm ls is showing.

Related

Where can I find a list of all npm flags / tags / options?

I am looking for the meaning of the flag --u as in "npm run test --u" but I don't know where all the flags are documented.
Here are the bottom are some shorthands, but besides "gangster" and other cool ones, I couldn't find --u
https://github.com/npm/npmconf/blob/master/config-defs.js#L405
"npm run test --u" ("--u") belongs to jest flag --updateSnapshot. If you run npm run test help or npm test help you will get all the flags of jest.
Inorder to get npm flags list or npm commands you can run this command npm -l
Here are the list of other npm flags that you might need:
-u or --update: Updates the packages installed in you current working directory
-g or --global: Installs the package globally rather than in your local working directory.
-v or --version: Displays the version of npm.
-h or --help: Displays help information for the npm command.
-l or --long: Shows extended information for the installed packages.
-j or --json: Outputs the npm registry data in json format.
-S or --save: Saves the package as a dependency in your package.json file. As of npm version 5, you might not need this anymore as packages will be saved by default into the package.json file
-D or --save-dev: Saves the package as a dev-dependency in the package.json file.
-O or --save-optional: Saves the package as an optional dependency in the package.json file.
-E or --save-exact: Saves the package at the exact version specified in the package.json file.
-P or --save-prod: Saves the package as a production dependency in the package.json file.
-B or --save-bundle: Saves the package as a bundled dependency in the package.json file.

What npm install -s mean?

I am installing some packages on NPM, sometimes I have to write -s and -g? What do they mean?
npm install -s socket.io
npm install -g xxxxxxx
npm -g <package> will install a package globally on your machine. Without the -g or --global flag, the package will be installed locally in the directory you were in when you ran the command.
npm -S <package> with an uppercase -S or --save will install the package and save it to your dependencies in your package.json, although I believe that is now the default behavior in current npm. I recommend reading the docs if you're unfamiliar with what's happening when you pass different options to npm.
#gmmetheny answered the question about the global -g flag, but -s appears to silence the output of the command (at least in npm v7.0.15).
In other words, including -s (or --silent) in your npm install command means that it will have no output (only a newline):
> npm install -s example-package1 example-package2
This may be useful for running the command in a script.
Running the command without the -s flag echoes information about what was installed, e.g.:
> npm install example-package1 example-package2
npm WARN deprecated some-pkg#1.2.3: this library is no longer supported
added 160 packages, and audited 160 packages in 6s
14 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
You can diff the resulting directories created after running each variant of the command and you can verify that the effects are the same.
As #Max mentioned, this option is NOT mentioned in the npm docs (at least not in any prevalent location where a user might find it after a reasonable amount of searching).

Can I re-create node_modules from package-lock.json?

I cloned a repository from github which has a package-lock.json (but no package.json). Then in a git bash terminal I go to the directory and run npm install but I just get a message saying there is no package.json and then everything in package-lock.json gets deleted so it's basically empty except for the project name and version.
I thought running npm install with a package-lock.json in the directory was enough to re-create node_modules, but am I seriously misunderstanding how this works? By the way I have node 8.12.0 and npm 6.4.1 and am running on Windows 10. Also, I think the package-lock.json was created on a unix system so could there be problems when using package-lock.json on a different OS?
I already tried running npm init just to get a package.json file and then running npm install but that still didn't get me a node_modules folder.
Starting from Mar 5, 2018, you can run npm ci to install packages from package-lock.json.
npm ci bypasses a package’s package.json to install modules from a
package’s lockfile.
https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable
package-lock.json records the exact version and url of packages need to install, thus you can use npm to install them accordingly:
npm can install from urls that point to tarballs
--no-package-lock option to tell npm to not touch package-lock.json file
For example, to install all packages in package-lock.json:
cat package-lock.json | jq '.dependencies[].resolved' | xargs npm i --no-package-lock
jq is a command line tool to pares jq, you can write a simple JavaScript script to parse it instead (if you do not want to install jq or learn jq's query syntax).
AFAIK, the package-lock.json file relies on the presence of a package.json file, so you'll not be able to recreate your node_modules folder from the package-lock.json file alone (happy to be proved wrong here).
Therefore, your best bet is to (mis)use a module like auto-install that is capable of generating the package.json file based on a project's dependencies, as they appear in the files.
Install it globally (npm install -g auto-install), then you'll need to generate an empty package.json file for it to run (use npm init -y in your project root). Kick things off with the command auto-install and it should add the dependencies to the package.json file.
HTH

npm: using 'npm uninstall' vs. just removing the folder

I wanted to try grunt-babel, so I opened up a terminal in my Home folder and did npm install --save-dev grunt-babel babel-preset-es2015 according to the plugin's instructions.
I was doing this too hastily, and realized I should probably have done this in my new project folder where I am dabbling with ES6 code. I had not even done npm init in that folder nor in the Home folder from where I executed the install command.
When I do npm uninstall grunt-babel, the preset files are removed but 91 folders of different dependencies remain in the node_modules folder.
Can I simply remove the folder instead of running npm uninstall 91 times?
This guy asked a similar question but none of the answers address his subquestion of just removing the folder: how to uninstall npm modules in node js?
npm uninstall <name> removes the module from node_modules, but not package.json.
npm uninstall <name> --save to also delete the dependency from package.json.
npm rm <package_name> removes the packages when uninstall not working
npm prune <name> (see docs) for extraneous packages and packages that are not listed on the parent package's dependencies list.
If you don't want to uninstall one by one run
rm -rf node_modules && npm cache clean && npm install
It's a good way for being sure the packages you uninstall are no more in the packages json.
Now in 2021 npm uninstall <name> will also removed it from package.json
UPDATED answer (2020):
These are all aliases to uninstall:
remove, rm, r, un, unlink
And today there is no need for --save flag since it is the default. The same goes for install BTW.
Use npm list as a tool to understand your changes. I usually use the time to make a capture file like:
npm list >1307
do some change
npm list >1309
so then:
cat 13??
or an editor lets me see what npm thinks it did.
For uninstall, only packages on the root all size of 'whole package' get removed. Other then that, the command is politely ignored...
For example:
├── safe-stable-stringify#1.1.0
├── semver#6.3.0
├─┬ tableify#1.1.0
│ └─┬ optimist#0.6.1
│ ├── minimist#0.0.8 deduped
│ └── wordwrap#0.0.3
safe-stable-stringify is a removal candidate, but wordwrap is not. Think about it, this is entirely reasonable !
npm uninstall pkgtoyank -save
updates packages.json by removing it from there as well.
npm is very well designed to say the least. I usually hugely avoid directly poking under it in ./node_modules I will copy things out from there to look at them, but why yank on a leash of a BIG CAT and get bit. it works; use it as its intended....

npm install -g *no arguments*

What does the command npm install -g (no arguments) do? I opened npm docs but didn't get it:
In global mode (ie, with -g or --global appended to the command), it installs the current package context (ie, the current working directory) as a global package.
What does it mean?
What it means is that whatever project currently in, npm will take the dependencies from the package.json in your current project folder and then install those dependencies globally.