Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
global-npm-seed
Advanced tools
This is a tutorial and a template for creating your own CLI with node! You can also checkout the final npm module.
Make sure that you have node and npm installed.
Start by creating an empty directory and navigate to it in your terminal. Run npm init
which will ask you a couple of questions about the module that you are building such as name, versions, repository and author. You can use the default values for all of these, or enter some real data if you want.
Once it has all the information, it will compile it into package.json. package.json holds other data and metadata to identify and install your module.
We need to make a couple of changes to package.json to get started.
The first property to add is preferGlobal
. preferGloabl is a handy little property that tell npm this module is intended to be used from the terminal. If a user tries to install the module locally, npm will warn them that the package is intended for global installation.
{
...
"preferGlobal": "true",
...
}
The directories
property is the one that makes this all work. By setting directories.bin
, we are telling npm which file or directory contains the files to be added to the system PATH.
For this demo, we want users to be able to run hello
from the terminal, so I need to create a new directory bin and create a file called hello in it. Then add the directories
property to package.json. Now when our module is installed the user will be able to access ./bin/hello from the terminal.
{
...
"preferGlobal": "true",
"directories": {
"bin": "./bin"
},
...
}
Create a directory called lib in your modules folder, and create hello.js inside of it. This file is going to contain all of our functionality. For now, it will display "Hello World!".
console.log('Hello World!');
We have our file ready to go, we still need to link the terminal and this file by modifying ./bin/hello
This is the file that is going to be executed by our terminal. We need it to do two things:
#!/usr/bin/env node
require('./../lib/hello.js');
Line one is a shebang. Line two is a require statement that requires our main file which will be executed. We now have all the pieces in place to test our CLI.
Navigate to your projects root directory, and exectute:
npm install -g ./
This is telling npm to install the module location in the current directory as a global module, which is exactly what we want. -g
is a flag that specifies that the module should be installed globally.
Now you can test the module by running
> hello
Hello World!
Our module is cool, but lets add a optional command-line argument to personalize the greeting. Adding support for --name=myname will do just that.
To do this, we are going to add a dependency to package.json. You can run npm install --save optimist
to install the module and add it as a dependency.
{
...
"dependencies": {
"optimist": "~0.3.5"
}
...
}
Optimist is a node module that helps parse command-line arguments. Now that it is included in out dependencies, npm will install optimist whenever somebody installs our module.
In our main file, we now need to parse the command-line arguments.
var argv = require('optimist').argv;
conosle.log('Hello ' + (argv.name || 'World') + '!');
Now we check to see if the user provided a name. If they did, we greet them with their name, else we greet them with our generic message.
Run npm install -g ./
to reinstall the module. This time, optimist is also going to be installed. Now you should be able to run hello with the name argument.
> hello
Hello World!
> hello --name=Andrew
Hello Andrew!
If you have a question, open an issue. I would love to help you setup your first command-line interface!
FAQs
An npm seed project for a command line interface
We found that global-npm-seed demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.