Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find module 'path'

Tags:

I'm attempting to learn Typescript and thought I should also make my webpack config in .ts. This is my 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:

  "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:

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?

like image 950
As above so below Avatar asked Mar 15 '18 19:03

As above so below


People also ask

Can not find the module?

If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed to the node command points to a file that exists. For example, if you run node src/index. js , make sure that the path src/index. js points to an existing file.

How do you solve Cannot find module or its corresponding type declarations?

The "Cannot find module or its corresponding type declarations" error occurs when TypeScript cannot locate a third-party or local module in our project. To solve the error, make sure to install the module and try setting moduleResolution to node in your tsconfig. json file.

Can not find module npm?

Set the Windows environment variable for NODE_PATH. This path is where your packages are installed. It's probably something likeNODE_PATH = C:\Users\user\node_modules or C:\Users\user\AppData\Roaming\npm\node_modules. Start a new cmd console and npm should work fine.

Can not find the Module Express?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.


1 Answers

This should help

npm i @types/node -D

Typescript needs typings for any module, except if that module is not written in typescript.

like image 93
Nurbol Alpysbayev Avatar answered Sep 20 '22 13:09

Nurbol Alpysbayev