我正在嘗試發布和使用僅打字稿的庫(不dist帶編譯.js檔案的檔案夾)
我創建了簡單的庫:
源代碼/測驗/索引.ts:
export const test = 42;
源代碼/索引.ts:
import {test} from "./test";
export {
test
};
包.json:
{
"name": "test",
"version": "1.0.0",
"type": "module",
"main": "src/index.ts"
}
并將其發布到本地 npm 注冊表。
接下來,我創建了一個簡單的應用程式,我在其中安裝了這個庫作為依賴項。
源代碼/索引.ts:
import {test} from "test"
console.log(test);
包.json:
{
"name": "foo",
"version": "1.0.0",
"type": "module",
"main": "src/index.ts",
"scripts": {
"start": "node --experimental-specifier-resolution node --loader ts-node/esm src/index.ts"
},
"dependencies": {
"test": "^1.0.0",
"ts-node": "^10.4.0"
}
}
tsconfig.json:
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "node",
"esModuleInterop": true,
"target": "esnext",
"sourceMap": true
}
}
當我通過npm run start我運行它時TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected string to be returned for the "format" from the "loader getFormat" function but got type object.
uj5u.com熱心網友回復:
為什么會發生
問題是ts-node不會轉換您的node_modules檔案夾。
解決方案
所以你需要明確告訴ts-node處理你的庫。在您的情況下,您必須start將專案根目錄中的命令從
node --experimental-specifier-resolution node --loader ts-node/esm src/index.ts
到類似的東西(轉譯 ALL node_modules。小心,這是一種不好的做法)
set TS_NODE_IGNORE='' && node --experimental-specifier-resolution node --loader ts-node/esm src/index.ts
或者更好一點- 使用cross-env
cross-env TS_NODE_IGNORE='' node --experimental-specifier-resolution node --loader ts-node/esm src/index.ts
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/343865.html
