Alex-D / check-disk-space

Light multi-platform disk space checker without third-party for Node.js
https://www.npmjs.com/package/check-disk-space
MIT License
103 stars 24 forks source link

Problem importing check-disk-space. Minor fix suggestion #14

Closed mmarszal closed 3 years ago

mmarszal commented 3 years ago

I've been trying to work on node installations that support esm, and found that check-disk-space doesn't work out of the box on them. I've tested this on node 16.9.1. Using require works fine however.

Whether or not if my package.json include "type": "module", and then try to import check-disk-space via import checkDiskSpace from "check-disk-space", I get the following error in the console:

(Use `node --trace-warnings ...\` to show where the warning was created)
/Users/mark/ch_test/node_modules/check-disk-space/dist/check-disk-space.es.js:1
import { execFile } from 'child_process';
^^^^^^

SyntaxError: Cannot use import statement outside a module`

However, if I make some minor changes to the module in such that I do the following:

  1. Add "type": "module" to check-disk-space's package.son
  2. Rename check-disk-space.cjs.js to check-disk-space.cjs
  3. Rename check-disk-space.es.js to check-disk-space.mjs
  4. change "main": "./dist/check-disk-space.cjs.js" to "main": "./dist/check-disk-space.cjs"
  5. change "import": "./dist/check-disk-space.es.js" to "import": "./dist/check-disk-space.mjs"
  6. change "require": "./dist/check-disk-space.cjs.js" to "require": "./dist/check-disk-space.cjs"

then I can use the import syntax without any problem in esm environments, and require in commonjs environments. I am not sure if the above fix is the most optimal, but it allowed me to work in both environments without any problems.

Testing code used was simply:

import checkDiskSpace from "check-disk-space";
//const checkDiskSpace = require("check-disk-space").default; // for testing require

(async () => {
  console.log(await checkDiskSpace("/"));
})();

I've also tested this with typescript with "target": "ESNext", "module": "ESNext", and "moduleResolution": "node" in my tsconfig.json.

rohitbishnoi commented 3 years ago

+1 Facing the same issue

yannmichaux commented 3 years ago

+2 same here, i've downloaded the library in my project for using it

stheine commented 3 years ago

Same issue here. Reverting to 2.1.0 until fixed.

Alex-D commented 3 years ago

Thank you for the report and the documentation of the issue :) This is fixed in the new 3.1.0 release