将使用 ParcelJS 构建的 react 应用程序迁移到 Typescript/React,我遇到了一些环境变量问题。实际上,这个问题似乎只影响错误输出,因为环境变量(URL,在<a>
标签中使用)在网页上正常工作。
这是终端输出:
/Users/---/Projects/---/---/src/cart/index.tsx(xxx,xxx)
Cannot find name 'process'.
126 | method: "post",
127 | url: `${process.env.URL}/checkout`,
| ^^^^^^^
128 | data: cart,
我尝试了几种方法,因为import * as process from "process"
不能解决问题:
Could not find a declaration file for module 'process'.
'./node_modules/process/index.js' implicitly has an 'any' type.
Try `npm install @types/process` if it exists or add a new
declaration (.d.ts) file containing `declare module 'process';`
> 6 | import * as process from "process";
| ^^^^^^^^^
但是,如果我尝试输出有效的变量:
const ENV = process.env.MIDDLEWARE_URL;
console.log('ENV', ENV);
浏览器输出:
ENV http://localhost:3000
最后,我不知道这是 Parcel 问题还是 TSConfig 问题。有任何想法吗 ?