为什么我的模块不在 otuput 文件夹中?



考虑以下文件:

mymodule.mjs

export const answer = 42

tsconfig.json

{
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
"baseUrl": ".",
"skipLibCheck": true,
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"declarationMap": true,
"inlineSources": false,
"declaration": true,
"stripInternal": true,
"lib": [
"es2016",
"dom"
],
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": true
},
"include": [
"src"
]
}

package.json:

{
"name": "deleteme2",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.1.0",
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.0.0",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "4.0.3",
"typescript": "^4.1.2",
"web-vitals": "^1.0.1"
},
"scripts": {
"compile": "tsc", 
"build": "yarn run compile",
"start": "node lib/server.mjs",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

使用

编译成功$ yarn run build

然而,当我试图启动它yarn run start时,我得到错误:

$ node lib/server.mjs
internal/modules/cjs/loader.js:892
throw err;
^
Error: Cannot find module 'C:Usersuser1Desktopdeleteme2libserver.mjs'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
at Function.Module._load (internal/modules/cjs/loader.js:745:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
error Command failed with exit code 1.

为什么server.mjs不在输出文件夹中?

因为你试图用tsc编译.mjs文件。

tsc只支持.ts,.tsx文件,因为它只用于将TypeScript编译为JavaScript。所以你不能用它来编译.mjs,.js或任何其他文件类型。

如果您将文件更改为mymodule.ts并将其放入src/中(因为您只在tsconfig.json中包含src),那么它应该按预期工作。在这种情况下,输出文件将在.js中,因此您需要运行node lib/myModule.js

相关内容

  • 没有找到相关文章

最新更新