[英]Mocha and Import Syntax: Cannot find module
Maintainer of multiple npm packages here.多个 npm 包的维护者在这里。 Been using mocha with the require syntax and wanting to migrate to the import syntax .
一直在使用mocha和require 语法并希望迁移到import 语法。
The error I am getting is我得到的错误是
Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js
With the following three files有以下三个文件
src/index.js
export const sum = (a, b) => a + b;
test/index.spec.js
import { sum } from '../src/index';
const expect = require('chai').expect;
describe('Testing Index', () => {
it('Testing sum', () => {
expect(sum(7, 13)).to.equal(20);
});
});
package.json
{
"name": "mocha-debug",
"type": "module",
"main": "index.js",
"scripts": {
"test": "mocha \"./test/**/*.spec.js\""
},
"license": "ISC",
"devDependencies": {
"chai": "4.3.4",
"mocha": "9.1.4"
}
}
and using node v14.18.2
, run yarn install
and并使用 node
v14.18.2
,运行yarn install
和
yarn test
> `Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js`
I've found a related issue that recommends using babel with --require @babel/register
, but wasn't able to get over the error.我发现了一个相关问题,建议将 babel 与
--require @babel/register
一起使用,但无法克服该错误。
I've set up a test repo to make it easy to reproduce the issue我已经建立了一个测试仓库,以便于重现问题
https://github.com/simlu/mocha-debug https://github.com/simlu/mocha-debug
What am I doing wrong here?我在这里做错了什么? How do I get the tests to run successfully?
如何让测试成功运行?
I solved it by just adding the file extension in my case I was importing my mongodb model so I imported with the file extension.ts我通过在我的情况下添加文件扩展名来解决它我正在导入我的 mongodb model 所以我使用文件 extension.ts 导入
var Employee = require('../models/Employee.ts')
Which solved the issue哪个解决了这个问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.