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

ESM import issue #228

Closed
samtsai opened this issue Jan 23, 2024 · 6 comments
Closed

ESM import issue #228

samtsai opened this issue Jan 23, 2024 · 6 comments
Labels

Comments

@samtsai
Copy link
Contributor

samtsai commented Jan 23, 2024

The command fails on importing of cypress.config.ts

DEBUG=find-cypress-specs npx find-cypress-specs --branch main
Need to install the following packages:
find-cypress-specs@1.41.4
Ok to proceed? (y)
  find-cypress-specs arguments { _: [], '--branch': 'main' } +0ms
  find-cypress-specs found file cypress.config.ts +0ms
  find-cypress-specs loading Cypress config from /Users/username/Code/firepower-test-library/cypress.config.ts +127ms
/Users/username/.npm/_npx/7109e851be596cd4/node_modules/ts-node/dist-raw/node-internal-errors.js:46
  const err = new Error(getErrRequireEsmMessage(filename, parentPath, packageJsonPath))
              ^
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/username/Code/firepower-test-library/cypress.config.ts
require() of ES modules is not supported.
require() of /Users/username/Code/firepower-test-library/cypress.config.ts from /Users/username/.npm/_npx/7109e851be596cd4/node_modules/require-and-forget/src/index.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/username/Code/firepower-test-library/package.json.

    at createErrRequireEsm (/Users/username/.npm/_npx/7109e851be596cd4/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/Users/username/.npm/_npx/7109e851be596cd4/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/Users/username/.npm/_npx/7109e851be596cd4/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at requireAndForget (/Users/username/.npm/_npx/7109e851be596cd4/node_modules/require-and-forget/src/index.js:28:17)
    at getConfigTs (/Users/username/.npm/_npx/7109e851be596cd4/node_modules/find-cypress-specs/src/index.js:51:25)
    at getConfig (/Users/username/.npm/_npx/7109e851be596cd4/node_modules/find-cypress-specs/src/index.js:87:12) {
  code: 'ERR_REQUIRE_ESM'
}

pacakge.json is set to "type": "module"

@bahmutov
Copy link
Owner

bahmutov commented Jan 23, 2024 via email

@samtsai
Copy link
Contributor Author

samtsai commented Jan 23, 2024

Sure, let me work on a smaller reproducible example.

@samtsai
Copy link
Contributor Author

samtsai commented Jan 23, 2024

https://github.com/samtsai/cypress-test-tiny/tree/typescript-esm-import-issue

I think the issue is actually in require-and-forget so I might look at your repo there.

@benjmountain
Copy link

Seeing the same issue as @samtsai, we hoped to migrate our org's Cypress codebase from CommonJS to ES Module but after updating the package.json to include "type": "module", find-cypress-specs breaks at runtime.

The error produced is the same as noted above (here is a short version):

/Users/username/repo/node_modules/require-and-forget/src/index.js:28
  const value = require(resolvedPath)

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/username/repo/cypress.config.js from /Users/username/repo/node_modules/require-and-forget/src/index.js not supported.

With a trace to line 28 of the require-and-forget's index.js

@samtsai
Copy link
Contributor Author

samtsai commented Jan 23, 2024

require-and-forget is not setup to work with ESM import, hence "require"-and-forget. I think our options are to:

  • use a new package like https://github.com/pygy/esm-reload in addition to require-and-forget that can handle ESM import with similar behavior, import but remove from cache
  • change require-and-forget to handle both environments

See some relevant notes:
nodejs/node#45207
nodejs/node#49442

cc @bahmutov

Copy link

🎉 This issue has been resolved in version 1.42.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

3 participants