繁体   English   中英

Typescript module not found 编译时出错

[英]Typescript module not found errors when compiling

当我编译 typescript 时,出现如下错误。 我使用webpack-dev-server运行它。

Module not found: Error: Can't resolve 'i18n' in ...
Module not found: Error: Can't resolve '../moment' in ...

但是在我这样导入它们的代码中

import * as i18n from "i18n";
import * as moment from 'moment';

并且没有下划线表示 static 检查器确定它已找到。

tsconfig.js

{
    "compilerOptions": {
        "outDir": "./dist",
        "noImplicitAny": true,
        "moduleResolution": "Node",
        "resolveJsonModule": true,
        "module": "ES2020",
        "target": "ES2020",
        "lib": [
            "ES2020"
        ],
        "allowJs": false,
        "alwaysStrict": true
    },
    "include": [
        "./src"
    ]
}

webpack.config.js

const path = require('path');

module.exports = {
    entry: './src/bot.ts',
    module: {
        rules: [
            {
                test: /\.ts$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
    resolve: {
        extensions: ['.ts'],
    },
    output: {
        filename: 'bot.js',
        path: path.resolve(__dirname, 'dist')
    }
};

package.json

{
    "name": "app",
    "version": "1.0.0",
    "description": "",
    "scripts": {
        "start": "npm dist/bot",
        "dev": "webpack-dev-server",
        "build": "webpack --mode production",
        "lint": "npx eslint . --ext .ts"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "@types/i18n": "^0.8.6",
        "@types/jest": "^26.0.7",
        "@types/moment": "^2.13.0",
        "@types/node": "^14.0.26",
        "@types/node-schedule": "^1.3.0",
        "@typescript-eslint/eslint-plugin": "^3.7.0",
        "@typescript-eslint/parser": "^3.7.0",
        "chokidar": "^3.4.1",
        "eslint": "^7.5.0",
        "eslint-plugin-jest": "^23.18.2",
        "ts-loader": "^8.0.1",
        "typescript": "^3.9.7",
        "webpack": "^4.20.2",
        "webpack-cli": "^3.1.2",
        "node-schedule": "^1.3.2",
        "webpack-dev-server": "^3.11.0"
    },
    "dependencies": {
        "discord.js": "^12.2.0",
        "i18n": "^0.10.0",
        "moment": "^2.27.0"
    }
}

我怎样才能解决这个问题?

您可能需要在 VS Code 上重新启动 typescript。

一种方法是

  1. go 在菜单中“开始”,
  2. select '转到文件'
  3. 键入大于号 (>)
  4. 然后输入“TypeScript:重启 TS 服务器”
  5. 按回车

您也许可以使用快捷键来更快地访问菜单项 Control-shift-P,以跳到第 4 步。

暂无
暂无

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

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