NodeJS

From Wildsong
(Redirected from Node)
Jump to navigationJump to search

NodeJS on Windows

The easiest way is using nvm, I think. I used to use chocolatey.

Go to https://github.com/nvm-sh/nvm and read.

nvm install --lts

Stuff I wish I had known a few weeks ago

Shortcut to npm docs https://docs.npmjs.com/cli-documentation/misc

Useful commands

  • npm ls - shows me everything in its tree, there are switches to filter
  • npm run - shows me what's in package.json scripts
  • npm version minor - bump the minor version number in package.json and run git tag version (Whoo whoo coolness!!), run the preversion, version scripts
  • npm version patch - bump the patch number instead

package.json scripts

There about 20 predefined, the complete list and what they mean is here. Additionally if that's not enough you can define your own and use npm run scriptname.

I have been using

  • start : I typically use this to run my app in a browser with "start: parcel serve index.html --open"
  • test : should be unit tests, I should really learn how to use unit tests with Node. :-)

I need to learn more about using npm for deployment. I used to use a "build" target to run parcel to build the app for deployment in build/ Then I'd sync the build/ files to the server. Well actually I still do that because I have not learned a better way yet.

Setting a browser

I usually use Chrome on Windows and Firefox on Mac. Instead of wiring a browser into my package.json 'start' command with "--open chrome", I now use just "--open" and let it pick. I can set the browser with

npm config set browser {chrome|firefox|lynx}

Reading docs

npm docs packagename

This jumps to a browser and opens the relevant site for packagename. For example, "npm docs react" opens https://reactjs.org. If you publish your own project as a package then it takes you to the npmjs.com site page.

Publishing a module

I read about it in the "Node Cookbook, 3rd edition" (from Packt).

I have done this before so some things are already set up. I checked my configuration with npm config get and npm config ls -l. The first command showed me that my init-author, init-email, and scope are all set the way I want them.

Everything I do has a map in it, so let's say I want to create a React component that wraps an OpenLayers map so that all I have to do to get a slippy OpenStreetMap map into my app is this:

import React from 'react'
import {render} from 'react-dom'
import {Map} from 'simplemap'
const App = () => {
  return (
    <>
       <h1>This is a map.</h1>
       <Map/>
    </>
  )
}
render(<App/>, document.getElementByid("app"));

(And I put the requisite index.html, package.json together and [publish it all on github here.])