vuejs-templates / pwa

PWA template for vue-cli based on the webpack template
MIT License
3.04k stars 508 forks source link

Error: Cannot find module 'chalk' #152

Open nwaughachukwuma opened 6 years ago

nwaughachukwuma commented 6 years ago

Hey guys (@jeffposnick and all) am getting this error on my Heroku App after CI/CD from GitLab to Heroku. Following is the gitlab-ci.yml script:

image: node:6

stages:
  - ver
  - init
  - tests
  - deploy

cache:
  paths:
    - node_modules/

ver:
  stage: ver
  script:
    - node --version
    - whoami
init:
  stage: init
  script:
    - npm install -g chalk
    - npm cache clean
    - rm -rf node-modules
    - npm install

run_tests:
 # needed for testing selenium on docker with java
  image: karthikiyengar/node-nightwatch:0.0.1
  stage: tests
  script:
    - sudo npm install
    - sudo npm test

deploy_staging:
  stage: deploy
  script:
    - npm install -g chalk
    - npm cache clean
    - rm -rf node-modules
    - npm install    
    - git remote add heroku https://heroku:$HEROKU_API_KEY@git.heroku.com/webapp-staging.git
    - git push heroku master
    - echo "Deployed to staging server"
  environment: staging
  only:
    - master

deploy_production:
  stage: deploy
  script:
    - npm install -g chalk
    - npm cache clean
    - rm -rf node-modules
    - npm install
    - git remote add heroku https://heroku:$HEROKU_API_KEY@git.heroku.com/webapp.git
    - git push heroku master
    - echo "Deployed to production server"
  environment: production
  when: manual
  only:
    - master

Help will be greatly appreciated. Thanks

jeffposnick commented 6 years ago
nwaughachukwuma commented 6 years ago

Hi @jeffposnick thanks for responding to this.

  1. I encountered the issue only on Heroku and not locally
  2. From my findings I discovered that Heroku doesn't install devDependencies. But even at that, after moving all the dependencies into production, I couldn't fire the app.
  3. It builds and deploys successfully on GitLab, but I get the error when I try to view the App on Heroku.
  4. The several npm install -g chalk are as a result of my attempt to fix the problem.
MichaelJCole commented 6 years ago

This happened to me about 10 mins ago while installing the PWA template.

I had to turn off local-npm (an npm caching program). Sometimes local-npm can't find packages. It could be a package caching bug with Heroku.

nwaughachukwuma commented 6 years ago

@MichaelJCole please how did you achieve this? I mean turning off the local-npm

MichaelJCole commented 6 years ago

Hey @nwaughachukwuma local-npm is a project I use when travelling so nvm is cached locally and I'm not using mobile data. https://www.npmjs.com/package/local-npm

If you haven't installed it, then that's probably not the problem. It's possible there is a proxy server in the way that is caching/proxying incorrectly. The simplest thing that could possibly work is to delete node_modules or use a different version of node/nvm.

lifecoderua commented 6 years ago

Also happens on a direct PWA template deployment to Heroku, no local-npm installed. I believe it is caused because Heroku runs app as PRODUCTION env (and specifically prunes dev modules), and Chalk is listed under devDependencies.

lifecoderua commented 6 years ago

The reason is build/build.js uses chalk. It may be moved into main dependencies, or made optional in build.js depending on NODE_ENV (just make sure you do this before forced process.env.NODE_ENV = 'production')

leodutra commented 6 years ago

I confirm @lifecoderua theory. It's happening on pwa and webpack... and maybe any other vue template

Cre3z commented 6 years ago

@nwaughachukwuma any solution to this yet? I just deployed to heroku and receiving the same error.

  1. dev/build works on my local env but as soon as I deploy to heroku the logs throw an error. 2018-07-16T10:11:47.171884+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" request_id=48b2c643-3c7c-4802-a066-bad93d00b546 fwd="105.186.163.232" dyno= connect= service= status=503 bytes= protocol=https

  2. I did run an npm install to make sure all depencies are installed.

  3. npm run build results in

    throw err;
    ^

Error: Cannot find module 'chalk'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/app/build/check-versions.js:2:15)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
  1. I did run npm install chalk -g to no effect.
Hemant-Synerzip commented 6 years ago

I have also faced the same problem. I just deleted my node_modules folder and did npm install. It worked for me. Hope it will work for you also.

leodutra commented 6 years ago

In thesis, build should not be run using production environment. This is bad practice on CI/CD and I think it is the way the CLI generated script expects us to work. We run build in dev and use the dist on production.

I know is not the best thing in the world, but that was how I solved that before.

The other option is to change webpack files (I think build.js) to receive production env for build mode. I were able to do that with 2 lines. Currently, there's no build parametrization for production (or at least it did not have that when I tweaked the webpack files).

I'm just returning here to expose these 2 solutions I used on my issue some months ago.

andrewrady commented 6 years ago

I was having this issue with hosting a Vue SPA on Heroku. I was able to fix this issue with adding "postinstall": "npm run build" in the scripts object in the package.json , and I also had NODE_ENV=production. Not sure if it helps with the PWA issue as I have not tested it personally. All I know is it was able to run the build command correctly after Heroku cleared out all the node_modules and reinstalled them. Hope this helps!

mariolamacchia commented 5 years ago

I think the problem is npm install skipping devDependencies when NODE_ENV=production. I fixed it with npm --production=false install

SaikalSdd commented 5 years ago

ATTENTION!!!

still doesnt work. still returning "Cannot find module 'chalk'". I tried: -npm install -g chalk -npm --production=false install -npm cache clean -tryed to delete node_modules, returning OPERATION NOT PERMITTED,ISTAT '\node_modules\.staging\acorn-...\CHANGELOG.md' Still without success(((( HELP!!!

juanbrujo commented 4 years ago

removing NODE_ENV = production in Netlify worked for me, finally

madsonsilva commented 2 years ago

I think the problem is npm install skipping devDependencies when NODE_ENV=production. I fixed it with npm --production=false install

works for me ! thanks!

jan-swiecki commented 2 years ago

I had the same issue with netlify.

For the lazy:

if [ "$NODE_ENV" == "production" ]; then
  echo "installing node_modules for production in dev mode"
  NODE_ENV=development npm install
  # or: NODE_ENV=development yarn install
fi

I'm not sure this is a good idea, but otherwise how should we build the project for production without devDependencies?