DEV Community

ChuangWANG
ChuangWANG

Posted on

Install an npm module demo

@izaacmendes - require() or import depending upon whether you're using ESM modules or CommonJS modules. If you have properly prepared your package.json file to contain your dependent modules, then you can just do npm install with no other arguments in the directory where you package.json is located and npm will fetch all the dependent modules and place them in the node_modules directory. See the --save-xxxx options for NPM for how to save your dependencies to your package.json file if you aren't already doing that. – jfriend00 Aug 7 '20 at 4:42

mathjs demo

npm init -y
npm install mathjs
Enter fullscreen mode Exit fullscreen mode

start.js

let test = require('mathjs')
let result = test.round(3.12484, 3)               
console.log(result)
Enter fullscreen mode Exit fullscreen mode

then run the command in cmd

npm run start
Enter fullscreen mode Exit fullscreen mode

the output is below

PS D:\save\code-rep\feature\npm> npm run start
Debugger attached.

> npm@1.0.0 start D:\save\code-rep\feature\npm
> node start.js

Debugger attached.
3.125
Waiting for the debugger to disconnect...
Waiting for the debugger to disconnect...
PS D:\save\code-rep\feature\npm>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)