NodeJS
V8 &
libuv
NPM
NVM
JS
echo 6 > ~/.nvmrc # configure global version to install
echo 6 > ./.nvmrc # configure local version to install
nvm i # install version from .nvmrc file
nvm i 7 # install latest 7.y.z version
nvm use # use version from .nvmrc file
nvm use 7 # use latest 7.Y.Z version
nvm ls # list of installed versions
nvm ls-remote # list existing versions
nvm alias default 6 # configure the active version when opening a shell. $NVM_DIR/bash_completion # add autocompletion to your shellautocompletion
node version management
Also see awesome NPM resources and tips
npm ini # create/update the package.json file locally
npm i # install all the deps in package.json (except peer deps)
npm i --production # install all the deps in package.json (except peer and dev deps)
npm i -S lodash # install a dep and save it in package.json
npm i -D lodash # install a dep and save it in package.json (as dev dep)
npm i -SE lodash # same as npm i -S but writes exact version
npm ls # list locally installed packages
npm ls -g # list globally installed packages
npm ls lodash # list local packages having lodash as a dependency
npm uninstall lodash # uninstall lodash locally (use save options to update package.json)
npm prune # uninstall local packages not existing in package.jsonnpm run # list lifecycle and custom scripts in package.json
npm run custom # run custom script
npm run custom -- -a # same but passing option -p to the script
npm start # one of the lifecycle scripts ; they do not need "run" word
npm stop # should stop the process the previous script has started
npm test # should run tests
npm build # should run build (also called when installing or linking this modules)packages management
lifecycle and custom scripts
source <(npm completion) # add autocompletion to your shellautocompletion
See http://benediktmeurer.de/ for latest digested news about V8
Find V8 heap flags in heap.cc as FLAG_* variables
See how nodejs works additional details
node # start the repl
node -v # echo node version
node index.js # execute index.js
node --debug index.js # execute index.js with debug agent
node --debug-brk index.js # same but the script waits for you to join
node --inspect index.js # execute index.js and show the link to the live inspector
node --inspect-brk index.js # same but the script waits for you to joinThe Node REPL has few interesting commands you can list with ".help" in a REPL
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a fileYou also have access to the repl module
> repl.repl.lines
[ 'repl.repl.lines', level: [] ]
To start the Node REPL, just type (with or witout options)
> nodeRun code in V8 virtual machine
Run child processes
Fork workers in other
Implements basic event emitters
Specialized event emitters transporting buffers or objects
Wrapper for binary data
Process and env info, emitter of process events, exit
OS info
(@deprecated) Execution context across async stacks
Discuss with file system
Discuss with sockets
HTTP over sockets
using http and fs to expose a file
using commander to implement a command
using config to handle configuration
using bunyan to log things
maybe using rabbitmq
# create the data volume container
docker run -d --name nexus-data sonatype/nexus3 echo "data-only container for Nexus"
# create the nexus container using the latter
docker run -d -p 8081:8081 --name nexus --volumes-from nexus-data sonatype/nexus3
# ping till it works
curl -u admin:admin123 http://localhost:8081/service/metrics/pingnpm install --save-dev babel-cli babel-preset-env
{
"presets": [
["env", { "targets": { "node": 5 } }]
]
}
Have this .babelrc file
Install babel
"babel": "babel src --out-dir dist",
"dev": "npm run babel -- --source-maps",Have the following in package.json in "scripts" entry
npm run babelNow you can run the following build
"main": "dist/index.js",Also ensure main script points to dist entry in package.json
Have some code in src/index.js
"prepublish": "npm run babel",Have the following in package.json in "scripts" entry
"registry": "http://localhost:8081/repository/npm-private/"
Have the following in package.json in "publishConfig" entry
npm publishPublish your module
src
.npmignore
.npmrc
.babelrcHave this .npmignore file
You may have to login on this private registry !
npm i -SE <your module name>Now install
registry=http://localhost:8081/repository/npm-group/Have this .npmrc file
You know console.log
NodeJS console has some specificities
You can
node --debug=5858 index.js # 5858 is
# default portnode debug localhost:5858node index.js # pid=1234node debug -p 1234node debug index.jsVia process ID (signal to start debugger + TCP)
Via TCP-based protocol
break in index.js:7
6 const server = createServer(
7 (req, res) => {
> 8 fs.createReadStream('./package.json')
9 .pipe(res);
10 }
$ node index.js # pid 1234Starting debugger agent.
Debugger listening on 127.0.0.1:5858$ node debug -p 1234connecting to 127.0.0.1:5858 ... ok$ curl ...debug> setBreakpoint('index.js', 8)debug> repl
Press Ctrl + C to leave debug repl
>node --inspect=9229 index.js # 9229 is
# default portDebugger listening on port 9229.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/b...$ node --inspect index.jsDebugger listening on port 9229.
To start debugging,
open the following URL in Chrome:
chrome-devtools://devtools/b...$ curl ...Node commands work because we tell it to interprete it with node
#!/usr/bin/env nodeIt can be run via
myCommandnode myCommandor
node --debug myCommandor even
There you can debug commands the same as other JS files
JetBrains NodeJS plugin
JetBrains NodeJS plugin configurations
JetBrains NodeJS plugin advanced features
Execute transpiled code and break in sources
Run tests from IDE and
Run in your VM and debug in your IDE
remote node interpreter URI + path mapping = debug app run in VM
#!/usr/bin/env node
const os = require("os");
const commander = require("commander");
const omelette = require("omelette");
const completion = omelette`mycommand ${[ 'start', 'stop' ]}`;
completion.init();
commander.command("completion")
.action(() => process.stdout.write(completion.generateCompletionCode() + os.EOL));
commander.command("start")
.action(() => console.log("start"));
commander.command("stop")
.action(() => console.log("stop"));
commander.parse(process.argv);Write in ./bin/mycommand.js
const completion = omelette`mycommand ${[ 'start', 'stop' ]}`;
completion.init();commander.command("start")
.action(() => console.log("start"));
commander.command("stop")
.action(() => console.log("stop"));commander.parse(process.argv);commander.command("completion")
.action(() => process.stdout.write(completion.generateCompletionCode() + os.EOL)); "bin": {
"mycommand": "bin/mycommand.js"
},Ensure package.json contains
Publish your package
npm publishInstall the package previously written
Your command is linked in ./node_modules/.bin
./node_modules/.bin/
└── mycommand -> ../<package name>/bin/mycommand.jsnpm i -SE <package name>and accessible in package scripts
"scripts": {
"start": "mycommand start"
},and accessible in a shell
$(npm bin)/mycommand start # local install
mycommand start # global installexport PATH=$PATH:./node_modules/.binsource <(mycommand completion)Ensure local commands are accessible by name
Source the command completion