npm init or npx create-react-app has become a familiar way of starting work on almost any JavaScript project. It can be a server, client, or even a desktop application.npm init .
npm install . Short version: npm i .npm test . Short version: npm t .npm --help . Short version: npm -h .--global . Shortcut: -g .--save-dev . Shortcut: -D .npm init installations by default. The usual option is npm init --yes or npm init --force . The abbreviated version is npm init -y or npm init -f .--save or -S flag. Packages are saved by default. And in order to install the package without saving it, you can use the --no-save flag.--save-optional . Shortcut: -O .--save-exact . Shortcut: -E .npm install command with the --save-bundle , or -B switch --save-bundle package.json entry for installed packages to appear in the bundledDependencies section. These packages will be packaged with the project when it is published. To create a tarball file containing project files and packages listed in bundledDependencies , you can use the npm pack command.. ) Is used to represent the root directory of the application, or (depending on the context) to represent the entry point to the application. In npm, this is what is set as the value of the main property in the package.json file: { "main": "index.js" } npx create-react-app . So, instead of running this command as npx create-react-app my-app (which will create a new my-app folder), you can run this command like this: npx create-react-app . (pay attention to the point that comes after the command). This will allow you to create a template React-application project in the folder in which you are at the time you start the command.npm init command to create a new project, you will most likely find that you enter the same data over and over in response to questions from the system. For example - it is quite likely that you are the author of most of the projects you create. In order to save time on entering the same data, you can set your own default values ​​for the corresponding fields: npm config set init.author.name "Joe Bloggs" npm config set init.author.email "joebloggs@gmail.com" npm config set init.author.url "joebloggs.com" npm config set init.license "MIT" npm config edit command. This will open the configuration file in the system editor. If you want to edit the global npm parameters, use the npm config edit -g command. echo "" > $(npm config get userconfig) npm config edit echo "" > $(npm config get globalconfig) npm config --global edit npm i -D cross-env command. Then you need to put a cross-env in front of each environment variable. For example - it might look like this: { "scripts": { "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js" } cross-env package is the most convenient tool for achieving cross-platform script compatibility. But it will be useful for you to take a look at the following two popular tools that can help in this task:&& construct is used. What about parallel scripts running? To achieve this goal, you can select the appropriate npm-package. The most popular packages of this kind are concurrently and npm-run-all . Here we will demonstrate the use of the package concurrently.npm i -D concurrently . Then in package.json you can use the following construction: { "start": "concurrently \"command1 arg\" \"command2 arg\"" } package.json files located in different directories. It would be convenient, for example, to run the scripts declared in them, being in the root directory of the project. This is much better than having to travel to different folders whenever you need to run a certain script. In order to do this, you can use the following two approaches.cd to organize an automatic transition to the desired directory. It might look something like this: cd folder && npm start && cd .. --prefix flag, with which you can specify the path: npm start --prefix path/to/your/folder npm start for the client and server parts of the project. Their code, respectively, is located in the client and server folders. "start": "concurrently \"(npm start --prefix client)\" \"(npm start --prefix server)\"", concurrently , simultaneously launches the presentation layer of the application and the Electron window. But using wait-on you can make the Electron window open only when the React presentation layer is ready for work and becomes available at http://localhost:3000 . This is what the wait-on usage looks like: "dev": "concurrently \"cross-env BROWSER=none npm run start\" \"wait-on http://localhost:3000 && electron .\"", BROWSER=none using the BROWSER=none environment variable, in front of which, for the sake of cross-platform compatibility of the solution, is the cross-env command.package.json , it is enough to go to the root directory of the project and execute the npm run command in the terminal.ntl module (Npm Task List): npm i -g ntl ntl command. It will display a list of available scripts and allow you to select the script that you want to run. Here's what it looks like.
prebuild and postbuild scripts that allow you to run some code before and after running the build script. Such pre-and post-scripts can be defined for any other script. This includes those descriptions that the programmer himself adds to p ackage.json .npm . In order to increase the corresponding part of the application version number, you can use the npm version command, followed by major , minor or patch : // 1.0.0 npm version patch // 1.0.1 npm version minor // 1.1.0 npm version major // 2.0.0 { "predeploy": "npm version patch" } package.json , allowing you to create your own abbreviated versions of commands. Install the json package globally: npm install -g json json utility to quickly edit a file with the -I . For example, to add a new foo script with a value of bar to a file, you can use the following command: json -I -f package.json -e 'this.scripts.foo="bar"' json utility.package.json file has a "repository" entry, this means that you can open the repository page in the default browser used in the system. To do this, use the npm repo command.git command line utility installed, this means that you can find out the address of your repository using this command: git config --get remote.origin.url json , you have installed the json utility, then you can use the following script to automatically add the correct information about the repository to package.json : json -I -f package.json -e "this.repository=\"$(git config --get remote.origin.url)\"" npm init command, which accepts the URL of the GitHub repository and automatically sends the first commit to it. Here we will talk about how to create such scripts. And in the next section, which will be our last tip, we'll talk about working with git .npm init command using the .npm-init.js . Create such a file in the current user's home directory (on Windows, this is usually C:/Users/<username> , and on Mac it is /Users/<username> ). After that, run the following command, which tells npm where this file is located: npm config set init-module ~\.npm-init.js git , let's look at a simple example of the .npm-init.js , which reproduces the questions that the system asks the user when using the npm init command without additional settings: module.exports = { name: prompt('package name', basename || package.name), version: prompt('version', '0.0.0'), decription: prompt('description', ''), main: prompt('entry point', 'index.js'), repository: prompt('git repository', ''), keywords: prompt(function (s) { return s.split(/\s+/) }), author: prompt('author', 'Joe Bloggs <joe.bloggs@gmail.com> (joebloggs.com)'), license: prompt('license', 'ISC') } nameInPackage: prompt('nameInPrompt', 'defaultValue') prompt method.npm init settings, simply delete the .npm-init.js .git commands in the .npm-init.js we need to find a way to work with the command line. To do this, you can use the child_process module. We connect it at the top of the file, and since we only need the execSync function, we only import it using restructuring: const { execSync } = require('child_process'); function run(func) { console.log(execSync(func).toString()) } README.md file and send the first commit to the repository.module.exports object of the .npm-init.js should be the following code: repository: prompt('github repository url', '', function (url) { if (url) { run('touch README.md'); run('git init'); run('git add README.md'); run('git commit -m "first commit"'); run(`git remote add origin ${url}`); run('git push -u origin master'); return url; }) .npm-init.js file should look after this addition: const { execSync } = require('child_process'); function run(func) { console.log(execSync(func).toString()) } module.exports = { name: prompt('package name', basename || package.name), version: prompt('version', '0.0.0'), decription: prompt('description', ''), main: prompt('entry point', 'index.js'), keywords: prompt(function (s) { return s.split(/\s+/) }), author: prompt('author', 'Joe Bloggs <joe.bloggs@gmail.com> (joebloggs.com)'), license: prompt('license', 'ISC'), repository: prompt('github repository url', '', function (url) { if (url) { run('touch README.md'); run('git init'); run('git add README.md'); run('git commit -m "first commit"'); run(`git remote add origin ${url}`); run('git push -u origin master'); return url; }), } package.json file that the system creates using this .npm-init.js : { "name": "Custom npm init", "version": "0.0.0", "decription": "A test project, to demonstrate a custom npm init script.", "main": "index.js", "keywords": [], "author": "Joe Bloggs <joe.bloggs@gmail.com> (joebloggs.com)", "license": "ISC", "repository": { "type": "git", "url": "git+https://github.com/JoeBloggs/custom.git" }, "bugs": { "url": "https://github.com/JoeBloggs/custom/issues" }, "homepage": "https://github.com/JoeBloggs/custom#readme" } package.json , or setting up npm init according to your needs.
Source: https://habr.com/ru/post/458504/
All Articles