【发布时间】:2022-01-31 02:51:12
【问题描述】:
我经常喜欢使用ts-node 来执行单个文件。
我希望能够运行像 ts-node ./page/home.tsx 这样的任何东西。
我在我的 nextjs 项目中这样做时遇到了问题。
export const WidgetList = createWidget<ButtonListProps>(WidgetListProps)
^
TypeError: (0 , Widget_1.createWidget) is not a function
我的 tsconfig.json 看起来像这样,必须换掉 nextjs 项目默认的注释。
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
// "module": "esnext",
"module": "CommonJS",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
// "jsx": "preserve",
"jsx": "react",
"incremental": true,
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
没有module 评论我得到Cannot use import statement outside a module
如果没有 jsx 评论,我会收到 jsx 错误
更新
该应用程序完全可以使用所有导入功能。
我不确定我是否理解为什么 node 无法跟踪此导入。
我已经添加了一个新的tsconfig.node.json
{
"compilerOptions": {
"strictNullChecks": true,
"module": "NodeNext",
"jsx": "react",
"esModuleInterop": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
}
我尝试执行项目中的任何文件:
ts-node --project ./tsconfig.node.json ./components/widget/Widget.tsx
我遇到了同样的错误。
【问题讨论】:
-
createWidget 是从哪里来的?
-
我有一个导出文件“
/components/widget/Widget.tsx” -
使用
ts-node运行 React 组件文件的用例是什么? -
@juliomalves 我想测试文件和 console.log 一个变量
标签: typescript next.js