I've written a javascript library as an ES6 module, primarily intended for use in the browser. Now I want to package it for node. I'm ok with restricting to node v 14+. I don't want to transpile.
I'm not sure how to handle the dependencies. One in particular, moment-js, is giving me trouble.
In the browser (tried latest FF + Chrome), I reference the moment library in a <script>
tag, and then in my code, I can just use the variable moment
, without having had to first import it.
my-module.js
:
function testFn() {
return moment.duration(300).asSeconds();
}
export { testFn }
browser code
:
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script type="module" src="my-module.js"></script>
<script>
import { testFn } from 'my-module.js';
let val = testFn();
console.log(val);
</script>
However, in node, I need the module to include an import
in order for it to work:
my-module.js
:
import moment from 'moment';
function testFn() {
return moment.duration(300).asSeconds();
}
export { testFn }
Then I can use my-module
in node:
import { testFn } from 'my-module.js';
let val = testFn();
console.log(val);
But, now the browser code is broken:
TypeError: Error resolving module specifier: moment
I've tried different import
s (import * as ...
, import { moment } from ...
) but no luck - the browser still complains.
Moment-js is just an example here - I'd like a generic solution as I have other dependencies too.
This must be an issue that other people have had and solved without resorting to awful workarounds like detecting the environment and conditionally importing. Does anyone have a good solution or reference? Thanks!!