My question concerns an existing library that I wish to publish as an NPM module. The library is already in use, and currently required via the local file syste
Another possibility is to use ECMAScript modules (ES modules), particularly the package exports field in your package.json file.
Given a package.json file with this config:
{
"name": "my",
"exports": {
"./": "./src/js/lib/my/"
}
}
You should be able to import modules from the library like:
import thing from 'my/thing'
import that from 'my/that'
This is enabled by default since node 13.0.0, but was behind the --experimental-exports flag from 12.13.0.
Note, that the ES Module spec is in the Stability:1 - Experimental stage and subject to change. I have no idea the extent to which this might be compatible with CommonJS modules.