Setup NodeJS ... Centos 7

How to install NodeJS?

 

 

yarn -v
 node -v
  npm -v

npm i -g forever (

npm install forever -g

)

cd /var/www/html

nano demo_server.js

node --inspect demo_server.js

forever --version

forever start demo_server.js

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

nvm ls

npm install forever-monitor

forever start app.js

 

nvm install 13.14.0

test the fault tolerance of forever: (https://github.com/foreversd/forever-monitor/tree/master/examples)

forever -m 5 examples/error-on-timer.js

 

 

For new installations we encourage you to use pm2 or nodemon

$ npm install pm2@latest -g
# or
$ yarn global add pm2

 

 

npm install -g nodemon

 

nodemon [your node app]

 

 

  1. yum install -y gcc-c++ make
  2. curl -sL https://rpm.nodesource.com/setup_12.x | sudo -E bash -
     DEPRECATION WARNING
      Node.js 12.x is no longer actively supported!
      You will not receive security or critical stability updates for this version.
      You should migrate to a supported version of Node.js as soon as possible.
      Use the installation script that corresponds to the version of Node.js you
      wish to install. e.g.
       * https://rpm.nodesource.com/setup_14.x - Node.js v14 LTS "Fermium" (recommended)
       * https://rpm.nodesource.com/setup_16.x - Node.js v16 "Gallium"
       * https://rpm.nodesource.com/setup_18.x - Node.js v18 "Eighteen" (current)
      Please see https://github.com/nodejs/Release for details about which
      version may be appropriate for you.
      The NodeSource Node.js distributions repository contains
      information both about supported versions of Node.js and supported Linux
      distributions. To learn more about usage, see the repository:
        https://github.com/nodesource/distributions
  3. curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
  4. yum install nodejs
    To install the Yarn package manager, run:
         curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
         sudo yum install yarn
  5. npm install yarn -g
  6. yarn -v
    1.22.19
  7.  node -v
    v14.21.3
  8. npm -v
    6.14.18
  9. cd /var/www/html
  10. nano demo_server.js
    var http = require('http');
    http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Welcome Node.js');
    }).listen(3001, "127.0.0.1");
    console.log('Server running at http://127.0.0.1:3001/');
  11.  node --inspect demo_server.js
    Debugger listening on ws://127.0.0.1:9229/18403e89-80cc-4432-b35e-32be08600554
    For help, see: https://nodejs.org/en/docs/inspector
    Server running at http://127.0.0.1:3001/
  12. npm i -g forever
    + forever@4.0.3
  13. forever --version
    v4.0.3
    (node:13935) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
    (Use `node --trace-warnings ...` to show where the warning was created)
  14. curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
  15. nvm ls-remote
  16. nvm install 12.22.12
  17. forever --version
    now the error has gone
  18. nvm install 13.14.0
  19. nvm run default --version
  20. npm install forever-monitor
 
 
working version, 2023
 

Step 1 – Add Node.js Yum Repository

First of all, You need to enable the node.js yum repository in your system provided by the Node.js official website. You also need development tools to build native add-ons to be installed on your system.

For Stable Release:-

yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_16.x | sudo -E bash -

Step 2 – Install Node.js on CentOS

After adding a yum repository to your system let’s install the Node.js package. NPM will also be installed with node.js. This command will also install many other dependent packages on your system.

yum install nodejs

Step 3 – Check Node.js and NPM Version

After installing node.js verify and check the installed version. You can find more details about current version on node.js official website.

node -v v18.7.0

Also, check the version of npm.

npm -v7.13.0

You have successfully installed Node.js on your CentOS 7 system. You may try a demo HTTP server as given below.

Step 4 – Create Demo Web Server (Optional)

This is an optional step. If you want to test your node.js install. Let’s create a web server with the “Welcome Node.js” text. Create a file demo_server.js

cd /var/www/html
nano demo_server.jsand add the following content
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Welcome Node.js');
}).listen(3001, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3001/');

Save your file and close.

Now start the web server using the command.

node --inspect demo_server.jsDebugger listening on ws://127.0.0.1:9229/9e0c7b4a-2ffe-48df-a4b0-b4635dcd9359
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://127.0.0.1:3001/

The web server has been started on port 3001. Now access http://127.0.0.1:3001/ URL in browser.

 

https://blog.logrocket.com/running-node-js-scripts-continuously-forever/

Installing forever

Forever can be used in two ways: with the forever CLI tool and with forever-monitor. The forever-monitor tool can be used to run an application with forever using code. We will see forever-monitor in action later in this article.

To use forever, we need to install it globally:

npm i -g forever

Running a script with forever

Running a script with forever is simple. First, use the following command:

forever start app.js

Note that app.js is the name of the script. We can also add a -a flag with the above command. This flag will ensure that log files get appended to the default log file located at forever’s default log file location. There are many other flags available that can be appended to the forever command.

We can also run multiple scripts at once by separating the script names with spaces. Here is an example:

forever start app.js index.js

When you start the processes using the above commands, you’ll receive some warnings and information about the file that it is processing.

After you start the script, you can list all the running processes with the list command, like so:

forever list

forever --version
v4.0.3
(node:13935) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

 

https://tecadmin.net/how-to-install-nvm-on-centos-7/

Installing NVM on CentOS 7

NVM provides a simple bash script for the installation on Linux systems. Open a terminal on your system or connect a remote system using SSH.

Make sure your system have curl installed.

yum install curl -y

 

Then execute the installer script as following command:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash   


=> Appending nvm source string to /root/.bashrc
=> Appending bash_completion source string to /root/.bashrc
=> You currently have modules installed globally with `npm`. These will no
=> longer be linked to the active version of Node when you install a new node
=> with `nvm`; and they may (depending on how you construct your `$PATH`)
=> override the binaries of modules installed with `nvm`:

/usr/lib
├── corepack@0.15.1
├── forever@4.0.3
└── yarn@1.22.19
=> If you wish to uninstall them at a later point (or re-install them under your
=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module


=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

 

https://bobbyhadz.com/blog/node-glibc-not-found-required-by-node

node: version `GLIBC_2.28` not found (required by node)

ldd --version

 

 

There are 2 common ways to solve the error:

  1. Install an older, more widely supported version of Node.js (16.X).
  2. Upgrade your Linux operating system to a newer version.

 

 

To find the default Node version set for the current user, type:

nvm run default --version 

You can run a Node script with the desired version of node.js using the below command:

nvm exec 12.18.3 server.js 

 

 


 

  1. cd /tmp
  2. wget https://nodejs.org/dist/v10.15.3/node-v10.15.3-linux-x64.tar.xz
  3. tar xf node-v10.15.3-linux-x64.tar.xz
  4.  mkdir /usr/local/lib/nodejs
  5. mv /tmp/node-v10.15.3-linux-x64/* /usr/local/lib/nodejs
  6. nano ~/.profile
    # Nodejs
    VERSION=v10.15.0
    DISTRO=linux-x64
    export PATH=/usr/local/lib/nodejs/bin:node
  7. ln -s /usr/local/lib/nodejs/bin/node /usr/bin/node
  8. ln -s /usr/local/lib/nodejs/bin/npm /usr/bin/npm
  9. ln -s /usr/local/lib/nodejs/bin/npx /usr/bin/npx
  10. node -v
  11. npm version
  12. npx -v

 

Installing NVM (Node Version Manager)

1. To install nvm, execute the command below.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

The installer script will be running and adding a new configuration to the '~/.bashrc' file.

 

 


Drupal Integration

  • npm install drupal-node.js
  • npm install forever-monitor or npm install forever -g <https://github.com/foreverjs/forever>
  • updatedb
  • locate drupal-node.js
  • cd {website-directory}//modules/node_modules/drupal-node.js
  • cp nodejs.config.js.example  nodejs.config.js
  • nano nodejs.config.js
  • locate forever
  • ln -s /usr/local/lib/nodejs/bin/forever /usr/bin/forever
  • forever start app.js

 

CREATE TABLE `messages` (
  `id` int(11) NOT NULL,
  `userId` int(11) NOT NULL,
  `toId` int(11) NOT NULL,
  `msg` mediumtext COLLATE utf8mb4_bin NOT NULL,
  `type` int(1) UNSIGNED NOT NULL DEFAULT '0' COMMENT '0 / null = text, 1 = image, 2=video',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `read_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

 

https://www.digitalocean.com/community/tutorials/how-to-install-node-js-...

Install Node Using the Node Version Manager

Another way of installing Node.js that is particularly flexible is through NVM, the Node version manager. This piece of software allows you to install and maintain many different independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your CentOS 7 machine, visit the project's GitHub page. Copy the

curl or wget

command from the README file that displays on the main page. This will point you towards the most recent version of the installation script.
 

Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn't doing anything you don't agree with. You can do that by removing the

| bash segment at the end of the curl command:
curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh

Take a look and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of NVM, but as of right now, the script can be downloaded and executed by typing:


curl https://raw.githubusercontent.com/creationix/nvm/v0.13.1/install.sh | bash

This will install the nvm script to your user account. To use it, you must first source your

.bash_profile:

source ~/.bash_profile

Now, you can ask NVM which versions of Node it knows about:


nvm list-remote

v0.1.14
     v0.1.15
     v0.1.16
     v0.1.17
     v0.1.18
     v0.1.19
     v0.1.20
     v0.1.21
     v0.1.22
     v0.1.23
     v0.1.24
     v0.1.25
     v0.1.26
     v0.1.27
     v0.1.28
..........................
 v10.13.0
    v10.14.0
    v10.14.1
    v10.14.2
    v10.15.0
    v10.15.1
    v10.15.2
    v10.15.3
     v11.0.0
     v11.1.0
     v11.2.0
     v11.3.0
     v11.4.0
     v11.5.0
     v11.6.0
     v11.7.0
     v11.8.0
     v11.9.0
    v11.10.0
    v11.10.1
    v11.11.0
    v11.12.0

 

nvm install v10.15.3

######################################################################## 100.0%
Checksums empty
Now using node v10.15.3

nvm list

->  v10.15.3
      system

 

nvm use v10.15.3

nvm alias default v10.15.3

node --version

 

########################################################################

LOCAL RUN NODE.JS FOR WEBSITE

 cd /var/www/websites/client1/domain19/web/micino_chat/node_modules/drupal-node.js

forever start app.js

Tags: