how to create a node package manager - javascript

my github repository application how to Create mypkgname CLI package manager application
I have registered this package in npm, so I can install it globally:
npm install -g mypkgname-cli
mypkgname init myApp
cd myApp
npm install
npm start
just examples
npm install -g mern-cli
npm install -g #angular/cli

You may want to use some cli arg pareser like commander
or yargs
There are many other command line argument parsers.
This tutorial here explains the usage well https://developer.atlassian.com/blog/2015/11/scripting-with-node/
Important parts include
adding #!/usr/bin/env node on top of your entry point script.
in package.json, something like
"bin": {
"mycliapp": "index.js"
}
then running npm link or npm install -g on your app.

Related

if i deleted my project but i have that in my github so did i have to re install all the dependencies again or just run npm install?

My question is if i deleted my project but i have that in my github so did i have to re install all the dependencies again or just run npm install? i formeted my pc so i project lost away but before formet i uploaded my project on github so do i have to download all dependencies again or just run npm install? i have dependencies of nodejs react native and choco socketio and some more so that i can do downlaod everthing again or just npm install and all dependencies will be install by npm
once you run git clone [REPO_URL] if all dependencies are listed in package.json then running npm install in cloned project directory will install all dependencies

Cannot install Vue Cli on Mac Catalina [duplicate]

While installing the dependencies of vue-cli, vue is not identified. Why?
rm -rf node_modules and npm install again
have a look here
Add sudo before yarn when installing
yarn global remove #vue/cli
sudo yarn global add #vue/cli
vue
I had the same issue for a while.
TL;DR
npm install #vue/cli-service --save-dev
As the documentation specify it is a development dependency https://cli.vuejs.org/guide/#cli-service
The CLI Service (#vue/cli-service) is a development dependency. It's an npm package installed locally into every project created by #vue/cli.
Origin
I had a fresh install of nodejs
And just did
>> sudo npm install -g #vue/cli#latest
>> vue --version
#vue/cli 4.5.8
The issue
The issue presented like this
>> npm run serve
yarn run v1.22.10
$ vue-cli-service build --mode development --watch
/bin/sh: 1: vue-cli-service: not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
The fixes:
npm install #vue/cli-service --save-dev
Which led me straight to another error message
>> npm run serve
yarn run v1.22.10
$ vue-cli-service build --mode development --watch
ERROR Error: Cannot find module 'vue-template-compiler/package.json'
Which I fixed the same way
npm i vue-template-compiler --save-dev
And now it is working fine.
Installing current version without permanently installing vue-cli.
npx #vue/cli create appname
It shows the vue executable is located at /home/alisha/.local/bin. So probably this location is not there in your $PATH.
You should be able to run the vue commands if you provide the full path, like:
~/.local/bin/vue create hello-world
You can also see if that directory is in your PATH by running some command like:
echo $PATH | grep '.local/bin/'
If it's there, you would see it, otherwise you can add it to your path by placing it in your ~/.profile.
Edit ~/.profile and add the following at the bottom of it.
PATH="$HOME/.local/bin:$PATH"
Hope it helps!!
I had the same issue while making a build for production.
You will require vue-cli to be installed. Use below command to install the latest version.
npm install -g #vue/cli#latest
Then
npm install
Might have to do with you having an old version on your computer:
Warning regarding Previous Versions
The package name changed from vue-cli to #vue/cli. If you have the previous vue-cli (1.x or 2.x) package installed globally, you need to uninstall it first with
npm uninstall vue-cli -g or yarn global remove vue-cli.
You can find it here: https://cli.vuejs.org/guide/installation.html
THIS FIXED THE ISSUE FOR ME:
After running
sudo npm install -g #vue/cli
I ran
sudo nano $HOME/.profile
and pasted the following line
export PATH=$PATH:/home/chike/.npm-global/bin
after writing the code, next thing I did was Ctrl + O, ENTER and Ctrl + X then wrote
vue init webpack myapp
When you install vue using cli that time you got the path of vue.Now you can copy the bin folder path.
In my example /home/sublime/.npm-packages/bin
Now you export the path below command
export PATH=$PATH:/home/sublime/.npm-packages/bin
I solved mine by running (add sudo if needed)
npm i -g vue-cli#2.9.6
npm i -g #vue/cli
Got similar issue when deploy vue project in jenkins.
Here is what I did:
Add node's bin/ dir, to jenkins user's .bashrc file.
e.g
# node
NODE_HOME=/home/dev/.nvm/versions/node/default
PATH=$NODE_HOME/bin:$PATH
Tips - about nvm & yarn
When you manage node version via nvm, make sure you already choose the node version in terminal. e.g:
nvm use stable
node -v
If you use nvm, and installed yarn via npm, then better install vue-cli via npm not yarn, otherwise the vue executable is not placed into node's bin/ dir, at least that's the case in my tests, and as a result will cause you fail to find the vue command.
Using Yarn on Ubuntu it is installed to ~/.npm-packages/bin/. You must add this directory to your PATH. For example run the following command, close your terminal and open a new one.
user#machine:~$ echo 'export PATH="$PATH:~/.npm-packages/bin/"' >> ~/.bashrc
Note: if the file ~/.bashrc does not exist then simply create it.
Following worked for me:
First remove all the existing ones:
yarn global remove #vue/cli
yarn global remove #vue/cli-service
sudo yarn global remove #vue/cli
sudo yarn global remove #vue/cli-service
Then add #vue/cli using sudo:
Note: use sudo if required
yarn global add #vue/cli
yarn global add #vue/cli-service
Then, the final thing to do is to RESTART the terminal.
vue --version
#vue/cli 4.5.9
If you already got a project, the only two things you need to do is:
Delete the directory node_modules (it is safe, since it is not under git and will regenerate in the next step)
In the command-line write yarn install (it will install everything you need)
If you start installing vue-cli manually in a existing project, it the package.json and package-lock.json will be updated. If you already did. Do a checkout from git, and follow my steps above
This may be a problem caused by version conflicts. "export PATH=$PATH:" This is really useful in some cases. But if you are also like me, after trying the direct “export path” method in the comment above, restarting the terminal still can not execute the situation, you can try this way.
Uninstall Vue
npm uninstall -g #vue/cli
Check the local-global npm package installation path, vue is installed in this directory, check if it has been removed.
npm root -g
Install vue (you can check with https://cli.vuejs.org/#getting-started to find the latest command)
npm install -g #vue/cli
Create a connection to the /usr/local/bin directory(You need to find the vue.js path after the local installation first, then replace this path with your latest local install path:/Users/xxxxx/.npm-global/lib/node_modules/#vue/cli/bin/vue.js)
ln -s /Users/xxxxx/.npm-global/lib/node_modules/#vue/cli/bin/vue.js /usr/local/bin/vue
View version number
vue -v
I was getting the same error because Node.js was not installed. My issue got resolved by installing Node.js using the following command:
sudo apt install nodejs-legacy
To see if you already have Node.js and npm installed and check the installed version, run the following commands:
node -v
npm -v
If both are installed then follow the steps here:
https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
I installed the package using yarn global add #vue/cli on my Ubuntu box and found the binary in /home/vonkad/.yarn/bin.
I had to modify my /home/vonkad/.bashrc and add the directory to the path export PATH=$PATH:/home/vonkad/.yarn/bin.
To fix this situation, I had to add the following line to my .zshrc (maybe in your case is .bashrc)
export PATH="$(yarn global bin):$PATH"
Effectively, the yarn global bin is a folder where vue (vue-cli 3) was placed.
What helped me
mac os catalina with zsh terminal
Uninstalled node and npm using https://www.positronx.io/how-to-uninstall-node-js-and-npm-from-macos/
Downloaded node/npm from https://nodejs.org/en/download/current/
sudo npm install -g #vue/cli
vue --version (#vue/cli 4.5.4)
I faced the same issue and now resolved. In my case I installed Node.js and NPM using the default Ubuntu repository by using this command sudo apt-get install nodejs npm
The problems seemed like those 2 packages are not well maintained so it caused some bugs.
So I purge those packages and reinstall it from nodesource which is officially recommended way to install (reference: Installation instruction from nodesource) using these commands.
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt-get install -y nodejs
Then reinstall #vue/cli again
sudo npm install -g #vue/cli
Now these issues have gone. Hope it helps some programmers.
You need to install vue via sudo like explained in the doc:
https://cli.vuejs.org/guide/installation.html
If you have WSL2 running and you use zsh like me, just add
yarn global add #vue/cli
# add this line to ~/.zshrc
export PATH="$HOME/.yarn/bin:$PATH"
$ vue --version
#vue/cli 4.x.xx
You can try the following code install see
npm install --global vue-cli
vue init webpack <YOUR-PROJECT-NAME-HERE>
cd <YOUR-PROJECT-NAME-HERE>
npm install
npm run dev

how to install package under (as) peerDependencies?

I'm building a library and trying to understand how to install/specify a dependency under peerDependencies.
The npm docs don't talk about this under the command npm install:
npm install (with no args, in package dir)
npm install [<#scope>/]<name>
npm install [<#scope>/]<name>#<tag>
npm install [<#scope>/]<name>#<version>
npm install [<#scope>/]<name>#<version range>
npm install <git-host>:<git-user>/<repo-name>
npm install <git repo url>
npm install <tarball file>
npm install <tarball url>
npm install <folder>
alias: npm i
common options: [-P|--save-prod|-D|--save-dev|-O|--save-optional] [-E|--save-exact] [-B|--save-bundle] [--no-save] [--dry-run]
Any help?
As far as I know, there isn't a command line shortcut for installing a peer dependency. Some Googling dug up this old issue where the npm folks briefly discussed adding that functionality, but it doesn't seem to have been made it in as of npm#6.
It's less-than-ideal, but I think manually editing your package.json file to specify peer dependencies by hand may be your best bet. As of this 2013 blog post from the Node.js team, that approach almost seems sanctioned:
Peer dependencies are pretty simple to use. When writing a plugin, figure out what version of the host package you peer-depend on, and add it to your package.json: ...

Is npm more like mvn or like pip?

I'd like to know if Javascript's npm installs dependencies OS-wide like Python's pip (if pip is not using a virtualenv), or if npm installs dependencies more like Java's mvn which stores the things in a local pom.xml file (to me mvn feels more like if it was always in a virtualenv if compared to pip).
So I mainly want to know if it's OS-wide installs or local installs that are performed with npm.
Both!!
npm install -g PACKAGE_NAME will install the package globally.
npm install PACKAGE_NAME will install the package locally in the current folder, under node_modules/.
npm install --save PACKAGE_NAME will install the package locally and save it as a dependency in your package.json.
Checkout https://www.sitepoint.com/beginners-guide-node-package-manager/
if you execute just npm install <dependency name> you are telling node to install package only on your current directory this will also create node_modules in current directory you are installing.
and if you execute the npm install -g <dependency name> with the -g you are saying that you want the node to install the dependency globally and the package will be save on the global node_modules. and most of the global dependencies can be access using CLI
example.
$ npm install mocha
you can execute the command in the current directory execute the npm install or where the node_module is
$ $PWD/node_modules/.bin/mocha -v
if you
$ npm install -g mocha
you can execute the command anywhere in your directory on the terminal
$ mocha -v

Print a list of all installed node.js modules

In a node.js script that I'm working on, I want to print all node.js modules (installed using npm) to the command line. How can I do this?
console.log(__filename);
//now I want to print all installed modules to the command line. How can I do this?
If you are only interested in the packages installed globally without the full TREE then:
npm -g ls --depth=0
or locally (omit -g) :
npm ls --depth=0
Use npm ls (there is even json output)
From the script:
test.js:
function npmls(cb) {
require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
if (err) return cb(err)
cb(null, JSON.parse(stdout));
});
}
npmls(console.log);
run:
> node test.js
null { name: 'x11', version: '0.0.11' }
list of all globally installed third party modules, write in console:
npm -g ls
in any os
npm -g list
and thats it
Generally, there are two ways to list out installed packages - through the Command Line Interface (CLI) or in your application using the API.
Both commands will print to stdout all the versions of packages that are installed, as well as their dependencies, in a tree-structure.
CLI
npm list
Use the -g (global) flag to list out all globally-installed packages. Use the --depth=0 flag to list out only the top packages and not their dependencies.
API
In your case, you want to run this within your script, so you'd need to use the API. From the docs:
npm.commands.ls(args, [silent,] callback)
In addition to printing to stdout, the data will also be passed into the callback.
Why not grab them from dependencies in package.json?
Of course, this will only give you the ones you actually saved, but you should be doing that anyway.
console.log(Object.keys(require('./package.json').dependencies));
for package in `sudo npm -g ls --depth=0 --parseable`; do
printf "${package##*/}\n";
done
As the end of 2021, there are few obvious way to do it, and a part as the only one give on the answer above this is a complete list.
The Node.js Documentation is actually pretty well explained regarding the matter, this is a collective list of the main commands.
All Commands will run the list of installed modules Locally. In order to run global level just add a -g flag at the end of the statement.
See the version of all installed npm packages, including their dependencies.
❯ npm list
>>> /Users/joe/dev/node/cowsay
└─┬ cowsay#1.3.1
├── get-stdin#5.0.1
├─┬ optimist#0.6.1
│ ├── minimist#0.0.10
│ └── wordwrap#0.0.3
├─┬ string-width#2.1.1
│ ├── is-fullwidth-code-point#2.0.0
│ └─┬ strip-ansi#4.0.0
│ └── ansi-regex#3.0.0
└── strip-eof#1.0.0
Get only your top-level packages
npm list --depth=0
Get the version of a specific package by specifying its name.
npm list <package-name>
See what's the latest available version of the package on the npm repository
npm view <package-name> version
Install an old version of an npm package using the # syntax
npm install #
npm install cowsay#1.2.0
Global package
npm install -g webpack#4.16.4
Listing all the previous versions of a package
npm view cowsay versions
[ '1.0.0',
'1.0.1',
'1.0.2',
'1.0.3',
'1.1.0',
'1.1.1',
'1.1.2',
'1.1.3',
....
]
Update all the Node.js dependencies
Install new minor or patch release
npm update
Install new minor or patch release but not update package.json
npm update --no-save
To discover new releases of the packages, this gives you the list of a few outdated packages in one repository that wasn't updated for quite a while
npm outdated
Some of those updates are major releases. Running npm update won't update the version of those. Major releases are never updated in this way because they (by definition) introduce breaking changes, and npm wants to save you trouble.
To update all packages to a new major version, install the npm-check-updates package globally:
npm install -g npm-check-updates
ncu -u
This will upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version
Dev Dependency
Install in development dependencies.
npm install <package-name> -D
npm install <package-name> --save-dev # same as above
Avoid installing those development dependencies in Production with
npm install --production
Uninstalling npm packages
npm uninstall <package-name>
npm uninstall -g <package-name> # globally uninstall
Uninstall a package and ** remove the reference in the package.json**
npm uninstall <package-name> -S
npm uninstall <package-name> --save # same as above
Some commands with global flag examples.
npm list -g
npm list --depth=0 -g
npm list <package-name> -g
npm view <package-name> version -g
Additional Commands
Answer by #prosti
Documentation
Find the installed version of an npm package
Install an older version of an npm package
Update all the Node.js dependencies to their latest version
Semantic Versioning using npm
Uninstalling npm packages
npm global or local packages
npm dependencies and devDependencies
The npx Node.js Package RunnerTABLE OF CONTENTS

Categories

Resources