Javascript 如何发布和使用npm包 我的模块
Javascript 如何发布和使用npm包 我的模块,javascript,node.js,npm,npm-publish,Javascript,Node.js,Npm,Npm Publish,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":
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"
}
{
"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"
}
}
test.js
exports.testMessage = function() {
console.log("test");
};
npm发布
这已成功发布,我可以在npm中看到该页面
我的正常项目使用上述软件包
npmpublicrepousage
--package.json
--index.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"
}
{
"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安装
后,我可以在/node\u modules/npmpublicrepo
中看到该文件夹,并且我能够正确地看到该模块的代码
运行使用该模块的脚本
然后,我使用脚本/index.js
中的前一个模块:
var test = require("npmpublicrepo");
test.testMessage();
但是,运行脚本失败:
node./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)
module.js:472
犯错误;
^
错误:找不到模块“npmpublicepo”
在Function.Module.\u解析文件名(Module.js:470:15)
在Function.Module.\u加载(Module.js:418:25)
at Module.require(Module.js:498:17)
根据需要(内部/module.js:20:19)
反对。(/Users/vimalprakash/Documents/Projects/NodeJs/npmpublicrepousage/index.js:1:74)
在模块处编译(模块js:571:32)
在Object.Module.\u extensions..js(Module.js:580:10)
在Module.load(Module.js:488:32)
在tryModuleLoad时(module.js:447:12)
在Function.Module.\u加载(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"
在您的npmpublicrepo package.json中,您公开了一个错误的。您设置了一个不存在的文件:
"main": "index.js"
您可以将test.js重命名为index.js,也可以将test.js文件作为程序的主要入口点,方法是:
"main": "test.js"