简体   繁体   English

“不能在模块外使用 import 语句” - JavaScript 模块导入到 TypeScript 文件中,JavaScript 模块内有其他导入

[英]"Cannot use import statement outside of module" - JavaScript module imported into TypeScript file, JavaScript module has other imports within

To further explain, it can be imagined that I have a set of JavaScript modules that are triggered from one initial module, which is imported into an Angular component.为了进一步解释,可以想象我有一组 JavaScript 模块,这些模块是从一个初始模块触发的,该模块被导入到 Angular 组件中。

What I have tried:我试过的:

I am using "type": "module" in the package.json file and have declared all scripts as modules in a typings file, I have also declared them under scripts in angular.json. I am using "type": "module" in the package.json file and have declared all scripts as modules in a typings file, I have also declared them under scripts in angular.json. I went as far as settings the scripts as modules in index.html, but I still receive the "Uncaught SyntaxError: Cannot use import statement outside a module".我将脚本设置为 index.html 中的模块,但我仍然收到“未捕获的语法错误:无法在模块外使用导入语句”。

When the error occurs:发生错误时:

The error comes from the first import statement from within the initial JavaScript module, eg, line 1.该错误来自初始 JavaScript 模块中的第一个导入语句,例如第 1 行。

The import statement simply looks like this from within the initial JavaScript module, with subsequent statements to other scripts, all of which will trigger the same problem.导入语句在最初的 JavaScript 模块中看起来像这样,随后对其他脚本的语句,所有这些都会触发相同的问题。

import * as objParser from './scripts/objParser.js'

I can confirm that the issue is not the code logic, since the code does actually work in development mode, but of course, I am unable to run this as a production package.我可以确认问题不在于代码逻辑,因为代码确实在开发模式下工作,但当然,我无法将其作为生产 package 运行。

Maybe the issue?也许是问题?

My idea is that Angular is not validating these JavaScript files as modules since they do not use import within the Angular code base.我的想法是 Angular 没有将这些 JavaScript 文件验证为模块,因为它们不使用 Angular 代码库中的导入。 Any Ideas?有任何想法吗?

use const objParser = require('./scripts/objParser.js');使用const objParser = require('./scripts/objParser.js'); . .

You can create an index.ts file in your scripts folder which will allow you to import all modules from that file.您可以在脚本文件夹中创建一个index.ts文件,该文件将允许您从该文件导入所有模块。

you add them to the index.ts like您将它们添加到index.ts中,例如

export * from './scripts/objParser.js';

then you could import like然后你可以像导入

import { ObjParser } from './scripts'

if it's not compatible it could be because they aren't typescript libraries.如果它不兼容,可能是因为它们不是 typescript 库。

暂无
暂无

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

相关问题 未捕获的语法错误:无法在模块外使用 import 语句。 将 typescript 文件导入 JavaScript 文件 - Uncaught SyntaxError: Cannot use import statement outside a module. import typescript file into JavaScript file Javascript - 不能在模块外使用导入语句 - Javascript - Cannot use import statement outside a module 运行编译的 Javascript 代码时,TypeScript 文件中出现“语法错误:无法在模块外使用导入语句”错误 - "SyntaxError: Cannot use import statement outside of a module" error in a TypeScript file when running compiled Javascript code Javascript 模块错误语法错误:不能在模块外使用导入语句 - Javascript Module error SyntaxError: Cannot use import statement outside a module WordPress JavaScript 导入错误 不能在模块外使用导入语句 - WordPress JavaScript import error Cannot use import statement outside a module Javascript 模块 - 无法在模块外使用导入语句 - Javascript modules - Cannot use import statement outside a module Javascript 模块语法错误:无法在模块外使用导入语句 - Javascript Modules SyntaxError: Cannot use import statement outside a module 未捕获的语法错误:无法在模块外使用导入语句 - Javascript - Uncaught SyntaxError: Cannot use import statement outside a module - Javascript 不能在模块外使用 import 语句(Vanilla Javascript) - Cannot use import statement outside a module(Vanilla Javascript) 无法在后端使用 typescript 的模块外部使用导入语句 - Cannot use import statement outside a module using typescript on backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM