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

Vue Typescript starter - import.meta.env unusuable #101

Closed
zefman opened this issue Jun 17, 2021 · 21 comments
Closed

Vue Typescript starter - import.meta.env unusuable #101

zefman opened this issue Jun 17, 2021 · 21 comments

Comments

@zefman
Copy link

zefman commented Jun 17, 2021

Hey currently you cannot use Vite's enviroment variables with the Vue typescript starter because the the tsconfig module option is set to "CommonJS".

tsconfig.json
{
  "compilerOptions": {
    "strict": true,
    "module": "CommonJS",
    "target": "ES2020",
    "lib": ["DOM"],
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "paths": {
      "~/*": ["./src/*"]
    },
  },
  "ts-node": {
    "transpileOnly": true
  }
}

Screenshot 2021-06-17 at 14 50 55

Is there a way around this?

@brillout
Copy link
Member

Would declare global do the trick? Otherwise overriding by defining a separate pages/tsconfig.js?

@zefman
Copy link
Author

zefman commented Jun 17, 2021

Unfortunately neither of those suggestions seem to work. If I ignore the the typescript errors and log import.meta it is missing the env object as well. I'm not too clear on how this is all working with Vite (first time using it) but will continue to poke around to see if I can figure it out.

@brillout
Copy link
Member

@brillout
Copy link
Member

See d478bec for how to change your tsconfig.json to support import.meta.

Let me know if you have any other questions.

@stephensamra
Copy link
Contributor

@brillout, those changes to tsconfig.json did not work for me. In server/index.ts, I'm trying to import a file from src that contains references to import.meta, and I am getting this error when I run yarn dev:

/Users/stephen/.../src/config.ts:4
    graphqlEndpoint: import.meta.env.VITE_GRAPHQL_ENDPOINT,
                            ^^^^

SyntaxError: Cannot use 'import.meta' outside a module

My tsconfig.json file:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": [
      "DOM",
    ],
    "types": [
      "vite/client"
    ],
    "baseUrl": ".",
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "ES2020",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "noImplicitAny": false,
    "paths": {
      "src/*": [
        "./src/*"
      ]
    }
  },
  "include": [
    "./src",
    "./server"
  ],
  "ts-node": {
    "transpileOnly": true,
    "compilerOptions": {
      "module": "CommonJS"
    }
  }
}

Any help would be appreciated, thanks.

@brillout
Copy link
Member

But your IDE is not complaining right?

How is your yarn dev script defined?

Worst case: bisec between your project and the boilerplate until you find the root cause

@stephensamra
Copy link
Contributor

stephensamra commented Jul 19, 2021

Correct, the IDE no longer shows it as an error. The yarn dev script is: ts-node ./server. I will compare the project and the example and narrow it down. Thanks.

@brillout
Copy link
Member

brillout commented Jul 19, 2021 via email

@deckchairlabs
Copy link

I didn't think import.meta would exist outside of the context of vite?

@stephensamra
Copy link
Contributor

@brillout thanks, I will look into that.

@brillout
Copy link
Member

@deckchairlabs That is correct. In particular import.meta doesn't exist in server.js. But better to have false positive than not being able to use import.meta at all.

@stephensamra
Copy link
Contributor

@brillout, you were right that ts-node was not picking up the tsconfig.json#ts-node options.

Adding a console.log inside ts-node for those options showed:

{
  "transpileOnly": undefined,
  "compilerOptions": undefined
}

However, when I inline those options in the package.json script like so:

"scripts": {
  "dev": "ts-node --transpile-only --compiler-options '{\"module\":\"CommonJS\"}' ./server",
}

... it shows:

{
  "transpileOnly": true, 
  "compilerOptions": { 
    "module": "CommonJS"
  }
}

Unfortunately, even with those options set, I still get the same error so I'm still looking into it. Thanks for the help thus far though!

@brillout
Copy link
Member

brillout commented Jul 19, 2021 via email

@chesterlaykin
Copy link

chesterlaykin commented Sep 20, 2022

Hello , I have also get import.meta errors when running thereact-ts project.
This happens when I install npm init vite-plugin-ssr@latest (v: 0.4.36) with the react-ts option, when starting the server.

The errors I get with react-ts begin with:
[vite] warning: "import.meta" is not available in the configured target environment ("ES2017") and will be empty
and then shows where the error occured, for example:

42 |
43 |  $RefreshReg$(_c, "PageContextProvider");
44 |  if (import.meta.hot) {
   |      ^
45 |    window.$RefreshReg$ = prevRefreshReg;
46 |    window.$RefreshSig$ = prevRefreshSig;

  Plugin: vite:esbuild
  File: (myproject)/renderer/usePageContext.tsx

The "window.$RefreshReg$code comes from plugin-react\dist\index.cjs.

tsconfig has"module": "ES2020"and

"ts-node": {
    "transpileOnly": true,
    "compilerOptions": {
      "module": "CommonJS"
    }
  }

@brillout
Copy link
Member

@chesterlaykin I can't reproduce with:

$ pnpm create vite-plugin-ssr@latest
# Select `react-ts`
$ pnpm install
$ pnpm exec tsc --noEmit

@chesterlaykin
Copy link

Here is a reproduction: .
I haven't run that "exec tsc" command though, is that required? I use yarn , but yarn exec tsc doesn't work.

I ran:
npm init vite-plugin-ssr@latest , (react-ts option)
yarn install
yarn run dev

@brillout
Copy link
Member

Alright, I could reproduce. It's a Vite bug; the best would be to create a new GitHub ticket over at Vite's repository and reference the reproduction you made. In the meantime, you can pin Vite to 3.0.9.

@chesterlaykin
Copy link

Thanks, I posted the issue and there is a workaround mentioned,
to set esbuild.target to 'esnext' (in vite config I guess).

  esbuild: {
    target: 'esnext'
  }

which works.
Otherwise, changing tsconfig "target": to "esnext" also got rid of the error.

@brillout
Copy link
Member

@chesterlaykin Thanks for the update 👌. Tracking issues: #461 and vitejs/vite#10167.

@bob-user
Copy link

@brillout, those changes to tsconfig.json did not work for me. In server/index.ts, I'm trying to import a file from src that contains references to import.meta, and I am getting this error when I run yarn dev:

/Users/stephen/.../src/config.ts:4
    graphqlEndpoint: import.meta.env.VITE_GRAPHQL_ENDPOINT,
                            ^^^^

SyntaxError: Cannot use 'import.meta' outside a module

My tsconfig.json file:

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": [
      "DOM",
    ],
    "types": [
      "vite/client"
    ],
    "baseUrl": ".",
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "ES2020",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react",
    "noImplicitAny": false,
    "paths": {
      "src/*": [
        "./src/*"
      ]
    }
  },
  "include": [
    "./src",
    "./server"
  ],
  "ts-node": {
    "transpileOnly": true,
    "compilerOptions": {
      "module": "CommonJS"
    }
  }
}

Any help would be appreciated, thanks.

@stephensamra did you have any luck with that ?

@stephensamra
Copy link
Contributor

@bob-user I don't remember how we got around the problem in the end, and I don't have the access to check. It's possible we ended up using dotenv to handle it.

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

No branches or pull requests

6 participants