Install the Node.js SDK

Updated at:

Before you can use the Node.js SDK to perform operations in Simple Log Service, you must install the SDK. This topic describes how to install the SDK.

Prerequisites

Notes

The Simple Log Service Node.js SDK is developed in JavaScript and does not support TypeScript (TS).

Install the SDK

  1. Create a project folder and navigate to the folder.

  2. Run the following command to initialize the project.

    npm init

    Enter sls_node for the package name and accept the default values for the other parameters. After the initialization is complete, a package.json file is automatically created. The following code shows an example of the file content:

    {
      "name": "sls_node",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
  3. Run the following command to install the Node.js SDK.

    npm install @alicloud/log

    If you encounter network issues when you use npm, you can use the npm mirror provided by Taobao.

    After the command is run, information about @alicloud/log is added to the package.json file. The following code shows an example of the file content:

    {
      "name": "sls_node",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "@alicloud/log": "^1.2.5"
      }
    }
  4. Set up the project. This topic uses Express as an example.

    1. Run the following command to install Express.

      npm install express

      For more information, see Install Express.

    2. Run the following command to install morgan.

      npm install morgan

      For more information, see morgan.

    3. Create an app.js file and add the following code to the file.

      var express = require('express')
      var morgan = require('morgan')
      var app = express()
      const logger = morgan(function (tokens, req, res) {
        return [
          tokens.method(req, res),
          tokens.url(req, res),
          tokens.status(req, res),
          tokens.res(req, res, 'content-length'), '-',
          tokens['response-time'](req, res), 'ms'
        ].join(' ')
      })
      app.use(logger)
      app.get('/', (req, res) => res.send('Hello World!'))
      app.listen(3000, () => console.log('Example app listening on port 3000!'))
    4. Run the following command to start the project.

      node app.js

    The following result is returned.

    Example app listening on port 3000!

FAQ

Cannot find the ../xx/jsSHA/src/sha.js file

This error occurs if a file from a dependent module is deleted. To resolve this issue, run the npm install aliyun-sdk command to reinstall the module. The missing file is automatically retrieved during the reinstallation.

What to do next

Node.js SDK Quick Start