package.json for minimal command-line utility - node.js

I am trying to create a command-line utility. But, npm install (no -g) does not link the executable.
My expectation is that npm install would install my executable locally.
My package.json looks like:
{
"name": "test-bin",
"version": "0.1.0",
"description": "Test bin",
"bin": "./bin/test-bin.js",
"main": "./index.js",
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "file:///tmp/test-bin.git"
}
}
index.js is:
module.exports = function() {
console.log('invoked')
}
bin/test-bin.js is:
require('../')()
If I run npm install, node_modules is created, but not .bin
However, if create another project elsewhere that uses the first as a dependency:
{
"name": "test-test-bin",
"version": "0.1.0",
"description": "Test test bin",
"author": "",
"license": "ISC",
"repository": {
"type": "git",
"url": "file:///tmp/test-test-bin.git"
},
"dependencies": {
"test-bin": "file:///Users/you/somewhere/test-bin"
}
}
then npm install links the executable in that project:
node_modules/.bin/test-bin
The npm documentation says, about "bin":
To use this, supply a bin field in your package.json which is a map of
command name to local file name. On install, npm will symlink that
file into prefix/bin for global installs, or ./node_modules/.bin/ for
local installs.
Is it as designed, or am I missing something?

Running npm install inside a package folder will install its dependencies, but it doesn't install any binaries that the package itself declares (you could argue what the point of that would be).
That only happens when the package is installed as a package (using npm install package-name or as a dependency for other packages).

Related

npm list -g shows packages I haven't installed

npm ls -g shows a ton a packages and i havent installed any of them except 1 or two.
enter image description here
the package.json in my home directory:
{
"name": "yash",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {},
"devDependencies": {},
"scripts": {
"test": "mocha"
},
"keywords": [],
"author": "",
"license": "ISC"
}
the node_modules folder in my home directory is empty.
so what are all these files?
The -g flag will show global packages. These are packages which are not included in your package.json but are instead either packaged with your installation or were installed with npm install -g.
If the global packages you install have their own dependencies, then you'll also see those child dependencies listed. For example, you can see you installed nodemon, so you'll also see chokidar in the list, since nodemon requires that package to work.
If you want to ignore the child dependencies and see only the packages you directly installed, you can use npm list -g --depth 0.
You can read more about global packages vs. local packages

npm install does not install the dependencies of individual node modules into the respective node modules folders

npm install was installing node modules correctly until recently when I ran into a bug. Now, npm install does not install the dependencies of individual node modules into the respective node modules folders.
See screenshot for what I mean.
The finder window at the forefront shows the correct npm install before the bug. npm install express would download and put the files in correct folders. The accepts folder is a node module of express and has its own node modules, mime types and negotiator.
Now, the accepts folder and its own node modules sit out at the same level as express folder. As seen in the finder window in the back.
This is causing me not to be able to upload to heroku.
Please advise on how to fix.
Here's my package.json
{
"name": "node-muse-examples-webgui",
"version": "0.1.0",
"description": "An example on how to use the node-muse module in a web interface.",
"main": "index.js",
"engines": {
"node": "7.2.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/ShaPOC/node-muse/tree/master/examples/webgui"
},
"author": "Jimmy Aupperlee <j.aup.gt#gmail.com>",
"license": "GPLv3",
"dependencies": {
"body-parser": "^1.15.2",
"express": "^4.14.0",
"leapjs": "^0.6.4",
"mongodb": "^2.2.7",
"node-muse": "^0.1.0",
"socket.io": "^1.3.5"
}
}
That behavior is not a bug, it actually is a new behavior introduced with npm#3.
That normally should not cause any conflict or problems, but if it does in your case try to install it with
npm install --legacy-bundling
instead, so delete the whole node_modules folder and reinstall it with that command.

npm looks like does not generate the correct wrapper for cli (nodejs)

I use Windows 10, node 5.3.0 and npm 3.5.2
I did a cli, a simple hello world. This is its package of the module
{
"name": "helloworld",
"version": "1.0.0",
"bin": {"hellow": "hello.js"},
"preferGlobal": true,
"description": "Hello...",
"main": "hello.js",
"scripts": {
"test": "node hello.js"
},
"keywords": [
"testing"
],
"author": "me",
"license": "ISC"
}
The test works ok, and I install the module from its directory
npm install -g helloworld
When I execute 'hellow' appears its path and Windows asks me how to open the file.
npm wrappers are like this, from node_modules directory
"%~dp0\node_modules\helloworld\hello.js" %*
I don't know what is going wrong. Thanks.
Ok, I solved it doing more investigation
This is a good guide of how to make a complete npm package http://www.anupshinde.com/posts/how-to-create-nodejs-npm-package/
besides you need to add this line at the beginning of the bin script
#!/usr/bin/env node
So, this is already done (and I am glad =) )

npm WARN install Refusing to install hapi as a dependency of itself

I tried to do the following (per instructions from official site):
mkdir hapi && cd hapi
npm init
npm install hapi --save
But this gives me an error like this:
npm WARN install Refusing to install hapi as a dependency of itself
Now, I made a new test folder called hapiTest and repeated the commands and then everything worked fine.
I tried the same process with a folder gulp and npm install gulp --save, and got the same error, so my conclusion is that I can't have the name of the folder be the same as the package that I want to install, but can someone back this statement up with some official documentation?
When you did the command npm init, there were probably some relevant questions you needed to answer. Specifically, the name of your module. When you use npm init, it assumes you want the name of the module you're creating to be called the name of the folder it is in.
So it's not the name of the folder that is stopping you from installing the dependency, it is the name of the npm module that you are creating.
Open the resulting package.json within your hapi directory, and rename the module to something other than hapi. Here's an example 'package.json' that works, even when residing in a folder called hapi:
{
"name": "hapi-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"hapi": "^7.5.2"
}
}
Added Note
I've not been able to find any documentation thus-far explaining this phenomena in the context of npm; though it is a bit of a no-brainer. Requiring modules with the same name within the same application would conflict with the CommonJS philosophy.
The name of your module is same as the module you are trying to install. NPM thinks that you are installing the module to itself. Change the name of your module and it will install perfectly.
Reason
Module name is same with library name
Solution
Change the module name to something else
Change 'name' in package.json
The issue can be simply explained as follows
the name of your package or module in package.json cannot be same as that of the package or module you are trying to install.
Here hapi is the name of your module and you are trying to install a module with name hapi with npm install hapi --save
This was my initial code
{
"name": "react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
which threw error
npm WARN package.json react#1.0.0 No description
npm WARN package.json react#1.0.0 No repository field.
npm WARN package.json react#1.0.0 No README data
npm WARN install Refusing to install react as a dependency of itself
then i renamed the name from react to react_app and my code looks like
{
"name": "react_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^15.6.1"
}
}
then it worked

npm install doesn't install any dependencies

I am trying to install packages in the package.json file. Unfortunately, when I run npm install, nothing happens (nothing is installed). I have used npm install on other repos and it works successfully.
Here is my path:
$PATH = /Users/me/.rbenv/shims:/Users/me/.rbenv/bin:/usr/local/share/npm/bin:/usr/local/bin:/Applications/Postgres.app/Contents/MacOS/bin:/usr/bin:/bin:/usr/sbin:/sbin
As you can see, npm/bin is in my bath and I believe this is correct.
Here are the instructions for this repo and where to run npm install (which I am doing)
go into "module"
run "npm install"
pair your laptop/pc with intelligent brick troubleshoot: http://www.ev-3.net/en/archives/97
run example.js: "node example.js"
see "example.js" for more details
When I run npm install in the module directory (that has the package.json) nothing installs.
Here is the package.json:
{
"name": "ev3-nodejs-bt",
"description": "Bt Api for lego ev3 robot",
"version": "0.0.4",
"private": false,
"dependencies": {
"serialport": "1.*"
},
"main": "Ev3.js",
"devDependencies": {"serialport": "1.*"},
"scripts": {
"test": "node Ev3.js"
},
"repository": {
"type": "git",
"url": "https://github.com/wenchaojiang/Ev3NodeJsBtAPI"
},
"keywords": [
"node.js",
"ev3",
"lego",
"robot",
"bluetooth"
],
"author": "Wenchao Jiang <psxwj#nottingham.ac.uk> (http://wenchaojames.wordpress.com/)",
"license": "MIT",
"bugs": {
"url": "https://github.com/wenchaojiang/Ev3NodeJsBtAPI/issues"
}
}
Do I have something set up wrong on my system? (I don't think I do based on my $PATH and successful installing packets in other node-js repos) Is this package.json file not valid?
npm install doesn't install (or echo) anything when all of the dependencies are satisfied. Ensure there's a serialport folder under node_modules.
If you want to reinstall everything, you just need to remove the node_modules folder:
rm -r node_modules
npm install
If you have an npm-shrinkwrap.json file, check it. The npm install command will only install the packages specified in that file.
According to the npm install docs:
If the package has a shrinkwrap file, the installation of dependencies will be driven by that.
I had the same problem with my project. And when I looked at my npm-shrinkwrap.json file, I saw dependencies: {}. So that's why it didn't install anything for me.

Resources