[英]Mocha and Import Syntax: Cannot find module
多個 npm 包的維護者在這里。 一直在使用mocha和require 語法並希望遷移到import 語法。
我得到的錯誤是
Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js
有以下三個文件
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"
}
}
並使用 node v14.18.2
,運行yarn install
和
yarn test
> `Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js`
我發現了一個相關問題,建議將 babel 與--require @babel/register
一起使用,但無法克服該錯誤。
我已經建立了一個測試倉庫,以便於重現問題
https://github.com/simlu/mocha-debug
我在這里做錯了什么? 如何讓測試成功運行?
我通過在我的情況下添加文件擴展名來解決它我正在導入我的 mongodb model 所以我使用文件 extension.ts 導入
var Employee = require('../models/Employee.ts')
哪個解決了這個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.