简体   繁体   中英

How publish and use the npm packages

My module

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 publish

This got successfully published and I can see the page in npm

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

My normal project to use above package

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

after doing npm install , I can see the folder in ./node_modules/npmpublicrepo and I am able to see the module's code properly.

Running a script which use the module

Then, I use the previous module in the script ./index.js :

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

But, running the script fails :

node ./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

My NPM version : 4.0.5

In your npmpublicrepo package.json you expose a wrong primary entry point to your program . 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:

"main": "test.js"

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