简体   繁体   English

Node.js + Typescript + Webpack =找不到模块

[英]Node.js + Typescript + Webpack = Module not found

I'm new with Webpack, Node.js and Typescript and I'm having trouble configuring my dev enviroment. 我是Webpack,Node.js和Typescript的新手,无法配置我的开发环境。

When running webpack to compile my src/server.ts to generate the /server/bundle.js I'm getting this error: 当运行webpack编译我的src/server.ts生成/server/bundle.js ,出现此错误:

ERROR in ./src/server.ts
Module not found: Error: Can't resolve 'hapi' in '/Volumes/Dados/giovane/dev/studio-hikari/nodang/nodang-api/src'
 @ ./src/server.ts 3:11-26

The architecture of the project is: 该项目的体系结构为:

在此处输入图片说明

The src/server.ts : src/server.ts

import * as Hapi from 'hapi';

const server = new Hapi.Server();

The webpack.config.js : webpack.config.js

const path = require('path');

module.exports = {
  entry: './src/server.ts',
  output: {
    filename: './server/bundle.js'
  },
  resolve: {
    extensions: ['.ts'],
    modules: [
      path.resolve('src'),
      path.resolve('node_modules')
    ]
  },
  module: {
    loaders: [
      {
        test: /.ts$/,
        loader: 'awesome-typescript-loader'
      }
    ]
  }
};

The package.json : package.json

{
  "name": "nodang-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "compile": "webpack --progress --watch",
    "serve": "node-dev server/bundle.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/hapi": "^16.0.0",
    "lodash": "^4.17.4"
  },
  "devDependencies": {
    "awesome-typescript-loader": "^3.0.8",
    "tsd": "^0.6.5",
    "typescript": "^2.2.1",
    "webpack": "^2.2.1"
  }
}

OBS: It's webpack 2 OBS:这是webpack 2

UPDATE 更新

After installing hapi and adding .js to webpack's resolve extentions and node as webpack's target I'm getting this erros with hapi modules: 在安装了hapi并将.js添加到webpack的解析范围和node作为webpack的目标之后,我得到了带有hapi模块的错误信息:

ERROR in ./~/hapi/lib/server.js
Module not found: Error: Can't resolve 'catbox' in '/Volumes/Dados/giovane/dev/studio-hikari/nodang/nodang-api/node_modules/hapi/lib'
 @ ./~/hapi/lib/server.js 5:15-32
 @ ./~/hapi/lib/index.js
 @ ./src/server.ts

ERROR in ./~/hapi/lib/server.js
Module not found: Error: Can't resolve 'catbox-memory' in '/Volumes/Dados/giovane/dev/studio-hikari/nodang/nodang-api/node_modules/hapi/lib'
 @ ./~/hapi/lib/server.js 6:21-45
 @ ./~/hapi/lib/index.js
 @ ./src/server.ts

You did not install hapi . 您没有安装hapi @types/hapi are just the type definitions that TypeScript uses for the library, but not the actual library itself. @types/hapi只是TypeScript用于库的类型定义,而不是实际的库本身。 So you need to add hapi as well: 因此,您还需要添加hapi

npm install --save hapi

Once you've installed it, the module can be found, although you'll get a new error that ./server could not be resolved in hapi/lib/index.js and that's because you configure resolve.extensions to only include .ts , but the library makes use of Node automatically resolving .js when leaving off the extension. 一旦你已经安装了它,该模块可以发现,尽管你会得到一个新的错误./server不能在解决hapi/lib/index.js那是因为你配置resolve.extensions只包括.ts ,但是该库在不使用扩展名的情况下会利用Node自动解析.js So you also need to include .js in the extensions: 因此,您还需要在扩展名中包含.js

extensions: ['.ts', '.js'],

After also resolving this issue, you'll be faced with another one, namely that Node built-in modules like fs can't be resolved. 在解决了这个问题之后,您将面临另一个问题,即无法解决诸如fs类的Node内置模块。 By default webpack builds for the web, so the Node built-in modules are not available. 默认情况下,Webpack是为Web构建的,因此Node内置模块不可用。 But you can change that by setting the target option in your webpack config to node : 但是您可以通过将webpack配置中的target选项设置为node来更改它:

target: 'node'

Edit 编辑

You're having trouble with other node_modules because you only use the top level node_modules , instead you want to always fall back to the regular module resolution of node_modules , so the resolve.modules should look like this: 您遇到其他麻烦node_modules因为你只用了顶级node_modules ,而是要始终回落到正规模块分辨率node_modules ,所以resolve.modules应该是这样的:

modules: [
  path.resolve('src'),
  'node_modules'
]

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

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