我在尝试将Typescripts模块解析与路径,json文件导入和动态导入一起使用时遇到一些问题。这就是我所做的:
"resolveJsonModule": true
以便能够导入.json文件我不确定,如果我在这里混淆了一些事情。但如果能够将以下三个功能结合使用,将不胜感激:
// 1. dynamic import
const subfolder = 'example'
let { ex } = await import(`./../../some/path/${subfolder}`)
// 2. json file import
import config from './../../config.json'
// 3. module resolution
// some/path/file_a.ts
import { example_function } from '@utils/ex'
...
const result = await example_function()
// src/utils/ex/index.ts
export async function example_function() {}
上面的代码通过编译器运行,并且编译时没有错误。 但是,当我尝试启动编译后的代码时,会出现如下错误:
➤ node lib/index.js
internal/modules/cjs/loader.js:670
throw err;
^
Error: Cannot find module '@utils/ex'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
at Function.Module._load (internal/modules/cjs/loader.js:591:27)
at Module.require (internal/modules/cjs/loader.js:723:19)
at require (internal/modules/cjs/helpers.js:14:16)
at Object.<anonymous> (/home/mod/cod/wo/thingylabs/calpobot/cb1/lib/utils/pipeline/pipelineRunChatops.js:52:12)
at Module._compile (internal/modules/cjs/loader.js:816:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
at Module.load (internal/modules/cjs/loader.js:685:32)
at Function.Module._load (internal/modules/cjs/loader.js:620:12)
at Module.require (internal/modules/cjs/loader.js:723:19)
{
"compilerOptions": {
"allowJs": false,
"lib": ["es2015", "es2017"],
"module": "commonjs",
"moduleResolution": "node",
"target": "es5",
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": false,
"pretty": true,
"strict": true,
"sourceMap": true,
"outDir": "./lib",
"skipLibCheck": true,
"noImplicitAny": true,
"esModuleInterop": true,
"declaration": true,
"resolveJsonModule": true,
"baseUrl": "./src",
"paths": {
"@utils/fs": ["utils/fs"],
"@pipelines/*": ["pipelines/*"],
"@jobs/*": ["jobs/*"],
}
},
"include": [
"src/**/*"
],
"compileOnSave": false
}
答案 0 :(得分:0)
您在此处讨论的问题:https://github.com/microsoft/TypeScript/issues/24715 作为解决方法,您可以替换
import config from './../../config.json'
与
const config = require('./../../config.json')