I developed small application.
I did this to install developed module globally:
npm install . -g
I got this response:
$ npm install -g .
update-deps-dev#1.0.0 C:\Users\Vladislav.Sharikov\AppData\Roaming\npm\node_modul
es\update-deps-dev
I got my module in place, where other globally installed modules are stored. Also, my module is available by:
npm ls -g --depth=0
Now, I want to start this module from any place on my pc. How should I do this?
I try to run, but getting this:
Vladislav.Sharikov#PC /D/Dev
$ update-deps-dev
sh: update-deps-dev: command not found
I am using Windows 7 x64 + Git Bash.
How should I start my globally installed node application?
My package.json file contents:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Not sure about git bash. But in a cross platform way you can simply define a bin field on your package json to tell npm to generate a binary available on your command line later. It really works fine on windows, mac, linux.
Note that it is a per user binary. If you are working on linux it won t install it into /usr/bin, but depending on your system, more probably ~/node_modules/bin.
Suppose your binary is defined into bin.js file, you just need to add a new Object field bin, and foreach bin you want to define a key, the name of the binary, and its value must point to the path of your bin file relative to the package file:
{
"name": "update-deps-dev",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"update-deps-dev": "./bin.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"adm-zip": "^0.4.7",
"fs": "0.0.2",
"https": "^1.0.0",
"q": "^1.4.1",
"shelljs": "^0.7.0"
}
}
Read also,
https://docs.npmjs.com/files/package.json#bin
https://docs.npmjs.com/cli/bin
Related
Today, I'm coding a few npm packages and a few things that need to be prepared repeatedly.
So I wanted to code a CLI to get those things done quickly.
Here is the src/cli.js code:
export function cli(args){
console.log(args);
}
Here is the package.json code:
{
"name": "my-project",
"version": "1.0.0",
"description": "A CLI to bootstrap new project",
"main": "src/index.js",
"bin": {
"#kensoni/my-project": "bin/my-project",
"my-project": "bin/my-project"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"cli"
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Ken Nguyen",
"license": "MIT",
"dependencies": {
"arg": "^5.0.0",
"esm": "^3.2.25",
"inquirer": "^8.1.1"
}
}
Here is the bin/my-project code:
#!/usr/bin/env/ node
require = require('esm')(module /*, options*/);
require('../src/cli').cli(process.argv);
After I execute the command npm link and open a new cmd type my-project, I get the following message:
'"/"' is not recognized as an internal or external command,
operable program or batch file.
I am using these versions:
node: 14.17.1
npm: 7.18.1
Any ideas how it might work.
Thanks in advance.
Remove "/" after env
#!/usr/bin/env node
//...
I am trying to reduce size of nodejs lambda bundle which uses aws-sdk.
This is the original lambda package.json file:
{
"name": "lambdanodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.784.0",
"bluebird": "^3.7.2",
"ioredis": "^4.19.2",
"redis": "^3.0.2",
"redis-clustr": "^1.7.0"
}
}
Overall size is 57MB, 54 belongs to aws-sdk.
To reduce size I tried using specific client services (v3 sdk).
Followed : https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-secrets-manager/package.json
{
"name": "lambdanodejs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"#aws-sdk/client-dynamodb": "^3.7.0",
"#aws-sdk/client-secrets-manager": "^3.7.0",
"bluebird": "^3.7.2",
"ioredis": "^4.19.2",
"redis": "^3.0.2",
"redis-clustr": "^1.7.0"
}
}
Now npm install results in even larger size around 190+MBs.
Also in node_modules I see lot of directories which were not there when using previous package.json install.
This v3 aws-sdk is supposed to be lighter.
Am I missing something?
Thanks!
Don't push aws-sdk to the lambda function. If you want to use it in the local development environment install it globally.
Lambda will provide all necessary SDK modules(v2 & v3) during runtime, just import them in the code.
Don't bundle the aws-sdk in the zip.
If your zip size is getting increased, use layers in lambda.
Import the node_moudles to lambda layers.
Then upload your code to lambda.
The package.json is:
{
"scripts": {
"test": "find ./js/tests -name '*.test.js' | xargs mocha -R spec",
"start": "node ./scores.js",
"run": "node ./scores.js"
},
"dependencies": {
"aws-sdk": "^2.734.0",
"body-parser": "^1.19.0",
..
"dotenv": "^8.2.0"
},
"name": "scores",
"version": "1.0.0",
"main": "",
"repository": {
"type": "git",
"url": "something"
}
}
npm install has been done and the package-lock.json created. Why are either npm run or npm run-script working?
$npm run-script ./scores.js
npm ERR! missing script: ./scores.js
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/steve/.npm/_logs/2021-03-01T23_01_56_745Z-debug.log
15:01:56/scores $ls -l scores.js
-rw-r--r-- 1 steve staff 21417 Feb 25 06:24 scores.js
I have identically structured package.json and scripts in other sibling directories that do work e.g. the following works via cd ../keys_server; npm run keys_server
$cat ../keys_server/package.json
{
"scripts": {
"test": "find ./js/tests -name '*.test.js' | xargs mocha -R spec",
"keys_server": "node ./keys_server.js",
"start": "node ./keys_server.js"
},
"dependencies": {
"aws-sdk": "^2.734.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
..
"dotenv": "^8.2.0"
},
"name": "keys_server",
"version": "1.0.0",
"main": "keys_server.js",
"repository": {
"type": "git",
"url": "something"
},
Update #pkumar noticed that the main entry was left empty. I now updated it to :
"main": "scores.js",
and then tried both
npm run-script ./scores.js and npm run scores and npm run scores.js. However none of them work:
$npm run scores
npm ERR! missing script: scores
The 'main' field is empty in package.json. try adding scores.js there and see if it works
The 'main' field is supposed to be the entry point of the server: https://docs.npmjs.com/cli/v7/configuring-npm/package-json
EDIT: The actual reason it's not working is because "scores" had not been defined under the scripts section. After adding that definition it should work
"scripts": {
..
"scores": "node ./scores.js"
}
For some reason, electron-packager can't find the module electron. I have installed it as instructed from the official site, yet it doesn't seem to work. Here is what seems to happen, my package.json, and how I run electron-packager
Package.json
{
"name": "electron-part1",
"version": "1.0.0",
"description": "An electron tutorial",
"main": "index.js",
"jquery": "^3.3.1",
"scripts": {
"start": "electron .",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "ISC",
"devDependencies": {
"electron": "^5.0.3",
"electron-packager": "^8.7.2"
},
"dependencies": {
"custom-electron-titlebar": "^3.2.5",
"jquery": "^3.5.1"
}
}
What happens:
How I call electron-packager:
electron-packager . electron-part1 --platform=win32 --arch=x64
I fixed it! If anyone else is having the same problem, copy electron from %USERPROFILE%\AppData\Roaming\npm\node_modules, and paste it to node modules in your main folder (The one with package.json) then you can package it as normal
I don't have much, but npm install gives me so many other packages I don't need
"dependencies": {
"require-dir": "^0.3.2"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-concat": "^2.6.1",
"gulp-rename": "^1.2.2",
"gulp-uglify": "^3.0.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": ""
},
"author": "",
"license": "ISC"
}
When you use npm to install your gulp dependencies, you also install the dependencies of your dependencies. Dependency graphs are complex, even if you think you only use a few packages explicitly, those packages could be standing on the shoulders of a vast amount of libraries.
Follow-up:
Guide to NPM