简体   繁体   English

如何发布和使用npm软件包

[英]How publish and use the npm packages

My module 我的模块

npmpublicrepo -- package.json -- test.js

package.json package.json

   {
  "name": "npmpublicrepo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

test.js test.js

exports.testMessage = function() {
   console.log("test");  
};

npm publish npm发布

This got successfully published and I can see the page in npm 这已成功发布,我可以在npm中看到页面

https://www.npmjs.com/package/npmpublicrepo https://www.npmjs.com/package/npmpublicrepo

My normal project to use above package 我正常的项目使用上面的包

npmpublicrepousage -- package.json -- index.js

package.json package.json

{
  "name": "npmpublicrepousage",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "npmpublicrepo": "^1.0.0"
  }
}

after doing npm install , I can see the folder in ./node_modules/npmpublicrepo and I am able to see the module's code properly. 完成npm install ,我可以在./node_modules/npmpublicrepo看到该文件夹​​,并且能够正确看到该模块的代码。

Running a script which use the module 运行使用该模块的脚本

Then, I use the previous module in the script ./index.js : 然后,我在脚本./index.js使用上一个模块:

var test = require("npmpublicrepo");
test.testMessage();

But, running the script fails : 但是,运行脚本失败:

node ./index.js 节点./index.js

...with the error : ...出现错误:

module.js:472
    throw err;
    ^

Error: Cannot find module 'npmpublicrepo'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/vimalprakash/Documents/Projects/NodeJs/npmpublicrepousage/index.js:1:74)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)

I don't know what i am missing. 我不知道我在想什么。

My node version : v7.4.0 我的节点版本:v7.4.0

My NPM version : 4.0.5 我的NPM版本:4.0.5

In your npmpublicrepo package.json you expose a wrong primary entry point to your program . 在npmpublicrepo package.json中,您向程序暴露了错误的主入口点 You set a file that does not exist: 您设置了一个不存在的文件:

"main": "index.js"

You could rename test.js to index.js or you could expose the test.js file as the primary entry point to your program with: 您可以使用以下命令将test.js重命名为index.js或将test.js文件作为程序的主要入口点公开:

"main": "test.js"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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