test文件夹下有一个developer.test.ts文件,第一句就是import * as assert from 'assert/strict',没有显示语法错误,我也检查了node_modules/@types/node/asset/strict.d.ts这个文件是存在的,代码如下:
declare module 'assert/strict' {
import { strict } from 'node:assert';
export = strict;
}
declare module 'node:assert/strict' {
import { strict } from 'node:assert';
export = strict;
}
当我用如下命令:npm run test:developer 测试文件时,报错:Error: Cannot find package 'assert' imported from C:\Users\xiaola\btp1\ase\test\developer.test.ts
tsconfig.json文件如下:
{
"compilerOptions": {
"module": "Node16",
"outDir": "dist",
"rootDir": ".",
"target": "ES2020",
"strict": true,
"skipLibCheck": true,
"sourceMap": true,
"lib": ["ES2020"]
},
"include": [
"src/**/*.ts",
"test/**/*.ts"
],
// Most ts-node options can be specified here using their programmatic names.
"ts-node": {
"esm": true,
"compilerOptions": {
// compilerOptions specified here will override those declared below,
// but *only* in ts-node. Useful if you want ts-node and tsc to use
// different options with a single tsconfig.json.
},
}
}
node的版本对吗?用16+的看看,你的配置没啥问题
该回答引用GPTᴼᴾᴱᴺᴬᴵ
错误信息提示无法找到导入的包'assert',这个包是在文件'developer.test.ts'中被导入的。看起来,导入语句试图从'assert/strict'路径导入'assert'模块,但这不是'assert'模块的有效路径。
一个可能的解决方法是将'developer.test.ts'中的导入语句更改为直接从'node'模块导入'assert'模块:
import * as assert from 'node:assert';
这应该可以正常工作,无需在tsconfig.json文件中声明任何模块。
如果您仍想从'assert/strict'路径导入'assert'模块,可以尝试以下步骤:
1.确保'assert'模块已安装在您的项目中,通过在项目目录中运行命令'npm install assert'来安装它。
2.将以下行添加到tsconfig.json文件中,以便将'assert'模块包含在编译过程中:
"compilerOptions": {
"types": ["node", "assert"]
},
3.将'developer.test.ts'中的导入语句更改为:
import * as assert from 'assert/strict';
这应该允许TypeScript编译器在编译'developer.test.ts'中的代码时找到'assert'模块。
参考GPT的回答和自己的思路,问题可能出现在测试文件中的import语句。您应该直接从“assert”中导入,而不是从“assert/strict”中导入。以下是您可以如何修改测试文件的方式:
import * as assert from 'assert';
// 其余的代码
这样应该可以解决“找不到导入的包'assert'”的问题。另外,由于您正在使用TypeScript,因此不需要在类型文件中声明assert/strict的模块。您可以从typings文件中删除以下行:
declare module 'assert/strict' {
import { strict } from 'node:assert';
export = strict;
}
进行这些更改后,您应该能够无任何问题地运行测试。
参考gpt和自己的思路,这个错误可能是由于你的 TypeScript 编译器没有正确地解析 assert/strict 模块导入语句。你可以尝试在 tsconfig.json 文件的 compilerOptions 中添加一个 "types": ["node"] 配置项来解决这个问题,它会告诉编译器从 node_modules/@types/node 文件夹中加载 node 类型定义。如果还是没有解决问题,你可以尝试将 assert/strict 模块导入语句改为 import * as assert from 'assert',这是因为 assert/strict 是从 Node.js v9.9.0 开始引入的,你可能需要更新你的 Node.js 版本才能使用它。
如果以上方法都没有解决问题,你可以检查一下你的工程目录是否有重名模块,或者尝试删除 node_modules 文件夹,再重新安装依赖。
没有安装moudle:assert
pip3 install assert
希望对你的身体有帮助