question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot read property 'replace' of undefined when running Bitbucket Pipelines

See original GitHub issue

Bug report

Describe the bug

Build fails when creating an artifact in Bitbucket using the node image: node:10.16.3.

The pipeline yaml used:

pipelines:
    branches:
        master:
            - step:
                # Prepare a dist artifact on every commit to master
                name: Prepare a deployment artifact
                deployment: test
                image: node:10.16.3
                caches:
                    - node
                artifacts:
                    - dist/**
                script:
                    - mkdir -p dist/
                    - yarn install --frozen-lockfile --production
                    # Disable warnings as errors with CI=false
                    - CI=false yarn build
                    - mv node_modules dist/
                    - mv .next dist/
                    - mv next.config.js dist/
                    - mv eco.config.js dist/
                    - mv package.json dist/
                    - mv server.js dist/
                    # NGINX configurations
                    - mv nginx dist/

Running a local yarn build succeeds but not when on pipelines. The error points me to assetPrefix:nextConfig.assetPrefix.replace(/\/$/,'') which I found on line 13*

*The error I got:

TypeError: Cannot read property 'replace' of undefined
    at _default (/opt/atlassian/pipelines/agent/build/node_modules/next/dist/export/index.js:13:82)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

My next.config.js file:

const sass = require('@zeit/next-sass')
const css = require('@zeit/next-css')
const withPlugins = require("next-compose-plugins")
const path = require('path')
require('next-images')
require('dotenv').config()

const nextConfig = {
  assetPrefix: process.env.NODE_ENV === 'production' ? process.env.HOST : "/",
  publicRuntimeConfig: { 
    staticFolder: process.env.NODE_ENV === 'production' ? process.env.HOST : "/"
  },

  /**
   * Environment for client which will be available at built time.
   */
  env: {
    API_HOST: process.env.API_HOST
  },

  webpack(config) {
    config.resolve.alias['components'] = path.join(__dirname, 'components')
    config.resolve.alias['layouts'] = path.join(__dirname, 'layouts')
    config.resolve.alias['assets'] = path.join(__dirname, 'assets')
    
    config.module.rules.push({
      test: /\.svg$/,
      use: ['@svgr/webpack'],
    });

    return config;
  }
}

//https://github.com/zeit/next-plugins/issues/266#issuecomment-474721942
module.exports = withPlugins([
  [sass],
  [css, { cssModules: false, url: false }]
], nextConfig);

Next.js version: 9.1.5

Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
SylarRubycommented, Dec 16, 2019

You are correct. How stupid of me. There is not a .env file during the pipeline build lol So doing this, in Bitbucket, solves it:

[...]
 - echo -e "HOST=${QA_HOST}\n" >> .env
[...]
2reactions
timneutkenscommented, Dec 16, 2019

Opened a PR to show a better error message here: https://github.com/zeit/next.js/pull/9759

The issue is that you’re providing undefined as the environment variable is probably not set.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot read property 'replace' of undefined when running ...
Build fails when creating an artifact in Bitbucket using the node image: node:10.16.3. The pipeline yaml used: pipelines: branches: master: - ...
Read more >
Has "Cannot read property of undefined" error message been ...
Today I was running my jest unit tests on Bitbucket pipeline and got failed due to different error message (see payload on screenshot...
Read more >
Undefined Properties in Bitbucket Pipeline
We'd like to do YAML references/aliases in the file and add a new property for it inside it (while we could also just...
Read more >
ERROR running auth:sfdxurl:store: Cannot read properties of ...
To fix the problem, you need to update your Auth URL to contain the new domain, that is, replace content of file ./SFDX_TMP_URL.txt...
Read more >
Cannot read property 'replace' of undefined - gitlab-ci-releaser
Creating a release without npm registry publishing Release failed TypeError: Cannot read property 'replace' of undefined at push ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found