微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基本的webpack打字稿示例在node_module包含TS7016上失败

如何解决基本的webpack打字稿示例在node_module包含TS7016上失败

遵循官方的Webpack打字稿集成指南https://webpack.js.org/guides/typescript/,但操作失败。我想念什么?

ERROR in /Users/kevzettler/code/hypeworks/src/index.ts
./src/index.ts
[tsl] ERROR in /Users/kevzettler/code/hypeworks/src/index.ts(1,21)
      TS7016: Could not find a declaration file for module 'lodash'. '/Users/kevzettler/code/hypeworks/node_modules/lodash/lodash.js' implicitly has an 'any' type.
ℹ 「wdm」: Failed to compile.

index.ts:

 import * as _ from 'lodash';

  function component() {
    const element = document.createElement('div');

    element.innerHTML = _.join(['Hello','webpack'],' ');

    return element;
  }

document.body.appendChild(component());

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/","sourceMap": true,"noImplicitAny": true,"module": "es6","target": "es5","jsx": "react","allowJs": true,"moduleResolution": "node","typeRoots": [
      "node_modules/@types"
    ],"lib": [
      "es2017","dom"
    ]
  }
}

webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/index.ts',devtool: 'inline-source-map',mode: 'production',module: {
    rules: [
      {
        test: /\.tsx?$/,use: 'ts-loader',exclude: /node_modules/,},],resolve: {
    extensions: [ '.tsx','.ts','.js' ],output: {
    filename: 'bundle.js',path: path.resolve(__dirname,'dist'),};

package.json

{
  "name": "derp","version": "0.0.1","description": "","private": true,"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1","start": "webpack-dev-server --mode development"
  },"keywords": [],"author": "","license": "ISC","devDependencies": {
    "ts-loader": "^8.0.4","typescript": "^4.0.3","webpack": "^4.44.2","webpack-cli": "^3.3.12"
  },"dependencies": {
    "lodash": "^4.17.20","uuid": "^8.3.1","webpack-dev-server": "^3.11.0"
  }
}

解决方法

您需要安装lodash类型,因为它们直接在lodash软件包中不可用。

npm install --save @types/lodash

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。