pwa icon indicating copy to clipboard operation
pwa copied to clipboard

Error: Cannot find module 'chalk'

Open nwaughachukwuma opened this issue 7 years ago • 17 comments

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:[email protected]/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:[email protected]/webapp.git
    - git push heroku master
    - echo "Deployed to production server"
  environment: production
  when: manual
  only:
    - master

Help will be greatly appreciated. Thanks

nwaughachukwuma avatar Jan 17 '18 03:01 nwaughachukwuma

  • Do you see this error locally, or just in the Heroku environment?
  • Can you provide some more context, such as the stack trace associated with that error message?
  • At what stage in the CI process does the error occur?
  • And I see several instances of npm install -g chalk in your config—did you deliberately add those in to attempt to fix the error, or are those needed for something else?

jeffposnick avatar Jan 17 '18 15:01 jeffposnick

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.

nwaughachukwuma avatar Jan 21 '18 02:01 nwaughachukwuma

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.

MichaelJCole avatar Feb 14 '18 01:02 MichaelJCole

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

nwaughachukwuma avatar Feb 21 '18 08:02 nwaughachukwuma

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.

MichaelJCole avatar Feb 22 '18 03:02 MichaelJCole

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 avatar Mar 05 '18 21:03 lifecoderua

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')

lifecoderua avatar Mar 05 '18 22:03 lifecoderua

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

leodutra avatar Apr 11 '18 13:04 leodutra

@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.

Cre3z avatar Jul 16 '18 10:07 Cre3z

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.

Hemant-Synerzip avatar Sep 22 '18 08:09 Hemant-Synerzip

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.

leodutra avatar Sep 26 '18 04:09 leodutra

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!

andrewrady avatar Sep 26 '18 05:09 andrewrady

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

mariolamacchia avatar Oct 16 '18 21:10 mariolamacchia

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!!!

SaikalSdd avatar Jul 22 '19 07:07 SaikalSdd

removing NODE_ENV = production in Netlify worked for me, finally

juanbrujo avatar Nov 26 '19 23:11 juanbrujo

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!

madsonsilva avatar Oct 14 '21 21:10 madsonsilva

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?

jan-swiecki avatar Dec 08 '21 08:12 jan-swiecki