使用node + webpack找不到模块'project directory'

时间:2017-03-11 18:48:16

标签: javascript node.js webpack

我在下方提供了webpack.config.jspackage.json。我已经尝试重新安装node_modules,但我不认为这是问题,因为它是整个项目,它没有找到。我对webpack很新,​​我认为这是webpack配置的一个问题。

任何帮助都将不胜感激。

当我尝试构建和启动应用程序时,我收到此错误。

Error: Cannot find module 'C:\code\project-name'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.runMain (module.js:604:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:149:9)
    at bootstrap_node.js:509:3

这是webpack.config.js

module.exports = {
  target: 'node',

  entry: {
    app: ['babel-polyfill', './src/app.js']
  },

  output: {
    path: './dist/',
    filename: '[name].js'
  },

  module: {
    loaders: [{
      test: /.js$/,
      exclude: /node_modules/,
      loader: 'babel'
    }]
  }
}

这是package.json

{
  "name": "project-name",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/app.js",
  "scripts": {
    "build:watch": "webpack --colors --progress --watch",
    "build": "webpack --colors --progress",
    "start": "node ."
  },
  "author": "",
  "license": "ISC",
  "standard": {
    "parser": "babel-eslint"
  },
  "babel": {
    "presets": [
      "latest",
      "stage-0"
    ]
  },
  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-eslint": "^7.0.0",
    "babel-loader": "^6.2.5",
    "babel-polyfill": "^6.16.0",
    "babel-preset-latest": "^6.16.0",
    "babel-preset-stage-0": "^6.16.0",
    "standard": "^8.4.0",
    "webpack": "^1.13.2"
  },
  "dependencies": {
    "lodash": "^4.16.4"
  }
}

这是app.js

console.log('test')

编辑:

这是项目的文件结构。

project-name/
    src/
        app.js
    webpack.config.js
    package.json
    node_modules/

1 个答案:

答案 0 :(得分:2)

start脚本中运行node .,这意味着运行当前目录的模块。因此节点看到有package.json并查找main属性并尝试运行它。你真正做的是运行node ./dist/app.js,但该文件不存在。

您需要先构建它,因此webpack实际上会创建该文件:

npm run build

您可能已经运行了npm build,这是一个npm内置命令,并且不会运行您的脚本。