Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_REQUIRE_ESM when including this in another project #119

Closed
Fleker opened this issue Oct 14, 2021 · 2 comments
Closed

ERR_REQUIRE_ESM when including this in another project #119

Fleker opened this issue Oct 14, 2021 · 2 comments

Comments

@Fleker
Copy link

Fleker commented Oct 14, 2021

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
Copy link
Owner

@Fleker
Copy link
Author

Fleker commented Oct 16, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants