Change name entry in package.json file to a valid npm name
Try the tool validate-npm-package-name to validate it
"name": "@EastDesire/jscolor", -> change to "name": "@eastdesire/jscolor", - note: must be all lowercase
Add node module exports
Copy jscolor.js file to jscolor-node.js file and add module exports:
module.exports = window.jscolor // default export
exports.jscolor = window.jscolor // named export
Now point the main entry in package.json to jscolor-node.js
This will make it available for NodeJS projects in a compatible way (See https://stackoverflow.com/questions/30241729/how-do-i-perform-an-export-that-is-compatible-with-es5-and-es6). The compatibility is handled by a transpiler such as Babel for pure javascript or in TypeScript by the compiler itself.
ES6 module usage example (using import)
import * as JsColor from "@eastdesire/jscolor";
const element = document.createElement("x");
const picker = new JsColor(element);
Old style NodeJs usage example (using require)
const JsColor = require("@eastdesire/jscolor")
const element = document.createElement("x");
const picker = new JsColor(element);
Since jscolor is also exported as a name constant (via export.jscolor) you can also import as follows:
const { jscolor } = require("@eastdesire/jscolor")
Similarly for ES5 import
import { jscolor } from "@eastdesire/jscolor";
You can then export more named constants as needed to make it easier to use.
You might want to have a look at my TypeScript conversion for how to convert it to a proper Javascript class, if that is the direction you want to take it... Makes it much easier to extend and customise for the end user.
Change
nameentry inpackage.jsonfile to a valid npm nameTry the tool validate-npm-package-name to validate it
"name": "@EastDesire/jscolor",-> change to"name": "@eastdesire/jscolor",- note: must be all lowercaseAdd node module exports
Copy
jscolor.jsfile tojscolor-node.jsfile and add module exports:Now point the
mainentry inpackage.jsontojscolor-node.jsThis will make it available for NodeJS projects in a compatible way (See https://stackoverflow.com/questions/30241729/how-do-i-perform-an-export-that-is-compatible-with-es5-and-es6). The compatibility is handled by a transpiler such as Babel for pure javascript or in TypeScript by the compiler itself.
ES6 module usage example (using
import)Old style NodeJs usage example (using
require)Since jscolor is also exported as a name constant (via
export.jscolor) you can also import as follows:const { jscolor } = require("@eastdesire/jscolor")Similarly for ES5 import
import { jscolor } from "@eastdesire/jscolor";You can then export more named constants as needed to make it easier to use.
You might want to have a look at my TypeScript conversion for how to convert it to a proper Javascript class, if that is the direction you want to take it... Makes it much easier to extend and customise for the end user.