Installing NPM Libraries
    • 29 Mar 2022
    • 9 Minutes to read
    • PDF

    Installing NPM Libraries

    • PDF

    Article summary

    Scripting supports the use of any third-party libraries with the following limitations:

    • The library must be compatible with node 0.12.7
    • Native libraries are not supported. For example, the serialport library.

     Node.js version 0.12.7 must first be installed on the host machine in order to install a new third-party library and all of its dependencies from the NPM using just one single command. The following sections show how to install Node.js 0.12.7, as well as how to install NPM libraries.

    Installing Node.js 0.12.7

    32-bit (node-v0.12.7-x86.msi), and one for 64-bit OS, located in the x64 folder(node-v0.12.7-x64.msi)

    • Execute the installer by double clicking on it.

     Figure 13. Installing Node.js  


    Click next until the Custom Setup window is opened. Include NPM package manager in the features to install. It is also advisable to add Node.js to PATH: 

    Figure 14. Installing Node.js  


    Keep clicking next until Node.js finishes installation. It will then be possible to test if that node has been successfully installed by opening a command prompt, and running the following command to get the Node.js version installed: node --version.

    Figure 15. Verifying Node.js installation

    Installing NPM libraries

    In this example, the request library will be installed in the N3uron node.

    1. The first step is to search for the NPM Express library, which in this case is located at https://www.npmjs.com/package/request. Once you are in the Express library, navigate to the GitHub page to check the required node version and see if there are any past versions available. The main goal is to check the library’s compatibility with the 0.12.7 version of Node.js.
      Figure 16. Library NPM page                                                                                                                                                                                                                                                                                                                                                                            
    2. Within the GitHub page, there are two ways of checking the required node versions. The majority will be inconsistent since minimum supported version is not a required field when publishing a library to NPM, meaning that not all packages will include this. Generally, the requirements can be found either on the main page (by searching for them) or within the package.json file, as shown in the below screenshot:  Figure 17. Library GitHub page                                                                                                                                                                                                                                                                                                                                                                         
    3. Within this package.json file, the keyword “engines” can be found using the search function. This keyword is used to indicate the minimum node version supported by the library, although, as mentioned earlier, this field is not required. If this keyword search returns no results, the only way to test whether the library is compatible with Node.js 0.12.7 is by trial and error, i.e. by installing the library and trying to use it. The following screenshot shows that the current version of the library requires a Node.js version higher than v.6, and therefore, this library is not compatible with N3uron.  Figure 18. Package.json file                                                                                                                                                                                                                                                                                                                                                                               
    4. Whilst the current version may not be compatible, an older version might be. Users can check for older versions of a library by clicking on “Brach: master” and selecting the tags, which will then return a list of all the versions that have been tagged. By checking these different tags, a compatible version may be found.  Figure 19. Switching library versions                                                                                                                                                                                                                                                                                                                                                                 
    5. Choosing a previous version will load the package.json for that version. Users can then search for the minimum Node.js version until a version is found that is compatible with Node.js version 0.12.7. In the below example, the latest compatible version with Node.js 0.12.7 is version 2.76.1:                             Figure 20. Node.js 0.12.7 compatible version                                                                                                                                                                                                                                                                                                                                                     
    6. The relevant compatible library can then be installed by calling npm from the command prompt. This is done by navigating to the data folder of the Scripting instance (in order to configure the library for use with Scripting). From this folder, a command line prompt can be opened by pressing Shift + Right Click and selecting “Open command window here”.                                                                                      Figure 21. Opening command line in the data folder                                                                                                                                                                                                                                                                                                                                           
    7. Once the command line window opens, the following command will be used to install the specified version of this library: npm install libraryName@libraryversion. If the version is omitted, the latest available version will be installed. In this example, the 2.76 version of request will be installed.: 
      npm install request@2.76
      Figure 22. Installing library via NPM 


     Once this is done, a new node_modules folder is created in the Scripting data folder. A request folder can now be found inside this folder, which corresponds to the library that was just installed. In this instance, the library can now be referenced in the N3uron Scripting module configuration options by setting the path to node_modules/request.


    Was this article helpful?

    What's Next