你好
我正在尝试使我的项目的导入与打字稿导入路径一起使用。每次我遇到这个错误:错误[ERR_MODULE_NOT_FOUND]:找不到从 dist/index.js 导入的包“app”
我的印象是,当编译节点时,通过node_modules的外部库解释我的“app”导入路径,我不明白为什么
我的项目结构:
-> src
-> cards
index.ts
-> game
index.ts
-> player
index.ts
index.ts (entry)
-> dist
tsconfig.ts
package.json
当我使用相对路径时,我的项目如下所示:
import { Game } from "./game";
启动良好,但一旦我使用打字稿导入路径,它就不再起作用
tsconfig:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"outDir": "./dist",
"moduleResolution": "node",
"baseUrl": ".",
"esModuleInterop": true,
"paths": {
"app/*": ["./src/*"]
}
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}
Package.json
{
"name": "president",
"version": "1.0.0",
"description": "President - card game",
"main": "index.ts",
"author": "Yanis Sahnoune",
"license": "MIT",
"type": "module",
"scripts": {
"serve:dev": "tsx watch index.ts",
"start": "rm -rf dist/ && yarn && yarn tsc && node --es-module-specifier-resolution=node dist/index.js",
"clean": "rm -rf node_modules dist && yarn cache clean"
},
"dependencies": {
"linebyline": "^1.3.0",
"module-alias": "^2.2.3",
"ts-node": "^10.9.1",
"tsx": "^3.12.7",
"typescript": "^5.1.6",
"vitest": "^0.34.1"
},
"devDependencies": {
"@babel/core": "^7.22.10",
"@babel/node": "^7.22.10",
"@babel/preset-env": "^7.22.10",
"esm": "^3.2.25",
"tsconfig-paths": "^4.2.0"
}
}
如果有人可以帮助我,我无法解决,谢谢
我尝试像这样使用 babel :
rm -rf dist/ && yarn && tsc && babel-node --extensions .ts dist/index.js
好吧,我找到了我的问题。
在我的项目中,我使用 rollup 使用 javascript ES6 构建我的项目。我在 tsconfig 文件中写入我的别名,但汇总需要配置他的配置并行打字稿。我在 rollup.config.js 中添加:
{
input: 'src/index.ts',
output: {
file: 'dist/index.mjs',
sourcemap: true,
format: 'es',
},
watch: {
chokidar: true,
buildDelay: true
},
plugins: [
typescript(),
esbuild({
include: /\.[jt]sx?$/,
minify: true,
tsconfig: 'tsconfig.json',
sourceMap: true,
}),
alias({
entries:[
{ find: 'app', replacement: './src' },
],
resolve: ['.ts', '.js'],
})
],
}
我有 @rollup/plugin-typescript 包中的条目和 typescript() 函数。有效!