[英]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 我正在创建私有npm并在此帮助下使用sinopia将其发布到本地npm存储库中
the .storage tree is below .storage树在下面
C:\sinopia
\ storage
\privateProj
package.json
privateProj1.0.1.taz
My main project location I was run the following command 我的主要项目位置是我运行的以下命令
npm install privateProj --save npm install privateProj-保存
it will update package.json is like bellow 它将更新package.json就像下面
{
"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 并在imag之后将privateProj下载到nodemodule文件夹中
then I try to run node myapp.js it show following message
然后我尝试运行节点myapp.js,它显示以下消息
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 privatePro / app.js中的代码是
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”功能将在node_modules中查找,请尝试
require('privateProj');
and rename node_modules/privateProj/app.js to node_modules/privateProj/index.js 并将node_modules / privateProj / app.js重命名为node_modules / privateProj / index.js
Also change your module code (privateProj code) from: 还可以从以下位置更改模块代码(privateProj代码):
exports.printFruits = printFruits;
to 至
module.exports = printFruits;
Also just noticed you're exporting the function, in your myApp.js change 还只是注意到您在myApp.js更改中正在导出函数
pp.printFruits();
to just 只是
pp();
First - you don't need to set full path to npm module, you can just require('moduleName'). 首先-您不需要为npm模块设置完整路径,您只需要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)
所以只使用
require(moduleName)
Optionally read more about how require works here (可选) 在此处详细了解需求的工作原理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.