node.js - 使用配置文件导入 Typescript 模块

标签 node.js typescript

我想用一些配置文件伪造访问node_modules的路径。看来下面的事情是不可能做到的,那我该如何打造我的道路呢?

目的是写入一些node_modules的绝对路径(因为一些文件被分割,所以这是需要的)。

import someFileSettings from "./../../models/someFileSettings";
import * as request from JSON.stringify(someFileSettings .somePathIneed+"request");

最佳答案

不,这是不可能的,因为 TypeScript 模块遵循标准 ES6 模块。为此,有一个基于 promise 的模块加载器 API

这是 the book of Dr. Axel Rauschmayer 的解释:

16.9.1 Can I use a variable to specify from which module I want to import?

The import statement is completely static: its module specifier is always fixed. If you want to dynamically determine what module to load, you need to use the programmatic loader API:

const moduleSpecifier = 'module_' + Math.random();
System.import(moduleSpecifier)
.then(the_module => {
    // Use the_module
})

...但是,请注意此警告:

The module loader API is not part of the ES6 standard

关于node.js - 使用配置文件导入 Typescript 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39506429/

相关文章:

javascript - 使用 `NODE_MODULE_VERSION 70.` 而不是 NODE_MODULE_VERSION 48 重建 nbind

javascript - TinyMCE gulp 配置

javascript - 在 Electron 中运行 Jest 并使用 --inspect-brk

angular - 导入 PopupModule.forRoot() 时出错

typescript - typedef 就像 C/C++

javascript - 我无法访问我的异步验证器,因为它位于 "__zone_symbol"对象中

javascript - 运行 grunt-bower 抛出 _.object 不是函数错误

android - phonegap 无法安装或正常工作

javascript - 将授权 header 添加到 GET 请求

reactjs - 我的测试无法与带有 typescript 的 react 测试库一起使用