[英]What is the best setting for tsconfig.json working with node.js modules?
到目前为止,我在“./src”中有 2 个文件: index.ts
和setConfig.ts
。 像这样导入'fs'和'path':
const fs = require('fs');
const path = require('path');
...这就是 Typescript 显然不喜欢的; 编译时说:
src/index.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'fs'.
1 const fs = require('fs');
~~
src/setConfig.ts:1:7
1 const fs = require('fs');
~~
'fs' was also declared here.
src/index.ts:2:7 - error TS2451: Cannot redeclare block-scoped variable 'path'.
2 const path = require('path');
~~~~
src/setConfig.ts:2:7
2 const path = require('path');
~~~~
'path' was also declared here.
src/setConfig.ts:1:7 - error TS2451: Cannot redeclare block-scoped variable 'fs'.
1 const fs = require('fs');
~~
src/index.ts:1:7
1 const fs = require('fs');
~~
'fs' was also declared here.
src/setConfig.ts:2:7 - error TS2451: Cannot redeclare block-scoped variable 'path'.
2 const path = require('path');
~~~~
src/index.ts:2:7
2 const path = require('path');
~~~~
'path' was also declared here.
Found 4 errors.
但是当我在setConfig.ts
中忽略它时,节点抱怨它不知道'fs'......
我的tsconfig.json
看起来像这样:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "./dist/",
"rootDir": "./src/",
"strict": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
}
}
那么,我还需要添加什么或做什么才能使我编译的 JavaScript 正常工作?
在setConfig.ts
模块中添加导出应该可以解决问题。
// setConfig.ts
export default {
// your exports
};
// Or
export function foo() {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.