Writing to files in Node.js

There are a lot of details in the File System API. The most common way is: const fs = require(‘fs’); fs.writeFile(“/tmp/test”, “Hey there!”, function(err) { if(err) { return console.log(err); } console.log(“The file was saved!”); }); // Or fs.writeFileSync(‘/tmp/test-sync’, ‘Hey there!’);

How can I update NodeJS and NPM to their latest versions?

Use: npm update -g npm See the docs for the update command: npm update [-g] [<pkg>…] This command will update all the packages listed to the latest version (specified by the tag config), respecting semver. Additionally, see the documentation on Node.js and NPM installation and Upgrading NPM. The following original answer is from the old … Read more

How to exit in Node.js

Call the global process object’s exit method: process.exit() From the docs: process.exit([exitcode]) Ends the process with the specified code. If omitted, exit with a ‘success’ code 0. To exit with a ‘failure’ code: process.exit(1); The shell that executed node should see the exit code as 1.

Do I commit the package-lock.json file created by npm 5?

Yes, package-lock.json is intended to be checked into source control. If you’re using npm 5+, you may see this notice on the command line: created a lockfile as package-lock.json. You should commit this file. According to npm help package-lock.json: package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. … Read more

How to update each dependency in package.json to the latest version?

Looks like npm-check-updates is the only way to make this happen now. npm i -g npm-check-updates ncu -u npm install On npm <3.11: Simply change every dependency’s version to *, then run npm update –save. (Note: broken in recent (3.11) versions of npm). Before: “dependencies”: { “express”: “*”, “mongodb”: “*”, “underscore”: “*”, “rjs”: “*”, “jade”: … Read more

What’s the difference between dependencies, devDependencies and peerDependencies in npm package.json file?

Summary of important behavior differences: dependencies are installed on both: npm install from a directory that contains package.json npm install $package on any other directory devDependencies are: also installed on npm install on a directory that contains package.json, unless you pass the –production flag (go upvote Gayan Charith’s answer), or if the NODE_ENV=production environment variable … Read more

Find the version of an installed npm package

Use npm list for local packages or npm list -g for globally installed packages. You can find the version of a specific package by passing its name as an argument. For example, npm list grunt will result in: projectName@projectVersion /path/to/project/folder └── grunt@0.4.1 Alternatively, you can just run npm list without passing a package name as … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)