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

[vite-typescript] how to use 'import.meta' #3317

Closed
3 tasks done
w3cways opened this issue Aug 23, 2023 · 5 comments
Closed
3 tasks done

[vite-typescript] how to use 'import.meta' #3317

w3cways opened this issue Aug 23, 2023 · 5 comments

Comments

@w3cways
Copy link

w3cways commented Aug 23, 2023

Pre-flight checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project uses.
  • I have searched the issue tracker for a bug that matches the one I want to file, without success.

Electron Forge version

6.4.1

Electron version

26.0.0

Operating system

macOS 12.4

Last known working Electron Forge version

none

Expected behavior

when I use 'import.meta' in renderer.ts, prompt error :'The "import.meta" meta property is only allowed when the "--module" option is "es2020", "es2022", "esnext", "system", "node16", or "nodenext"'

tsconfig.json
{ "compilerOptions": { "target": "ESNext", "module": "commonjs", "allowJs": true, "skipLibCheck": true, "esModuleInterop": true, "noImplicitAny": true, "sourceMap": true, "baseUrl": ".", "outDir": "dist", "moduleResolution": "node", "resolveJsonModule": true }, "include": ["src"] }
if I change 'commonjs' to 'ESNext', then run command 'npm run start', got errors:
An unhandled rejection has occurred inside Forge: SyntaxError: Cannot use import statement outside a module

Is there a way to fix this?

Actual behavior

can use 'import.meta' in [vite-typescript]

Steps to reproduce

   use 'import.meta' in renderer.ts

Additional information

No response

@caoxiemeihao
Copy link
Member

You can try this!

{
  "compilerOptions": {
-   "module": "commonjs",
+   "module": "ESNext",
  }
}

🤔 However this seems to conflict here 👉 #3178 (comment)

@w3cways w3cways closed this as completed Aug 24, 2023
@yang131323
Copy link

You can try this!

{
  "compilerOptions": {
-   "module": "commonjs",
+   "module": "ESNext",
  }
}

🤔 However this seems to conflict here 👉 #3178 (comment)

Then when compiling with vite,i cannot use 'import.meta'.Is that so?

@wutiange
Copy link

wutiange commented Jan 6, 2024

@w3cways How did you finally solve this problem?

@w3cways
Copy link
Author

w3cways commented Jan 15, 2024

@w3cways How did you finally solve this problem?

{
"ts-node": {
// these options are overrides used only by ts-node
// same as our --compilerOptions flag and our TS_NODE_COMPILER_OPTIONS environment variable
"compilerOptions": {
"module": "commonjs"
}
},
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"isolatedModules": true,
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": true,
"sourceMap": true,
"baseUrl": ".",
"outDir": "dist",
"moduleResolution": "Node",
"resolveJsonModule": true,
"typeRoots": ["./types"],
"types": ["vite/client", "@types/node"]
},
"include": ["src/*", "./types"],
"exclude": ["node_modules"]
}

@jamesopti
Copy link

jamesopti commented Aug 14, 2024

What is the expected way to do this now?

I followed the setup using npm init electron-app@latest my-app -- --template=vite-typescript and still am unable to use import.meta

EDIT: I had left formats: ['es'], in the config.build.lib setting when debugging.

Removing it and going with @w3cways solution worked

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

No branches or pull requests

5 participants