javascript - Use existing JS "package" with Node.JS? -


i have existing js code releasing npm package. let's call "my-pkg". i've created package.json , installed new local package. package consists of these files:

  • my-pkg.js
  • package.json

my-pkg.js

function ping() {     console.log("hi!"); } 

package.json

{   "name": "my-pkg",   "version": "1.0.0",   "description": "my test package.",   "main": "my-pkg.js",   "scripts": {     "test": "echo \"error: no test specified\" && exit 1"   },   "author": "me",   "license": "isc" } 

i try use package this:

npm install .\my-pkg node > var mypkg = require("my-pkg"); undefined > mypkg.ping(); typeerror: undefined not function 

note on windows 8 in elevated command line node.js v0.12.2. looking expose existing js functionality, not write brand new npm package. missing here?

all node.js modules must export contents via module.exports. lack following in file:

module.exports.ping = ping; 

this works (exports assigned module.exports):

exports.ping = ping; 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -