I want TypeScript to be able to write the access token in an external file (token.ts) and import and load it.

Asked 1 years ago, Updated 1 years ago, 83 views

I'm blogging in Next.js.If I write Github's personal access token directly in the code, I have trouble giving the code to Github, so I try to write the access token I don't want to post in an external file and ignore it with .gitignore, but it doesn't work.

What I did

  • Create token.ts in the root folder of next.js and const accessToken="access token";
  • I thought I could use it anywhere I wanted to use accessToken from '../token', but I got a red ~~ and got an error.

Error Contents

Token.ts side error with token

'token.ts'cannot be compiled under' -- isolatedModules' cause it is considered a global script file.Add an import, export, or import' export {}' statement to make it a module

Read side error

File'/Users/username/Desktop/mac_work/nodejs/blog/my-blog/token.ts' is not a module.

javascript typescript next.js

2022-09-29 22:11

1 Answers

File to load token.ts

I forgot
//export.
export const accessToken="token";

On the file side you want to load

I forgot to enclose it with
//{}.
import {accessToken} from '../token'


2022-09-29 22:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.