In this guide, you will learn how to build and run your own command.
In your favorite editor, create a hello-world.js
file.
function helloWorld () {console.log('Hello World');}// expose the function to be required latermodule.exports = helloWorld;
To run the script file, navigate in the right folder using cd()
and ls()
.
> ls()[ 'hello-world.js' ]> hello = require(path.resolve('hello-world.js'))[Function: helloWorld]> hello()Hello Worldundefined
VoilĂ , the script prints Hello World
in the stdout. Notice undefined
, which is the returned value of the function.
In many cases, scripts are released in a package. A package is a file or directory that is described by a package.json
file. This file gives information about the package itself such as its name, its authors, its version but also how to interact with it, how to publish it, etc. More information about how to create a Node.js module here.
To run a script from a package, you must have it installed at a global level or in the current working directory.
> $('npm install --global hellonacre')'\nadded 1 package, and audited 2 packages in 430ms\n\nfound 0 vulnerabilities\n'> hellonacre()Hello Worldundefined
// init package.json with empty config> cat.append('package.json', '{}')'{}'// install hellonacre package in the working directory> $('npm install hellonacre')'\n' +'added 1 package, and audited 3 packages in 1s\n' +'\n' +'1 package is looking for funding\n' +' run `npm fund` for details\n' +'\n' +'found 0 vulnerabilities\n'// check if the package is present in node_modules folder> ls('node_modules/')[ 'node_modules/.package-lock.json', 'node_modules/hellonacre' ]> hellonacre()Hello Worldundefined