P John Raj
P John Raj

Reputation: 537

How to use my private npm package in my main project

I am creating private npm and publish it my local npm repository using sinopia with help of this

the .storage tree is below

C:\sinopia
    \ storage
      \privateProj
          package.json
          privateProj1.0.1.taz

My main project location I was run the following command

npm install privateProj --save

It will update package.json is like below

   {
  "name": "privateprojectClient",
  "version": "1.0.0",
  "description": "",
  "main": "myApp.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "privateProj": "^1.0.1"
  }
}

and download privateProj in to nodemodule folder reffer following imag enter image description here then I try to run node myapp.js it show following message

I:\NodeJSProject\privateprojectClient>node myApp.js

module.js:340
    throw err;
          ^
Error: Cannot find module '/node_modules/privateProj'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (I:\NodeJSProject\privateprojectClient\myApp.js:5:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

The code in privatePro/app.js is

var db = require('diskdb');

db.connect('./examples/db', ['articles','comments','users']);

var article1 = {
    title : 'diskDB rocks',
    published : 'today',
    rating : '5 stars'
}

var article2 = {
    title : 'diskDB rocks',
    published : 'yesterday',
    rating : '5 stars'
}

var article3 = {
    title : 'diskDB rocks',
    published : 'today',
    rating : '4 stars'
}
db.articles.save([article1, article2, article3]);

var articleComments = {
    title: 'diskDB rocks',
    published: '2 days ago',
    comments: [{
        name: 'a user',
        comment: 'this is cool',
        rating: 2
    }, {
        name: 'b user',
        comment: 'this is ratchet',
        rating: 3
    }, {
        name: 'c user',
        comment: 'this is awesome',
        rating: 2
    }]
}

db.comments.save(articleComments);



var printFruits = function() {
    console.log(db.articles.find());
}

// To test the app locally
 //npm publishprintFruits(); // << uncomment this
// and run
// $ node app.js

exports.printFruits = printFruits;

Upvotes: 0

Views: 192

Answers (2)

Stuart Bennett
Stuart Bennett

Reputation: 121

Node "require" function will look in node_modules by default, try

require('privateProj');

and rename node_modules/privateProj/app.js to node_modules/privateProj/index.js

Also change your module code (privateProj code) from:

exports.printFruits = printFruits;

to

module.exports = printFruits;

Also just noticed you're exporting the function, in your myApp.js change

pp.printFruits();

to just

pp();

Upvotes: 1

freele
freele

Reputation: 140

First - you don't need to set full path to npm module, you can just require('moduleName').
Second (and main issue in your case) - you set absolute path and that's why your script can't find it.
So use just require(moduleName)
Optionally read more about how require works here

Upvotes: 0

Related Questions