sindresorhus / trash

Move files and directories to the trash
MIT License
2.57k stars 78 forks source link

ERR_REQUIRE_ESM when including this in another project #119

Closed Fleker closed 2 years ago

Fleker commented 2 years ago

Good afternoon, I was looking at integrating this into the larger npkill CLI tool and ran into an error in the execution stage. It looks like there's some compatibility issues between this library and npkill due to a discrepancy over module format.

This may be a combination of this library's main file extension and Typescript in the other lib. Typescript converts the

import trash from 'trash'

to be require automatically, and other libraries mean that I can't completely change this.

I had to remove "type": "module" from the package.json and replace the import path from "node:path" to be const path = require("path") and otherwise reformat the files to be standard Node.

This also occurs with your dependency is-path-inside.

After messing around with this for a bit, I've decided to stop as it was getting too onerous.

Original stacktrace:

nick@Windows:/mnt/c/Users/Nick/Development/npkill$ yarn start --trash
yarn run v1.21.1
$ ts-node -r tsconfig-paths/register ./src/main.ts --trash
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /mnt/c/Users/Nick/Development/npkill/node_modules/trash/index.js
require() of ES modules is not supported.
require() of /mnt/c/Users/Nick/Development/npkill/node_modules/trash/index.js from /mnt/c/Users/Nick/Development/npkill/src/services/unix-files.service.ts is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /mnt/c/Users/Nick/Development/npkill/node_modules/trash/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/mnt/c/Users/Nick/Development/npkill/src/services/unix-files.service.ts:6:1)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Module.m._compile (/mnt/c/Users/Nick/Development/npkill/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Object.require.extensions.<computed> [as .ts] (/mnt/c/Users/Nick/Development/npkill/node_modules/ts-node/src/index.ts:861:12)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
sindresorhus commented 2 years ago

See https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

Fleker commented 2 years ago

Thank you for passing that along. It looks like there's still quite a few incompatibility issue, but I think I can make it work with refactoring the npkill project.