简体   繁体   English

在js文件中导入默认导出ts文件

[英]Import default export ts file in a js file

I have a.ts file that looks like this:我有一个看起来像这样的 .ts 文件:

// index.ts
import MyModule from './MyModule';
import MyModule2 from './MyModule2';

export default [
  MyModule,
  MyModule2
]

I'm trying to import this file in a js file, like so:我正在尝试将此文件导入 js 文件中,如下所示:

const modules = require('../../../../entity/index');

This works fine locally, but I get the following error in my production environment.这在本地工作正常,但在我的生产环境中出现以下错误。 What's the issue?有什么问题? Error: Cannot find module '../../../../entity/index'

edit: I changed the import to:编辑:我将导入更改为:

const models = require('./../../../../entity/index.ts').default;

Now the error is现在的错误是

import MyModule from 'MyModule';
^^^^^^^
SyntaxError: Cannot use import statement outside a module

which kinda makes sense.这有点道理。 How can I solve this?我该如何解决这个问题?

if you have already specified in your package.json the property如果您已经在 package.json 中指定了该属性

 "type": module,

try:尝试:

 import {Mymodule} from 'MyModule.js';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM