#独家
请问,为何不能这样运行ts文件呢?

2023-08-11 0 3,514

请问,为何不能这样运行ts文件呢?

文件util.ts:import ip from ‘ip’ export const getLocalIPs = () => { //var ip = require(“ip”); console.dir ( ip.address() ); return ip.address() }

文件:__test__/util.test.tsexport default {} import { getLocalIPs } from ‘../util’ getLocalIPs()

然后我在执行:npx ts-node __test__/util.test.ts

的时候,报错:npx ts-node util.test.ts (node:61127) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension. (Use `node –trace-warnings …` to show where the warning was created) /Users/john/Desktop/projects/个人/nextjs/renderer/components/func/Traceroute/__test__/util.test.ts:1 import { getLocalIPs } from ‘../util’; ^^^^^^ SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1084:15) at Module._compile (node:internal/modules/cjs/loader:1119:27) at Module.m._compile (/Users/john/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1618:23) at Module._extensions..js (node:internal/modules/cjs/loader:1209:10) at Object.require.extensions.<computed> [as .ts] (/Users/john/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1621:12) at Module.load (node:internal/modules/cjs/loader:1033:32) at Function.Module._load (node:internal/modules/cjs/loader:868:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at phase4 (/Users/john/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/bin.ts:649:14) mba:__test__ john$ npx ts-node util.test.ts (node:61376) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension. (Use `node –trace-warnings …` to show where the warning was created) /Users/john/Desktop/projects/个人/nextjs/renderer/components/func/Traceroute/__test__/util.test.ts:1 export default {}; ^^^^^^ SyntaxError: Unexpected token ‘export’ at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1084:15) at Module._compile (node:internal/modules/cjs/loader:1119:27) at Module.m._compile (/Users/john/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1618:23) at Module._extensions..js (node:internal/modules/cjs/loader:1209:10) at Object.require.extensions.<computed> [as .ts] (/Users/john/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1621:12) at Module.load (node:internal/modules/cjs/loader:1033:32) at Function.Module._load (node:internal/modules/cjs/loader:868:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at phase4 (/Users/john/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/bin.ts:649:14)

请问,这个报错的原因是什么呢? 我已经在

文件:__test__/util.test.ts
添加了导出啊:export default {}


编辑1:我已经添加package.json

“type”: “module”,

但是现在报错找不到util模块:CustomError: Cannot find module ‘/Users/john/Desktop/projects/个人/nextjs/renderer/components/func/Traceroute/util’ imported from /Users/john/Desktop/projects/个人/nextjs/renderer/components/func/Traceroute/__test__/util.test.ts

请问这种情况还有什么问题呢?

package.json里设置”type”: “module”:”type”: “module”

或者tsconfig.json配置:{ “compilerOptions”: { “module”: “ESNext” } }

错误信息说的很清楚了:

Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.

改一下 package.json 里面的模式就好了。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. 有软官网所有资源来源于开发团队,加入会员即可下载使用!如有问题请联系右下角在线客服!
2. 有软官方保障所有软件都通过人工亲测,为每位会员用户提供安全可靠的应用软件、游戏资源下载及程序开发服务。
3. 有软团队针对会员诉求,历经多年拥有现今开发成果, 每款应用程序上线前都经过人工测试无误后提供安装使用,只为会员提供安全原创的应用。
4. PC/移动端应用下载后如遇安装使用问题请联系右下角在线客服或提交工单,一对一指导解决疑难。

有软官网_用软件,找有软 技术分享 请问,为何不能这样运行ts文件呢? https://www.jiaruvip.com/2481.html

有软应用商店是经过官方安全认证,保障正版软件平台

相关资源

官方客服团队

为您解决烦忧 - 24小时在线 专业服务