繁体   English   中英

如何发布和使用npm软件包

[英]How publish and use the npm packages

我的模块

npmpublicrepo -- package.json -- test.js

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

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

npm发布

这已成功发布,我可以在npm中看到页面

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

我正常的项目使用上面的包

npmpublicrepousage -- package.json -- index.js

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"
  }
}

完成npm install ,我可以在./node_modules/npmpublicrepo看到该文件夹​​,并且能够正确看到该模块的代码。

运行使用该模块的脚本

然后,我在脚本./index.js使用上一个模块:

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

但是,运行脚本失败:

节点./index.js

...出现错误:

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)

我不知道我在想什么。

我的节点版本:v7.4.0

我的NPM版本:4.0.5

在npmpublicrepo package.json中,您向程序暴露了错误的主入口点 您设置了一个不存在的文件:

"main": "index.js"

您可以使用以下命令将test.js重命名为index.js或将test.js文件作为程序的主要入口点公开:

"main": "test.js"

暂无
暂无

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

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