简体   繁体   中英

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 bellow

   {
  "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 在此处输入图片说明 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)

Please help me to solve this problem.

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;

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM