Floyd Resler
Floyd Resler

Reputation: 1786

Where Should Node Modules Be Installed?

I've been struggling for a while trying to get installed node modules loaded via JavaScript. I finally got common.js installed and working. However, when I do the following command, the module can't be found:

const tbl = require("tableexport");

I'm guessing I have the node_modules folder in an incorrect spot. Where should it be? In my case, the URL to my project is something like:

domain.com/main/project

Right now, node_modules resides in "project".

Here is my package.json file:

{
  "name": "clientssite",
  "version": "1.0.0",
  "description": "Client's Site",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Floyd Resler",
  "license": "ISC",
  "dependencies": {
    "common.js": "^1.1.1",
    "tableexport": "^5.2.0"
  }
}

The file structure from the root of the domain folder is:

adex
    clientsSite
        node_modules
            adler-32
            async
            blobjs
            bootstrap
            cfb
            codepage
            commander
            common.js
            crc-32
            exit-on-epipe
            file-sverjs
            trac
            jquery
            printj
            ssf
            tableexport
            wrench
            xlsx
        package.json

I used npm to install the packages.

Upvotes: 0

Views: 2357

Answers (1)

kevintechie
kevintechie

Reputation: 1521

You might be confusing node "modules" with "packages". While packages typically utilize modules, they are not the same.

Packages are installed by a program called npm. By default, npm puts them into the node_modules directory when you install a package. The node_modules directory is usually in the same directory as your package.json file. After you install a package, you can reference a module in the package using the package name.

Depending on the module system you are using, you will either use require or import statements to reference modules in your code.

When you write your own modules, you can put them anywhere, but typically they go in the same directory as your package.json file or in a subdirectory of the directory that contains your package.json file. Once you have created a module, you can reference it from other files (which can also be modules) using the path to that module in your require or import statements rather than a package name.

You do not put your module code in nodes special node_modules directory.

Upvotes: 1

Related Questions