简体   繁体   English

如何在简单的 typescript 中导入节点模块

[英]How to import node modules in simple typescript

I'm new to TypeScript and I'm just trying to include a module that imports from a node module.我是 TypeScript 的新手,我只是想包含一个从节点模块导入的模块。 I'm not using webpack or any other build tools as I'm trying to keep this as basic as possible to understand.我没有使用 webpack 或任何其他构建工具,因为我试图保持尽可能基本的理解。

This is my structure:这是我的结构:

/node_modules    -- contains node modules I want to import in main.ts
/scripts/ts/main.ts
/scripts/main.js -- the transpiled main.ts file
/index.html      -- includes the module script tag for scripts/main.js
/package.json
/tsconfig.json

This is my tsconfig.json:这是我的 tsconfig.json:

{
    "compilerOptions": {
        "target": "ES6",
        "module": "ES2015",
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "outDir": "scripts",
        "moduleResolution": "Node",
        "experimentalDecorators": true
    },
    "$schema": "https://json.schemastore.org/tsconfig",
    "exclude": [],
    "include": [
        "scripts/ts/**/*"
    ]
}

My main.ts file has these imports:我的 main.ts 文件有这些导入:

import { css, html, LitElement } from 'lit';
import { customElement, property } from 'lit/decorators.js';

The main.ts file gets transpiled and main.js is loaded successfully. main.ts 文件被编译并且 main.js 被成功加载。 But I get this error:但我得到这个错误:

Uncaught TypeError: Failed to resolve module specifier "lit". Relative references must start with either "/", "./", or "../".

The import statements are transpiled "as is".导入语句按“原样”进行转换。 They look exactly the same in the transpiled file so I imagine the issue is that the module path is wrong: The browser does not know that "lit" is in "node_modules/lit" and when I manually change the path in the transpiled file to../node_modules/lit/index.js then I get another error which is它们在转译文件中看起来完全相同,所以我想问题是模块路径错误:浏览器不知道“lit”在“node_modules/lit”中,当我手动将转译文件中的路径更改为../node_modules/lit/index.js 然后我得到另一个错误是

Uncaught TypeError: Failed to resolve module specifier "@lit/reactive-element". Relative references must start with either "/", "./", or "../".

Which I imagine is a module imported down the heirarchy by other lit modules.我想这是一个由其他点亮模块沿层次结构导入的模块。 Basically my import paths are not transpiled properly and I don't know what to do.基本上我的导入路径没有正确转换,我不知道该怎么做。 Any help would be great!任何帮助都会很棒!

Thanks to @cdimitroulas for making me think about this.感谢@cdimitroulas 让我思考这个问题。

ES6 imports need to have an absolute or relative path - "plain node imports" simply don't work and these paths need to be resolved somehow not only for the main module's imports but for every other module's imports down the dependency graph. ES6 导入需要有一个绝对或相对路径——“普通节点导入”根本不起作用,这些路径需要以某种方式解析,不仅针对主模块的导入,而且针对依赖关系图中的每个其他模块的导入。

The Typescript compiler does not handle this and so the current setup was missing a tool that would resolve these paths (like web-dev-server) or bundle the modules and all their dependencies in one file (like webpack) thereby eliminating the need to resolve the paths. Typescript 编译器无法处理此问题,因此当前设置缺少可以解析这些路径(如 web-dev-server)或将模块及其所有依赖项捆绑在一个文件中(如 webpack)的工具,从而无需解析路径。

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

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