Node modules from a different path using NODE_PATH

Submitted by Dickens A S on Sat, 04/09/2022 - 15:41

This article explains how to use node.js modules from different folder, for example you have installed "express" in a different folder and want to use it another application without installing inside local node_modules

How to do it

1.) Install express in a different folder

Example c:\dev\test1

npm install express

Now you have c:\dev\test1\node_modules created

2.) Go to another folder and create your index.js

Example c:\dev\test2

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

3.) Setup NODE_PATH

set NODE_PATH=C:\dev\test1\node_modules;.\node_modules

Now you are all set 

4.) Run you app

node index.js
Example app listening on port 3000

That's all :)

 

Node.JS
NODE_PATH

Add new comment