kill-sticky icon indicating copy to clipboard operation
kill-sticky copied to clipboard

Fix build by converting bookmarkletify.js to an ES module

Open tom93 opened this issue 1 year ago • 0 comments

The package 'get-stdin' switched to an ES module in version 9.0.0 [1], which broke the build (full error message below).

The error could be fixed by switching to a dynamic import(), namely import('get-stdin').then(...), but that's awkward; so instead this commit converts bookmarkletify.js to an ES module (by changing the file extension to .mjs), which allows using the nicer import statement syntax: import getStdin from 'get-stdin';.

There is an additional problem: import specifiers don't support the NODE_PATH environment variable[2] and only search local 'node_modules' directories, so the 'get-stdin' module is not found.

This commit fixes the module search problem by installing the 'get-stdin' package locally instead of globally. (The other packages are still installed globally, so that their executables will be available in the default $PATH.)

The final complication is the location of the 'node_modules' directory. The build command bind-mounts the repository into the container on /kill-sticky. Creating a 'node_modules' directory inside /kill-sticky at build time wouldn't work, because the bind mount would hide it. And creating it at runtime is ugly, because it pollutes the directory outside the container.

Instead, this commit moves /kill-sticky to /build/kill-sticky, and then creates the 'node_modules' directory in /build (outside the 'kill-sticky' directory so it won't pollute the repository, but still in the module search path).


Here is the error message with get-stdin 9.0.0 prior to this commit:

/kill-sticky/src/bookmarkletify.js:1
const getStdin = require('get-stdin');
                 ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/local/lib/node_modules/get-stdin/index.js from /kill-sticky/src/bookmarkletify.js not supported.
Instead change the require of index.js in /kill-sticky/src/bookmarkletify.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/kill-sticky/src/bookmarkletify.js:1:18) {
  code: 'ERR_REQUIRE_ESM'
}

[1]: https://github.com/sindresorhus/get-stdin/commit/8f70f58fd5cb9e5836d58e2f8dda16128c5e3f3e

[2]: https://nodejs.org/docs/latest-v19.x/api/esm.html#no-node_path

tom93 avatar Mar 22 '23 18:03 tom93