qbolt qbolt - 9 months ago 52
Javascript Question

Cannot find module 'project directory' using node + webpack

I've provided my

webpack.config.js
and
package.json
below. I've tried reinstalling the
node_modules
, but I don't think that's the issue seeing as it's the entire project it's not finding. I'm fairly new to webpack and I think it's a problem with the webpack configuration.

Any help would be appreciated.

I'm getting this error when I try to build and start the application.

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


This is the
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'
}]
}
}


This is the
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"
}
}


This is the
app.js


console.log('test')


Edit:

Here is the file structure of the project.

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

Answer Source

In your start script you run node ., which means to run the module of the current directory. So node sees that there is a package.json and looks for the main property and tries to run it. What you really do is run node ./dist/app.js, but that file doesn't exist.

You need to build it first, so webpack actually creates that file:

npm run build

You may have run npm build, which is an npm built-in command and does not run your script.