Installing local Node.js project globally - javascript

I'm trying to install my custom made project globally across my system so I can call it anywhere in my computer from the cmd, but it doesn't quite work.
I tried:
npm install -g .
and more but I can't remember. This is my package.json:
...
"name": "nodejs-insta-reddit-cli",
"version": "1.0.0",
"description": "",
"main": "./build/index.js",
"bin": {
"instapostwouter": "./build/index.js"
},
...
The reason the main path is in ./build/index.js is because I'm using typescript.
So my question is: How would I install my project globally so that I can use it on my workstation in any directory?

Use npm link in the directory of local module you want to access globally.
Then you add it using npm link package_name.
OR you can publish the module (Private or Public) and use install it globally and use it.
More information on Npm Link
NOTE: if you are working with cli package then make sure you have a bin folder and entry inside the package.json file.

Related

Is there a way to get npm install to install "bin" executables in the local node_modules directory (the .bin directory)?

I have a Node project, and I can npm pack it into a tarball and install it in another directory for testing. When I do that, the set of files in the "bin" clause of my package.json are correctly symlinked into the node_modules/.bin directory there. Thus I can use npx something to run the "something" script and it works as I would expect.
From the actual project directory, however, npm install doesn't do that. The scripts are there in my top-level "bin" directory of course, but npx doesn't find them. Obviously I could just run them directly, but it would be nice if I could run the same shell commands to run them in other installation directories and in the project home directory.
I don't want to install the package globally; I want to keep everything local, at least for now. So can I get npm to make those symlinks for me, or should I just bite the bullet and do it myself?
Here's my package.json:
{
"name": "#foo/plazadb",
"version": "0.1.0",
"description": "Basic database setup with upload/download from CSV",
"main": "lib/plazadb.js",
"author": "Pointy",
"license": "ISC",
"dependencies": {
"arg": "^5.0.1",
"cls-hooked": "^4.2.2",
"csv-parser": "^3.0.0",
"csv-stringify": "^6.0.5",
"neat-csv": "^7.0.0",
"pg": "^8.7.*",
"sequelize": "^6.16.*"
},
"bin": {
"conntest": "./bin/conntest.js",
"download": "./bin/download.js",
"upload": "./bin/upload.js"
}
}
The "bin" files all exist (of course, otherwise they would never work). What I'm trying (out of ignorance) is a simple npm install from the project directory.
One way to do is by
specifying a local prefix by using npm config set prefix <path> or one of these methods linked here.
generating symlinks using npm link
As a word of caution, to undo npm link executing npm unlink . may not work. Instead one may have to run npm uninstall -g ..

Can my NPM CLI package be executed on CMD without installing globally?

I have written an NPM package which has its own CLI commands.
Let's name that package as xyz and imagine it's now avaialable on npmjs.com
So, let's say a user installs this package in his project by running npm install xyz.
And now he wants to run a CLI command provided by xyz package on his terminal in his project.
xyz do_this
Can this be done without installing this package globally by user ? Or without any further configuration for the user ?
Here's some part of the package.json of the xyz package.
{
"name": "xyz",
"version": "1.0.0",
"description": "Description",
"main": "index.js",
"preferGlobal": true,
"bin": "./index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
........
Here's how npm works. When you install a package in the local directory, it installs the executable dependencies files inside node_modules inside the folder with package.json, but if you use --global, it places it globally where the user has set their path.
For example, when I run npm install http-server, my executable ends up as ./node_modules/http-server/bin/http-server but when i install it globally, I have it as node_modules/http-server/bin
The work around for this is, if you want to just run the executable, just execute it inside like so ./node_modules/http-server/bin/http-server. If you want it as a command, you'll need too have the user add the directory to their Path so when the user enters the command, the computer looks for the command inside that folder.
Here's a guide to adding PATH directories in Linux, you'll just add the directory /pathtofolder/node_modules/http-server/bin/ or whatever your folder is. https://linuxize.com/post/how-to-add-directory-to-path-in-linux/
For example, if I wanted to add http-server from my local folder to my path, I would run
export PATH="/pathtofolder/node_modules/http-server/bin/:$PATH"
Good luck! Let me know how I can help you!

How do you use "yarn create <pkg-name>"?

I was reading Yarn blog and found it supports yarn create just like create-react-app.
https://yarnpkg.com/blog/2017/05/12/introducing-yarn/
I tried locally... basically made very simple application with following package.json.
{
"name": "create-test-app",
"version": "1.0.0",
"bin": {
"create-test-app": "./index.js"
},
"dependencies": {
...
}
}
But somehow,,, it is complaining that it can't find the package.
"error Couldn't find package "create-test-app" on the "npm" registry.
In order to use "yarn create", I should upload on "npm" registry? Can't try it in locally? Thanks in advance.
The way to use this is yarn create <starter-kit-package>. Starter kit package must have been installed globally. You can find create-* starter kit packages in the npm registry.
Here is the link to documentation for Yarn create
To add to #Oluwafemi Sule 's answer.
The way to use this is yarn create <starter-kit-package>. Starter kit package must have been installed globally. You can find create-* starter kit packages in the npm registry.
This means that for it to work you need to install it to your yarn global or publish it to npm registry.
Fortunately you can install your local package globally. So start by creating a starter kit package then do this:
yarn global add file:/path/to/create-kit-package
Then after this you can do:
yarn create kit-package
In the above kit-package is the name of the package without create, since you are supposed to create the package with a name that matches the following format:
create-package-name
so on the cli you call the command as :
yarn create package-name
to invoke once it is installed globally.
You can publish this to NPM once you are happy with the package so that someone else or yourself can install it the normal way without first adding the package globally.
Yarn takes from NPM and does not have its own mechanism for creating packages. When you create a package on NPM, Yarn will actually grab its details through the NPM Registry.
More information: http://blog.npmjs.org/post/151660845210/hello-yarn

node.js minimal setup and for what is the package.json?

So i start off with writing my own node.js app and the only thing i want is to include a package for saml.
So i was wondering what is the minimal requirement for my app.
I was just creating a node.js file and then i installed the package by:
node install some-saml-passports-package.
I got this warning after installation:
npm WARN enoent ENOENT: no such file or directory, open '.../mynodeapp/package.json'
I removed the package and created a package.json file. Which results in a parsing error because there was no content inside.
I have read that I only need a package.json when I plan to create my own package. Also I read something about local and global installation of package files in npm. Well, i need a bit of clarification for my doing.
What is the use of package.json?
Do I need it when I only want to create a standalone app in node with one dependency to an existing package?
Is it recommended even if I don't need it at all?
There is no minimum requirement for node.js application. The package.json is only required to manage your application for dependencies, module descriptions , handle npm scripts etc. You can install packages without package.json, just don't use the --save flag with npm install. E.g.:
npm install <package_name> - you can still run your node application without any problem. But I will suggest you to add a package.json file and add following:
{
"name": "<package_name>",
"version": "<version>", //0.0.1 for starters
"description": "",
"main": "<entry point of application>", // example: app.js , index.js etc
,
"author": "<your name>",
"license": "ISC",
"dependencies": {}
}
You can create this by adding it manually or just execute npm init in your project root directory and answer some basic questions in console.
Here are some useful links:
npm init
how to use package.json
The minimal file is:
{
}
Now you can start using commands like npm install web3 --save and they will save onto this file.
Create package.json via npm init command.
Package.json contains data about your project and what is more important for standalone app - it contains dependencies list. You can install all dependencies from package.json with npm install.
If you want to install package and save it in package.json type npm install package-name --save.

Not able to install "node_module"s

I am a node.js beginner..!!
I am trying to install node_modules but getting following error.
I am on Windows and using node.js v5.7.0
Also, getting the same error on Mac OS X.
First this is not an error but a warning saying you don't have a package.json file. This file could look like this for a beginning.
{
"name": "project name",
"version": "0.0.1",
"private": true,
"main": "main.js",
"start": "node main.js",
"dependencies": {
"colors": "^1.1.2"
}
}
Check this to why using a package.json file.
Edit
You can also run npm init (suggested by #Nicklas Ridewing)
You need to create a package.json file before you run npm install colors
Just run
npm init
And follow the guide to create one :)
Use npm init to setup you project package.json.
Use npm install <package-name> --save to save it to your dependencies.
Use npm install <package-name> --save-dev to save it to your development dependencies.
Those will be saved in to your package.json which will be your project main configuration file.

Categories