0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

node

Last updated at Posted at 2025-10-19

nodeのインストール確認

     node -v

package.jsonを作成する

    npm init

と入力するとカレントディレクトリ(今いるディレクトリ)でnode.jsのプロジェクトを作るという意味になる

    user@TMCITMRNTM1039 test % npm init
    This utility will walk you through creating a package.json file.
    It only covers the most common items, and tries to guess sensible defaults.
    
    See `npm help init` for definitive documentation on these fields
    and exactly what they do.
    
    Use `npm install <pkg>` afterwards to install a package and
    save it as a dependency in the package.json file.
    
    Press ^C at any time to quit.
    package name: (test) 
  • このツールは、package.jsonファイルを作成する手順を案内します

  • 最も一般的な項目だけを扱い、妥当と思われる初期値を自動で設定します

  • npm install <パッケージ名>を使うことで、外部パッケージをインストールし

  • 依存関係として、package.jsonに記録します

  • いつでもCtrl + Cで終了できます

  • デフォルトのパッケージネームはtest(ディレクトリ名がtestなので)
    npm init =対話形式で設定 npm init -y = 全てデフォルト値設定(yes ot all)の意味

  • [ ] description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC) About to write to /Users/user/Desktop/test/package.json:

      {
        "name": "test",
        "version": "1.0.0",
        "description": "",
        "main": "index.js",
        "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
        },
        "author": "",
        "license": "ISC"
      }
      
      
      Is this OK? (yes) 
    

-npm initの場合でも、全てenterでOK
package.jsonファイルが生成される

  • srcにindex.jsファイルを作成し
    console.log("Hello Node.js!");
    と入力する

-package.jsonのscriptsにstartを追加して、index.jsを実行できる様にする

package.jsonファイルを開き
"scripts": {
"start" : "node src/index.js"
}

ターミナル上でnode ファイル名するとファイルを実行してくれる

  • npmコマンドを実行してmocha,chai@4をインストールする
  • chai@4の様に、バージョンを指定してインストールできる
  • 今回は、devDependencies(開発環境)にインストールしたいので
    json npm install mocha chai@4 --save-dev
    もしくは
    npm install mocha chai@4 -D
    結果として
devDependencies": {
"chai": "^4.5.0",
"mocha": "^11.7.4",
}
  • npm run testmochaを実行できるよう、package.jsonのscriptsを修正する
  • mochaは、実行可能スクリプトとして登録されているので、node ファイルのようにしなくても実行してくれる
"scripts": {
"test": "mocha",
}

mochaはテスト実行ツール(テストフレームワーク)

  • prettierをインストールしてみる
  • 不揃いなコードを、src/index.jsに追加してみてprettierで修正して見る
  • ルートディレクトリ(一番上のディレクトリ)にprettierrcファイルを作成し、以下を追加
{
  "trailingComma": "es5",
  "tabWidth": 2,
  "semi": true,
  "singleQuote": true
}
  • package.jsonのscriptsを修正、prettierを実行できる様にする
"scripts": {
  "format": "prettier --write ."
}
  • npm run formatを実行してsrc/index.jsファイルがフォーマットされることを確認

  • requireとmodule.exports

  • export.jsに過去のコードを追加、require.jsで実行できる様エクスポトする
    function sayHello() { console.log("hello from export.js"); }

  • npm run requireでexport.jsからエクスポートしたコードをrequire.jsで実行できるよう、package.jsonのscriptsを修正する

**export.js**
function sayHello() { console.log('hello from export.js'); } module.exports = {sayHello};

**require.js**
const{sayHello} =require('./export.js);

  • 他にもexport,requireして見る
  • `function sayHello() {
    console.log('hello from export.js');
    }

const num = 10;
function callNum(num) {
console.log(num);
}
module.exports = { sayHello, num, callNum };`
オブジェクトは、キーと値が同じ場合、値を省略できる

  • require.js
    `const { num, callNum, sayHello } = require('./export.js');

sayHello();
console.log(num + 1);
callNum(num);`

分割代入は名前に紐づいて代入されるので、受け口のオブジェクトのキー名が違うと代入できない

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?