Skip to main content
Version: Next

NPM Package

A npm package contains the package.json file that describes the meta information for the package.

The package name is specified with the name field in package.json, no longer by the directory name. The entry point is still index.js or index.ts by default, but can be override by the exports or main field in package.json.

Example

For example let's recreate the syntax package (in Simple Package) in npm package format:

$HOME/.rsvim
|- rsvim.js
|- syntax/
|- package.json
|- lib/
|- index.js
|- utils.js

syntax/package.json

{
"name": "syntax",
"version": "0.1.0"
"exports": {
".": "./lib/index.js"
},
// Or
"main": "./lib/index.js"
}

syntax/lib/index.js

import { hello } from "./utils.js";

export default { hello };

syntax/lib/utils.js

export function hello(value) {
Rsvim.cmd.echo(`Hello:${value}`);
}

rsvim.js

import syntax from "syntax";

syntax.hello("Rsvim!");

Rsvim will resolve the module name by the "name" field in package.json, no longer by the package's directory name. It means you can provide a different directory name.

Rsvim will resolve the module path by looking into the package.json file. In this example, the "syntax" specifier is resolved to the syntax/lib/index.js file, because package.json specifies the entry point with "exports": {".": "./lib/index.js"}.

Node Modules

Rsvim also tries to find npm packages in the node_modules directory under config home.

Let's rewrite the syntax example (in the above):

$HOME/.rsvim
|- rsvim.js
|- node_modules/
|- syntax/
|- package.json
|- lib/
|- index.js
|- utils.js

rsvim.js

import syntax from "syntax";

syntax.hello("Rsvim!");

Other files are still the same...

The "syntax" specifier is resolved to node_modules/syntax/lib/index.js file.