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

Error resolving GOV.UK Frontend subpath exports in Node.js 17+ #3755

Closed
romaricpascal opened this issue Jun 6, 2023 · 3 comments · Fixed by #3816
Closed

Error resolving GOV.UK Frontend subpath exports in Node.js 17+ #3755

romaricpascal opened this issue Jun 6, 2023 · 3 comments · Fixed by #3816
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) javascript

Comments

@romaricpascal
Copy link
Member

romaricpascal commented Jun 6, 2023

Description of the issue

We've identified two scenarios when trying to require() or import GOV.UK Frontend

  1. Node.js >= 17 errors with GOV.UK Frontend v4 folder mapping in "exports"
  2. Node.js <= 12.18 errors with GOV.UK Frontend v5 (unreleased) subpath patterns in "exports"
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './govuk/all.js' is not defined by "exports" in /Users/romaric.pascal/code/package-exports/node_modules/govuk-frontend/package.json

This can happen if extending our components and having a test suite run with a tool like Mocha that uses native Node require or import to load modules.

Steps to reproduce the issue

  1. Create a new prototype GOV.UK Frontend v4.6.0

    npx govuk-prototype-kit create package-exports
    cd package-exports
  2. Run Node.js require.resolve() to locate GOV.UK Frontend's default exports

    node --eval "console.log(require.resolve('govuk-frontend'))" --conditions require
    node --eval "console.log(require.resolve('govuk-frontend'))" --conditions import
  3. Run Node.js require.resolve() to locate a subpath export

    node --eval "console.log(require.resolve('govuk-frontend/govuk/all.js'))"

    Using Node.js 16 you'll see the following warning:

    [DEP0148] DeprecationWarning: Use of deprecated folder mapping "./govuk/" in the "exports" field module resolution of the package at /node_modules/govuk-frontend/package.json.
    Update this package.json to use a subpath pattern like "./govuk/*"

    Using Node.js 17+ you'll see the following error:

    Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './govuk/all.js' is not defined by "exports" in /node_modules/govuk-frontend/package.json

Actual vs expected behaviour

Instead of receiving an error, the subpath export should should be resolved properly.

@colinrotherham suggested the following update to our exports to achieve so, using both :

"exports": {
  ".": {
    "sass": "./govuk/all.scss",
    "import": "./govuk-esm/all.mjs",
    "require": "./govuk/all.js"
  },
  "./govuk/": "./govuk/",
  "./govuk/*": "./govuk/*",
  "./govuk-esm/": "./govuk-esm/",
  "./govuk-esm/*": "./govuk-esm/*",
  "./package.json": "./package.json"
},
@romaricpascal romaricpascal added 🐛 bug Something isn't working the way it should (including incorrect wording in documentation) javascript labels Jun 6, 2023
@colinrotherham
Copy link
Contributor

Thanks @romaricpascal

The hybrid idea was because package.json subpath patterns requires Node.js 12.20.0+

But we should check if a single "./*": "./*" catch-all is all we need:

"exports": {
  ".": {
    "sass": "./govuk/all.scss",
    "import": "./govuk-esm/all.mjs",
    "require": "./govuk/all.js"
  },
  "./govuk/": "./govuk/",
  "./govuk-esm/": "./govuk-esm/",
  "./package.json": "./package.json",
  "./*": "./*"
},

colinrotherham added a commit that referenced this issue Jun 15, 2023
We switched to “subpath patterns” recently (Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
@colinrotherham colinrotherham added this to the v4.7.0 milestone Jun 15, 2023
colinrotherham added a commit that referenced this issue Jun 15, 2023
We switched to “subpath patterns” recently (Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
colinrotherham added a commit that referenced this issue Jun 15, 2023
We switched to “subpath patterns” recently (Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
colinrotherham added a commit that referenced this issue Jun 16, 2023
We switched to “subpath patterns” recently (Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
@colinrotherham
Copy link
Contributor

@romaricpascal Updated the description to show how both v4 and v5 are affected

@colinrotherham colinrotherham changed the title requireing or importing specific JavaScript files in Node >=17 Error resolving GOV.UK Frontend subpath exports in Node.js 17+ Jun 16, 2023
colinrotherham added a commit that referenced this issue Jun 16, 2023
We switched to “subpath patterns” recently (supported in Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
@colinrotherham colinrotherham linked a pull request Jun 16, 2023 that will close this issue
@colinrotherham colinrotherham moved this from Backlog 🗄 to In progress 📝 in GOV.UK Design System cycle board Jun 16, 2023
@colinrotherham colinrotherham self-assigned this Jun 16, 2023
@colinrotherham colinrotherham moved this from In progress 📝 to Needs review 🔍 in GOV.UK Design System cycle board Jun 16, 2023
colinrotherham added a commit that referenced this issue Jun 16, 2023
We hadn’t realised that support for “subpath exports” with trailing slashes (not asterisks) was deprecated in Node.js 16 and removed in Node.js 17 https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

But fully switching to “subpath patterns” requires Node.js 12.20+ so both flavours will be needed for backwards compatibility

Fixes: #3755
@colinrotherham
Copy link
Contributor

Node.js <= 12.18 fix (v5 only) ready for review:

Node.js >= 17 fix (v4 only) ready for review:

colinrotherham added a commit that referenced this issue Jun 19, 2023
We switched to “subpath patterns” recently (supported in Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
colinrotherham added a commit that referenced this issue Jun 19, 2023
We switched to “subpath patterns” recently (supported in Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
@colinrotherham colinrotherham removed this from the v4.7.0 milestone Jun 21, 2023
querkmachine pushed a commit that referenced this issue Jun 23, 2023
We switched to “subpath patterns” recently (supported in Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
@colinrotherham colinrotherham moved this from Needs review 🔍 to Done 🏁 in GOV.UK Design System cycle board Jun 26, 2023
owenatgov pushed a commit that referenced this issue Jul 3, 2023
We hadn’t realised that support for “subpath exports” with trailing slashes (not asterisks) was deprecated in Node.js 16 and removed in Node.js 17 https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

But fully switching to “subpath patterns” requires Node.js 12.20+ so both flavours will be needed for backwards compatibility

Fixes: #3755
colinrotherham added a commit that referenced this issue Jul 19, 2023
We switched to “subpath patterns” recently (supported in Node.js 12.20+) but hadn’t realised that “subpath exports” with trailing slashes (not asterisks) were still needed for older versions

We need both flavours because trailing slashes in exports were deprecated in Node.js 16
https://nodejs.org/docs/latest-v18.x/api/deprecations.html#dep0148-folder-mappings-in-exports-trailing-

Fixes: #3755
colinrotherham added a commit to alphagov/govuk-prototype-kit that referenced this issue Aug 24, 2023
colinrotherham added a commit to alphagov/govuk-prototype-kit that referenced this issue Aug 24, 2023
colinrotherham added a commit to alphagov/govuk-prototype-kit that referenced this issue Aug 24, 2023
colinrotherham added a commit to alphagov/govuk-prototype-kit that referenced this issue Aug 24, 2023
colinrotherham added a commit to alphagov/govuk-prototype-kit that referenced this issue Aug 31, 2023
@colinrotherham colinrotherham removed their assignment Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working the way it should (including incorrect wording in documentation) javascript
Projects
Development

Successfully merging a pull request may close this issue.

2 participants