Модуль не найден: Ошибка: не удается разрешить модуль 'server'

Я пытаюсь использовать webpack для управления файлами в моем проекте. Я использую webpack-dev-server и bower-webpack-plugin. Когда я запускаю сервер, я получаю ошибку в консоли браузера.

Модуль не найден: Ошибка: невозможно разрешить сервер модуля

webpack.config.js

const BowerWebpackPlugin = require("bower-webpack-plugin");

module.exports = {
    entry: './src/script/index.jsx',
    output: {
        filename: 'bundle.js', //this is the default name, so you can skip it
        //at this directory our bundle file will be available
        //make sure port 8090 is used when launching webpack-dev-server
        publicPath: 'http://localhost:8090/assets'
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {   
                test: /\.js[x]?$/, 
                loaders: ['react-hot', 'jsx', 'babel'],
                exclude: /node_modules/ 
              },
              {
                test: /\.scss$/,
                loaders: [ 'style', 'css?sourceMap', 'sass?sourceMap' ]
              }
        ]
    },
    plugins: [
        new BowerWebpackPlugin()
    ],
    externals: {
        //don't bundle the 'react' npm package with our bundle.js
        //but get it from a global 'React' variable
        'react': 'React'
    },
    resolve: {
        extensions: ['', '.js', '.jsx'],
        modulesDirectories: ["./src", "node_modules", "bower_components"]
    }
}

Изменить: теперь я использую эту конфигурацию webpack без bower-webpack-plugin

package.json

    {
  "scripts": {
    "start": "npm run serve | npm run dev",
    "serve": "./node_modules/.bin/http-server -p 8080",
    "dev": "webpack-dev-server --progress --colors --port 8090"
  },
  "name": "Wonder",
  "version": "0.0.0",
  "description": "Internal app",
  "main": "./src/script/index.jsx",
  "author": "and",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^5.8.22",
    "babel-loader": "^5.3.2",
    "bower-webpack-plugin": "^0.1.8",
    "css-loader": "^0.16.0",
    "extract-text-webpack-plugin": "^0.8.2",
    "http-server": "^0.8.0",
    "jsx-loader": "^0.13.2",
    "node-sass": "^3.2.0",
    "path": "^0.11.14",
    "react": "^0.13.3",
    "react-hot-loader": "^2.0.0-alpha-2",
    "reinstall": "^1.1.0",
    "sass-loader": "^2.0.1",
    "style-loader": "^0.12.3",
    "webpack": "^1.11.0",
    "webpack-dev-server": "^1.10.1"
  }
}

вывод консоли браузера

    Uncaught Error: Cannot find module "server"
webpackMissingModule @ bootstrap 19214e8f07aa8a69f314:39
(anonymous function) @ bootstrap 19214e8f07aa8a69f314:39
__webpack_require__ @ bootstrap 19214e8f07aa8a69f314:19
(anonymous function) @ bootstrap 19214e8f07aa8a69f314:39
(anonymous function) @ bootstrap 19214e8f07aa8a69f314:39
webpack-dev-server.js:1 [WDS] Errors while compiling.

    webpack-dev-server.js:1 multi main
    Module not found: Error: Cannot resolve module 'server' in /Users/and/devel/webpack_test
    resolve module server in /Users/and/devel/webpack_test
      looking for modules in /Users/and/devel/webpack_test/src
        /Users/and/devel/webpack_test/src/server doesn't exist (module as directory)
        resolve 'file' server in /Users/and/devel/webpack_test/src
          resolve file
            /Users/and/devel/webpack_test/src/server.js doesn't exist
            /Users/and/devel/webpack_test/src/server doesn't exist
            /Users/and/devel/webpack_test/src/server.jsx doesn't exist
      looking for modules in /Users/and/devel/webpack_test/node_modules
        /Users/and/devel/webpack_test/node_modules/server doesn't exist (module as directory)
        resolve 'file' server in /Users/and/devel/webpack_test/node_modules
          resolve file
            /Users/and/devel/webpack_test/node_modules/server doesn't exist
            /Users/and/devel/webpack_test/node_modules/server.js doesn't exist
            /Users/and/devel/webpack_test/node_modules/server.jsx doesn't exist
      looking for modules in /Users/and/devel/webpack_test/bower_components
        /Users/and/devel/webpack_test/bower_components/server doesn't exist (module as directory)
        resolve 'file' server in /Users/and/devel/webpack_test/bower_components
          resolve file
            /Users/and/devel/webpack_test/bower_components/server doesn't exist
            /Users/and/devel/webpack_test/bower_components/server.js doesn't exist
            /Users/and/devel/webpack_test/bower_components/server.jsx doesn't exist
        resolve 'bower component' server manifest files using [bower.json]
          resolve file
            /Users/and/devel/webpack_test/bower_components/server/bower.json doesn't exists
            /Users/and/devel/webpack_test/src/server/bower.json doesn't exists
            /Users/and/devel/webpack_test/node_modules/server/bower.json doesn't exists
            /Users/and/devel/bower_components/server/bower.json doesn't exists
            /Users/and/devel/src/server/bower.json doesn't exists
            /Users/and/devel/node_modules/server/bower.json doesn't exists
            /Users/and/bower_components/server/bower.json doesn't exists
            /Users/and/src/server/bower.json doesn't exists
            /Users/and/node_modules/server/bower.json doesn't exists
            /Users/bower_components/server/bower.json doesn't exists
            /Users/src/server/bower.json doesn't exists
            /Users/node_modules/server/bower.json doesn't exists
            /bower_components/server/bower.json doesn't exists
            /src/server/bower.json doesn't exists
            /node_modules/server/bower.json doesn't exists
    [/Users/and/devel/webpack_test/src/server.js]
    [/Users/and/devel/webpack_test/src/server]
    [/Users/and/devel/webpack_test/src/server]
    [/Users/and/devel/webpack_test/src/server.jsx]
    [/Users/and/devel/webpack_test/node_modules/server]
    [/Users/and/devel/webpack_test/node_modules/server]
    [/Users/and/devel/webpack_test/node_modules/server.js]
    [/Users/and/devel/webpack_test/node_modules/server.jsx]
    [/Users/and/devel/webpack_test/bower_components/server]
    [/Users/and/devel/webpack_test/bower_components/server]
    [/Users/and/devel/webpack_test/bower_components/server.js]
    [/Users/and/devel/webpack_test/bower_components/server.jsx]
     @ multi main

(anonymous function) @ webpack-dev-server.js:1
n.emit @ webpack-dev-server.js:1
r.onevent @ webpack-dev-server.js:1
r.onpacket @ webpack-dev-server.js:1
t.exports @ webpack-dev-server.js:1
n.emit @ webpack-dev-server.js:1
r.ondecoded @ webpack-dev-server.js:1
t.exports @ webpack-dev-server.js:1
n.emit @ webpack-dev-server.js:1
s.add @ webpack-dev-server.js:1
r.ondata @ webpack-dev-server.js:1
t.exports @ webpack-dev-server.js:1
n.emit @ webpack-dev-server.js:1
r.onPacket @ webpack-dev-server.js:2
(anonymous function) @ webpack-dev-server.js:1
n.emit @ webpack-dev-server.js:1
r.onPacket @ webpack-dev-server.js:1
r.onData.n @ webpack-dev-server.js:1
(anonymous function) @ webpack-dev-server.js:1
e.decodePayloadAsBinary @ webpack-dev-server.js:1
e.decodePayload @ webpack-dev-server.js:1
r.onData @ webpack-dev-server.js:1
(anonymous function) @ webpack-dev-server.js:2
n.emit @ webpack-dev-server.js:1
i.onData @ webpack-dev-server.js:2
i.onLoad @ webpack-dev-server.js:2
i.create.hasXDR.n.onreadystatechange @ webpack-dev-server.js:2

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Basic Property Grid</title>
    <!-- include react -->
    <script src="./node_modules/react/dist/react-with-addons.js"></script>

</head>
<body>
    <div id="content">
        <!-- this is where the root react component will get rendered -->
    </div>
    <!-- include the webpack-dev-server script so our scripts get reloaded when we make a change -->
    <!-- we'll run the webpack dev server on port 8090, so make sure it is correct -->
    <script src="http://localhost:8090/webpack-dev-server.js"></script>
    <!-- include the bundle that contains all our scripts, produced by webpack -->
    <!-- the bundle is served by the webpack-dev-server, so serve it also from localhost:8090 -->
    <script type="text/javascript" src="http://localhost:8090/assets/bundle.js"></script>

</body>
</html>

bower.json

{
  "name": "Wonder",
  "version": "0.0.0",
  "description": "Internal app",
  "main": "index.html",
  "authors": [
    "And"
  ],
  "license": "MIT",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components",
    "test",
    "tests"
  ],
  "devDependencies": {
    "jquery": "~2.1.4"
  }
}

Ответ 1

Где-то в ваших исходных файлах или сторонних библиотеках есть ссылка на ресурс с именем server (без какого-либо расширения). Он может быть в форме require('server'). Webpack изо всех сил пытается найти его. Он проверяет различные формы (например, server как каталог, server.js, server.jsx и т.д.) В разных местах, но в конце концов он терпит неудачу. Попытайтесь выяснить, где эта ссылка.

Ответ 2

Я получил схожую ошибку с помощью webpack-dev-сервера. Несмотря на то, что webpack был установлен глобально, мне пришлось установить его как часть проекта, чтобы решить проблему "webpack/hot/dev-server"

Ответ 3

Получена аналогичная ошибка, когда, ошибочно, используется npm start server вместо npm run server.

Если у вас server script в package.json, запуск npm run server запустит его. Если вы используете npm start server, он будет искать модуль server в вашем источнике и попытаться запустить его.

Небольшая опечатка, но может легко возникнуть из-за отсутствия внимания.

Ответ 4

В моей ситуации я использую TFS и webpack и получаю много ошибок module not found.

Оказалось, что в моем файле package.json у меня был несуществующий пакет, который я не удалил.

"primeng": "1.0.0-rc.4",
"primeng-ultima": "Unavailable"

Как только я удалил этот пакет, все сработало.

К счастью, журналы привели меня к такому выводу. Что случилось, npm пропустил восстановление других пакетов, когда он не смог загрузить этот пакет.