postcss / postcss-cli

CLI for postcss
Other
824 stars 93 forks source link

Version 9 is throwing exception on ESM loader #404

Closed MarcLongJoyride closed 2 years ago

MarcLongJoyride commented 2 years ago

I am getting the following error on node v12.18.0 in WSL 2.0 (Windows version 10.0.19043.1237)

(node:2975) ExperimentalWarning: The ESM module loader is experimental.
internal/modules/run_main.js:54
    internalBinding('errors').triggerUncaughtException(
                              ^
Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:720:11)
    at Loader.resolve (internal/modules/esm/loader.js:97:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:243:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
    at link (internal/modules/esm/module_job.js:41:36) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}

If I run ./node_modules/.bin/postcss, I am able to limit this error down specifically to postcss.

Downgrading from postcss-cli 9.0.0 to 8.x resolves the issue.

(Edit: also see the issue in v14 of node)

RyanZim commented 2 years ago

Are you using --use?

dennisreimann commented 2 years ago

I got the same problem with 9.0.0 and 9.0.1 -- but only when building on Netlify. The same build works fine locally and with GitHub Actions.

Not using --use, my command is plain and simple: postcss src/css/main.css --output dist/css/main.css

This is what the postcss config looks like:

const mediaVariables = require('postcss-media-variables')

module.exports = {
  plugins: [
    require('autoprefixer'),
    require('postcss-import'),
    require('postcss-nesting'),
    mediaVariables,
    require('postcss-custom-media'),
    require('postcss-calc'),
    mediaVariables,
  ],
}

All packages are current:

    "autoprefixer": "10.3.6",
    "postcss": "8.3.8",
    "postcss-calc": "8.0.0",
    "postcss-cli": "9.0.1",
    "postcss-custom-media": "8.0.0",
    "postcss-import": "14.0.2",
    "postcss-media-variables": "2.0.1",
    "postcss-nesting": "8.0.1",
RyanZim commented 2 years ago

Oh, just noticed; you're on Node v12.18; technically, ESM is only supported in Node v12.20+ I just assumed everyone would be on the latest Node v12 if they were using v12

dennisreimann commented 2 years ago

@RyanZim Thanks for the clarification -- Netlify is by default still using v12.18, that's the cause there.

brycewray commented 2 years ago

Same issue on Cloudflare Pages. It runs (as of now) 12.18.0, so the procedure is to add an environment variable with the key of NODE_VERSION and a value of at least 12.20.0. (Vercel and Render use 14.x AFAIK.)

kintarowins commented 2 years ago

I had the same issue on postcss 8.3.9 and node 14.12.0. After upgrading to node 14.18, it worked.

internal/process/esm_loader.js:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. Received protocol 'node:'
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:756:11)
    at Loader.resolve (internal/modules/esm/loader.js:85:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:229:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:51:40)
    at link (internal/modules/esm/module_job.js:50:36) {
  code: 'ERR_UNSUPPORTED_ESM_URL_SCHEME'
}
ensodx-snedzhip commented 1 year ago

Same issue on Cloudflare Pages. It runs (as of now) 12.18.0, so the procedure is to add an environment variable with the key of NODE_VERSION and a value of at least 12.20.0. (Vercel and Render use 14.x AFAIK.)

This solved the issue on my side. Thank you so much!

pmpinto commented 1 year ago

As @brycewray points out above (thanks 👍), Cloudflare pages runs on Node 12.18.0 by default. It can, however, be bumped up until 17.x, according to https://developers.cloudflare.com/pages/platform/build-configuration/#language-support-and-tools

And as an alternative to what @brycewray suggest, creating an .nvmrc file in the root of the repo works just as well. I installed Node 17.9.1 (the most up to date possible) and this error is gone.

Once Cloudflare pages v2 comes out, and according to this discussion, it will be possible to install Node 18.