【転載】NodeJS学習ノート

5507 ワード

References:
https://segmentfault.com/a/1190000017193008https://www.nodebeginner.org/#serving-somethings-usefulhttps://www.npmjs.com.cn/
コンテント:
Create a Node.js Module and Use it locally,refer to  https://segmentfault.com/a/1190000017193008
How to create a Node.js Module
  • Create a new directory and enter it.   E.g./d/sayHello World
  • Init your Node.js module.  Run command「npm init」to set your package.json file.    NOTE:npm init-y
  • Create yourntry point            e.g. /d/sayHello World/index.js
  • module.exports = {
        say() {
            console.log('Hello World!');
        }
    }
    How to user your Node.js Module locally
  • Create a new directory and enter it.
  • Install the Node.js module that you created before
  • Enter into the Node shell interface,and test if your Node.js Module is available to use
  • node
    > var test = require([Node.js Module Name])   // e.g. var test = require('sayhelloworld')
    > test.say()
    The Node Beginner Book,refer tohttps://www.nodebeginner.org/#serving-somethingn-useful
    Following was referred tohttps://www.npmjs.com.cn/
    Install package locally
  • Go to the directory that you want to place the package
  • Run npm install[package name]
  • Update package to local
  • Go to the directory which package.json file was.
  • Run npm udate
  • Run npm outdated、shoutput.
  • ユニスタンルpackage in local
  • Go to node_modules directory.
  • Run npm uninstall[package name]
  • Run npm uninstall--save[package name]
  • Run npm uninstall--save-dev[package name]
  • Install package globally
  • Run npm install-g[package name]
  • Update package globally
  • Run npm udate-g[package name]
  • Update all package s by running npm udate-g               
  • Find out which packages need to be udated by running npm outdated-g--depth=0
  • ユニスターパッケージglobally
  • Run npm uninstall-g[package name]
  • Node.jsテンプレートの作成方法     --> see part 1 above.
    How to work with scoped package.
  • If a package's name begins with@,then it is a scoped package.The scope is everthing in between the@and the slash.      @scope/project-name
  • Each npm user has their own scope:   @username/project-name
  • Initialize a scoped package
  • Publishing a scoped package
  • Using a scoped package
  • In package.json   {「dependencies」:「@username/project-name」:「^1.00」}
  • On command line:      npm install@username/project-name--save
  • In require statement:   var project Name=require(「@username/project-name」)
  • Understanding package and modules
  • A package is a file or directory that is decribed by a package.json.This can happen in a bunch of different ways
  • A module is any file or directory that can be loaded by Node.js'require().Again,there re re several configrations that allow this to happen.
  • What is a package?A package is any of the follwing:
  • A folder containing a program described by a package.json file.
  • A gzippped tall containing(a).
  • A url that resoves to(b).
  • A@that is published on the registry with(c).
  • A@that points to(d).
  • A that has a latest.
  • A git url that,when cloned,reults in(a).
  • Noting all these package possibility,it follows that even if you never Problish your package to the public registry,you can still get a lot of benefits of using npm:
  • If you just want to write a node program,and/or,
  • If you also want to be able to ily install it elsewhere after packing it up into a taball.
  • What is a module
    A module is anything that can be loaded with require()in a Node.js program.The following wing are all examples of things can be loaded as modules:
  • A folder with a package.json file containing a main field.
  • A folder with an index.js file in it.
  • A JavaScript file.
  • Most npm packages are modules.
  • Generaally,npm package s that are used in Node.js program are loaded with require,making them modules.However,there'sのrequirement that an npm package be module.
  • Some packages、e.g.cli package s、only contain an executeable command interface and don't provide a mail d for use.js programs.The package e e e e e e e e e e not modules.
  • Almost all npm packages(at least、those that are Node programs)contain many modules within(because evere file they load with require()is a module)
  • In the context of a Node program,the module is also the thing that was loaded from a file.For example,in the follwing program:   var req=require('request')
  • We might say that「The variable refers to the request module」
  • File and Directory Names in the Node.js and npm Ecosystem
  • So,why is it the node_modules folder、but package.json file?Why not node_packages or module.json?
  • The package.json file defines the package.
  • The node_modules folder is the place Node.js look s for modules.
  • For example,if You create a file at node_modules/foo.js and then had a program that dididi var f=require('foo.js')、it would load the module.However、foo.js is not a“package”in this case beuse it have not have a package.jage.json
  • Alternative ely,if You create a package which does not have an index.js or a“main”field in the packge.json file,then it is not a module.Even if it's installed in node_modules,it can't be an argment to require().