web_development:js:node

NPM / Node

npm init
npm init --yes

You can set default config options for the init command. For example, to set the default author email, author name, and license, on the command line, run the following commands:

> npm set init.author.email "example-user@example.com"
> npm set init.author.name "example_user"
> npm set init.license "MIT"

Random Commands

npm install grunt-cli //install prod dependency
npm install grunt-cli --save//install prod dependency
npm install grunt-cli --save-dev//install dev dependency
npm install -g grunt-cli //install globally
npm install bootstrap@3.0.0 //install certain version
npm uninstall jshint --save-dev

- uninstall a devDependency

npm list -g --depth=0

- use this to check installed packages or 'npm ls'

npm view package //package details
npm view bootstrap versions //view package versions
npm start

- usually how you start a project after git clone(1st step) or npm install(2nd step).

npm install -g npm

- how to upgrade npm

https://www.agiliq.com/blog/2019/01/using-npm-to-manage-frontend-libraries/

<script src="node_modules/jquery/dist/jquery.js"></script>

https://60devs.com/npm-install-specific-version.html

npm install lodash

- a basic example

npm install lodash@4.17.4

- IF YOU KNOW THE EXACT VERSION OF THE PACKAGE

npm install lodash@^4.0.0

- If you do not know the exact version of the package, this will install the latest 4.x.x version.

Use –save to add the installed module to the package.json’s dependencies and –save-dev to add it to devDependencies.
If you install a module without defining a specific version (i.e. without any version or using a semantic range), NPM will add the semantic range to the package.json as is. To prevent this, use –save-exact flag in addition to –save or –save-dev. This flag will force NPM to store the exact module version in the package.json.

npm outdated

- use this to check installed packages

npm update

- use this to update packages, then use 'npm outdated' and check it outputs nothing

https://docs.npmjs.com/cli/run-script

npm run-script --flags
npm run-script <command> [--silent] [-- <args>...]

As of npm@2.0.0, you can use custom arguments when executing scripts. The special option – is used by getopt to delimit the end of the options. npm will pass all the arguments after the – directly to your script:

npm run test -- --grep="pattern"

The arguments will only be passed to the script specified after npm run and not to any pre or post script.

The env script is a special built-in command that can be used to list environment variables that will be available to the script at runtime. If an “env” command is defined in your package, it will take precedence over the built-in.

You can use the –if-present flag to avoid exiting with a non-zero exit code when the script is undefined. This lets you run potentially undefined scripts without breaking the execution chain.

https://stackoverflow.com/questions/13059991/update-package-json-version-automatically
 
 
// `fs` is used instead of require to prevent caching in watch (require caches)
 
let fs = require('fs');
let semver = require('semver');
 
if (fs.existsSync('./package.json')) {
    var package = require('./package.json');
    let currentVersion = package.version;
    let type = process.argv[2];
    if (!['major', 'minor', 'patch'].includes(type)) {
        type = 'patch';
    }
 
    let newVersion = semver.inc(package.version, type);
    package.version = newVersion;
    fs.writeFileSync('./package.json', JSON.stringify(package, null, 2));
 
    console.log('Version updated', currentVersion, '=>', newVersion);
}
  • web_development/js/node.txt
  • Last modified: 2021/11/29 18:56
  • by jimboobrien