简体   繁体   English

找不到模块“路径”

[英]Cannot find module 'path'

I'm attempting to learn Typescript and thought I should also make my webpack config in .ts .我正在尝试学习 Typescript,并认为我也应该在.ts中进行 webpack 配置。 This is my webpack.config.ts :这是我的webpack.config.ts

import * as webpack from 'webpack';
import * as path from 'path';

const config: webpack.Configuration = {
    entry: path.resolve('src/main.ts'),

    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },

    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },

    output: {
        filename: 'index.js',
        path: path.resolve( 'dist')
    }
}

export default config;

As well as my package.json :以及我的package.json

  "main": "index.js",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "webpack --config devtools/webpack.config.ts --display-error-details",
    "post-build": "webpack-dev-server --config devtools/webpack.config.ts --mode development"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-loader": "^4.0.1",
    "ts-node": "^5.0.1",
    "typescript": "^2.7.2",
    "webpack": "^4.1.1",
    "webpack-cli": "^2.0.12",
    "webpack-dev-server": "^3.1.1"
  }
}

The error I get when running npm run build is:运行 npm run build 时出现的错误是:

TS2307: Cannot find module 'path'

I have also tried requiring path, but then I get a different error saying it cant find module require.我也尝试过要求路径,但后来我得到一个不同的错误,说它找不到模块要求。

What seems to be the issue?似乎是什么问题?

This should help这应该有帮助

npm i @types/node -D

Typescript needs typings for any module, except if that module is not written in typescript. Typescript 需要为任何模块输入类型,除非该模块不是用 typescript 编写的。

Using使用

"types": ["node"]

in tsconfig.json as mentioned in the comments, solved the issue for me.在评论中提到的tsconfig.json中,为我解决了这个问题。

Try using require syntax rather than import & change webpack.config.ts to the following code尝试使用require语法,而不是将webpack.config.ts import和更改为以下代码

webpack.config.ts webpack.config.ts

const webpack = require('webpack');
const path = require('path');

const config: webpack.Configuration = {
    entry: path.resolve('src/main.ts'),

    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/
            }
        ]
    },

    resolve: {
        extensions: ['.tsx', '.ts', '.js']
    },

    output: {
        filename: 'index.js',
        path: path.resolve( 'dist')
    }
}

module.exports = config;

And then run npm run build然后运行npm run build

First of all no need of .ts extension for webpack config file.首先,webpack 配置文件不需要.ts扩展名。 Its just internal purpose for building the bundle.它只是构建捆绑包的内部目的。 Use normal .js file.使用普通的.js文件。

Webpack is not ran by browser, its by Node Js which runs webpack module and make bundle as per config. Webpack 不是由浏览器运行的,它由运行 webpack 模块并根据配置生成包的Node Js运行。

Now Node Js understand its own module system is which is require现在Node Js明白了它自己的模块系统是require

So it would be like below: require in below is Node Js module importing syntax.所以它会像下面这样:下面的require是 Node Js 模块导入语法。

const webpack = require('webpack');
const path = require('path');

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

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