Error: Cannot find module 'body-parser' on clean project with 'body-parser' included in package.json

I just started a clean new project and added ‘body-parser’ to package.json using the ‘Add Package’ button.

I immediately get an error in the logs: Error: Cannot find module 'body-parser'

What am I doing wrong?

I see there’s a new package-lock.json file in the sidebar - is that related?

# package.json

"dependencies": {
    "express": "^4.15.3",
    "body-parser": "^1.17.2"
  },
# server.js

var bodyParser = require('body-parser');
# logs

Error: Cannot find module 'body-parser'
3:24 PM
    at Function.Module._resolveFilename (module.js:485:15)
3:24 PM
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
Jump to
at Object.<anonymous> (/app/server.js:8:18)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

For a quick fix update the scripts section of your package.json to include:

"preinstall": "rm package-lock.json",

The change over to npm 5 for projects seems to be causing issues with module installation. The above will delete the package-lock file allowing install to take place as expected.

1 Like

hi weavermedia,

you did everything correctly, it was a problem on our end. When we upgraded to Node.js 8.1 we caused this issue. It should be fixed now! Thanks for reporting! :slight_smile:

2 Likes

Thanks @shindakun - good to know :+1:

@etamponi - great, thanks! :rocket: