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

Config should be parsed with a target what supports import.meta #1499

Closed
2 of 3 tasks
sionzee opened this issue Jan 12, 2021 · 0 comments
Closed
2 of 3 tasks

Config should be parsed with a target what supports import.meta #1499

sionzee opened this issue Jan 12, 2021 · 0 comments

Comments

@sionzee
Copy link

sionzee commented Jan 12, 2021

⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.

  • Read the docs.
  • Use Vite >=2.0. (1.x is no longer supported)
  • If the issue is related to 1.x -> 2.0 upgrade, read the Migration Guide first.

Describe the bug

You are not able to declare __dirname in ESM mode because import.meta is not available in the vite.config.ts.
I'm using the __dirname to find packages in monorepo.

Reproduction

  1. Set your package's target to module.
  2. Create vite.config.ts and put there:
import {dirname} from "path";
import {fileURLToPath} from "url";

const metaUrl = import.meta.url;
console.log("meta-url", metaUrl);
const __dirname = dirname(fileURLToPath(metaUrl));

export default {};
  1. Run vite
    You will get undefined for metaUrl and an error for fileURLtoPath cuz it expects a string.

System Info

  • vite version: this is happening since beta 22
  • Operating System: Windows 10 but using WSL
  • Node version: 14.13.0
  • Package manager (npm/yarn/pnpm) and version: yarn 1.22.5

Logs (Optional if provided reproduction)

$ DEBUG=* yarn vite
yarn run v1.22.5
$ <workspace>/node_modules/.bin/vite
"import.meta" is not available in the configured target environment and will be empty
2  |  import {fileURLToPath} from "url";
3  |
4  |  const metaUrl = import.meta.url;
|                  ^
5  |  console.log("meta-url", metaUrl);
6  |  const __dirname = dirname(fileURLToPath(metaUrl));

meta-url undefined
failed to load config from <workspace>/vite.config.ts
error when starting dev server:
  TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string or an instance of URL. Received undefined
at fileURLToPath (internal/url.js:1348:11)
at data:text/javascript,import { dirname } from 'path';
import { fileURLToPath } from 'url';

const import_meta = {};
const metaUrl = import_meta.url;
console.log("meta-url", metaUrl);
const __dirname = dirname(fileURLToPath(metaUrl));
var vite_config = {};

export default vite_config;
:1:182
at ModuleJob.run (internal/modules/esm/module_job.js:146:23)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async Loader.import (internal/modules/esm/loader.js:165:24)
at async loadConfigFromFile (<workspace>/node_modules/vite/dist/node/chunks/dep-478c65b9.js:46855:31)
at async resolveConfig (<workspace>/node_modules/vite/dist/node/chunks/dep-478c65b9.js:46650:28)
at async createServer$2 (<workspace>/node_modules/vite/dist/node/chunks/dep-478c65b9.js:65202:20)
at async CAC.<anonymous> (<workspace>/node_modules/vite/dist/node/cli.js:681:24)
  error Command failed with exit code 1.
  info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
@github-actions github-actions bot locked and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant