Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.2 beta]: Cannot read properties of undefined (reading 'push') #12960

Closed
Kinbaum opened this issue Mar 25, 2022 · 17 comments
Closed

[4.2 beta]: Cannot read properties of undefined (reading 'push') #12960

Kinbaum opened this issue Mar 25, 2022 · 17 comments
Assignees
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members

Comments

@Kinbaum
Copy link

Kinbaum commented Mar 25, 2022

Describe the Bug

When using the 4.2.0-beta.0 with typescript, when I run a production build and then try to start my application, I get the following error:

[2022-03-25 08:47:55.681] debug: ⛔️ Server wasn't able to start properly.
[2022-03-25 08:47:55.682] error: Cannot read properties of undefined (reading 'push')
TypeError: Cannot read properties of undefined (reading 'push')
    at Object.addCreateLocalizationAction (/node_modules/@strapi/plugin-i18n/server/services/core-api.js:184:48)
    at /node_modules/@strapi/plugin-i18n/server/register.js:78:22
    at Array.forEach (<anonymous>)
    at extendLocalizedContentTypes (/node_modules/@strapi/plugin-i18n/server/register.js:56:38)
    at Object.module.exports [as register] (/node_modules/@strapi/plugin-i18n/server/register.js:12:3)
    at Object.register (/node_modules/@strapi/strapi/lib/core/domain/module/index.js:47:46)
    at Object.register (/node_modules/@strapi/strapi/lib/core/registries/modules.js:33:19)
    at async Strapi.runLifecyclesFunctions (/node_modules/@strapi/strapi/lib/Strapi.js:499:5)
    at async Strapi.register (/node_modules/@strapi/strapi/lib/Strapi.js:370:5)
    at async Strapi.load (/node_modules/@strapi/strapi/lib/Strapi.js:445:5)
    at async Strapi.start (/node_modules/@strapi/strapi/lib/Strapi.js:194:9)
Error: command finished with error: command (apps/strapi) yarn run start exited (1)

This is the code the error is thrown on:

const addCreateLocalizationAction = contentType => {
  const { modelName, apiName } = contentType;

  const localizationRoute = createLocalizationRoute(contentType);

  // !! Error is thrown here
  strapi.api[apiName].routes[modelName].routes.push(localizationRoute);

  strapi.container.get('controllers').extend(`api::${apiName}.${modelName}`, controller => {
    return Object.assign(controller, {
      createLocalization: createLocalizationHandler(contentType),
    });
  });
};

Here are the files of my collection.
There has been no customization to these

import { factories } from '@strapi/strapi';

export default factories.createCoreController('api::my-collection.my-collection');
import { factories } from '@strapi/strapi';

export default factories.createCoreRouter('api::my-collection.my-collection');
import { factories } from '@strapi/strapi';

export default factories.createCoreService('api::my-collection.my-collection');

Required System information

  • Node.js version: 16.14.0
  • NPM version: 8.3.1
  • Strapi version: 4.2.0-beta.0
  • Database: MySQL
  • Operating system: macOS

Expected behavior

Strapi to start correctly

@Kinbaum Kinbaum changed the title [4.2 beta]: [4.2 beta]: Cannot read properties of undefined (reading 'push') Mar 25, 2022
@maccomaccomaccomacco maccomaccomaccomacco added this to To be reviewed (Open) in Developer Experience - Old via automation Mar 28, 2022
@Convly Convly self-assigned this Mar 30, 2022
@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members labels Apr 11, 2022
@mhagmajer
Copy link

We've also run into this problem. When can we expect a stable release of Strapi 4.2?

@blankart
Copy link

blankart commented Jun 7, 2022

I am also getting an error related to strapi.api[apiName].routes[modelName].routes being undefined.

I followed this to setup unit testing. I encountered this error:

TypeError: Cannot read property 'forEach' of undefined

       7 |   if (!instance) {
       8 |     /** the following code in copied from `./node_modules/strapi/lib/Strapi.js` */
    >  9 |     await Strapi().load()
         |     ^
      10 |     instance = strapi // strapi is global now
      11 |     await instance.app
      12 |

      at node_modules/@strapi/strapi/lib/services/server/register-routes.js:98:21
      at node_modules/lodash/lodash.js:4967:15
      at baseForOwn (node_modules/lodash/lodash.js:3032:24)
      at node_modules/lodash/lodash.js:4936:18
      at Function.forEach (node_modules/lodash/lodash.js:9410:14)
      at registerAPIRoutes (node_modules/@strapi/strapi/lib/services/server/register-routes.js:94:7)
      at Object.<anonymous>.module.exports (node_modules/@strapi/strapi/lib/services/server/register-routes.js:25:3)
      at Object.initRouting (node_modules/@strapi/strapi/lib/services/server/index.js:93:13)
      at Strapi.bootstrap (node_modules/@strapi/strapi/lib/Strapi.js:435:23)
      at Strapi.load (node_modules/@strapi/strapi/lib/Strapi.js:446:5)
      at setupStrapi (tests/helpers/strapi.js:9:5)
      at tests/app.test.js:9:3

I'm using strapi version 4.2.0-beta.0

@florianmrz
Copy link
Contributor

@blankart Are you using TypeScript by any chance?

I've run into this exact same issue and it's related to how Strapi loads the APIs, routes etc.
Any newer version of Strapi (e.g. v4.0.3-beta.1) includes the following snippet in lib/Strapi.js to resolve the app and dir directories. What they are for is explained here:

/**
* Resolve the working directories based on the instance options.
*
* Behavior:
* - `appDir` is the directory where Strapi will write every file (schemas, generated APIs, controllers or services)
* - `distDir` is the directory where Strapi will read configurations, schemas and any compiled code
*
* Default values:
* - If `appDir` is `undefined`, it'll be set to `process.cwd()`
* - If `distDir` is `undefined`, it'll be set to `appDir`
*/
const resolveWorkingDirectories = opts => {

I've had this issue when Strapi was not able to load routes / controllers / middleware etc. due to searching in the wrong folder (the TS src/ folder instead of compiled JS files in dist/src/).

The following snippets loads the respective files. It can only handle .js and .json files. If the appDir points towards your TypeScript .ts files, it will return an empty object as per defined in the default case, resulting in the error @Kinbaum described.

const loadFile = file => {
const ext = extname(file);
switch (ext) {
case '.js':
return importDefault(file);
case '.json':
return fse.readJSON(file);
default:
return {};
}
};

@Bassel17
Copy link
Member

This issue doesn't seem to be present in the current latest release of Strapi 4.3.0
will consider it resolved

@sjoukedv
Copy link
Contributor

sjoukedv commented Aug 1, 2022

@Bassel17 I'd like to re-open with"@strapi/plugin-i18n": "4.3.0" (and core 4.3.0):

yarn run v1.22.19
$ strapi start
[2022-08-01 14:21:33.623] debug: ⛔️ Server wasn't able to start properly.
[2022-08-01 14:21:33.626] error: Cannot read properties of undefined (reading 'push')
TypeError: Cannot read properties of undefined (reading 'push')
    at Object.addCreateLocalizationAction (/app/node_modules/@strapi/plugin-i18n/server/services/core-api.js:182:48)
    at /app/node_modules/@strapi/plugin-i18n/server/register.js:78:22
    at Array.forEach (<anonymous>)
    at extendLocalizedContentTypes (/app/node_modules/@strapi/plugin-i18n/server/register.js:56:38)
    at Object.module.exports [as register] (/app/node_modules/@strapi/plugin-i18n/server/register.js:12:3)
    at Object.register (/app/node_modules/@strapi/strapi/lib/core/domain/module/index.js:47:46)
    at Object.register (/app/node_modules/@strapi/strapi/lib/core/registries/modules.js:33:19)
    at async Strapi.runLifecyclesFunctions (/app/node_modules/@strapi/strapi/lib/Strapi.js:510:5)
    at async Strapi.register (/app/node_modules/@strapi/strapi/lib/Strapi.js:379:5)
    at async Strapi.load (/app/node_modules/@strapi/strapi/lib/Strapi.js:456:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

FYI: I migrated this existing project to Typescript

@biko8
Copy link

biko8 commented Aug 2, 2022

Im not sure why but to me this error appears when adding "sourceMap": true, into compilerOptions of tsconfig.json.
I removed it and it works to me.

@roelbeerens
Copy link
Contributor

roelbeerens commented Aug 6, 2022

Seems to be still happening on 4.3.2 as well. Local build is fine, Docker build however, seems to be breaking.

Scratch that, my build removed the tsconfig.json file (as part of a 'clean build'), which seems is needed to determine if it's a TS project or not. Wouldn't it just be easier to check for a dist folder? Or maybe start the project with a environment variable?

@Bassel17
Copy link
Member

Bassel17 commented Aug 9, 2022

@roelbeerens the dist folder is something that is generated while you develop a typescript project it doesn't really exist beforehand and the naming can also be changed by the user in the tsconfig which is why we can't really use it to determine the project.
As for why we are not using an env to determine if it's TS or not, it's simply because the tsconfig is necessary to run a typescript project. However I am interested to see in what usecase you wouldn't want the tsconfig to be present ?

@Bassel17
Copy link
Member

Bassel17 commented Aug 9, 2022

@sjoukedv I would want to know more information about this, I wasn't able to reproduce this.
it might be related to an issue we faced with windows (if you are using windows), I invite you to try again on 4.3.2 and let me know how to goes.

@antixrist
Copy link

@Bassel17 this is the same issue as #14088

@sjoukedv
Copy link
Contributor

@sjoukedv I would want to know more information about this, I wasn't able to reproduce this. it might be related to an issue we faced with windows (if you are using windows), I invite you to try again on 4.3.2 and let me know how to goes.

I believe I ended up adding a debug console.log() to the i18n plugin source to find out that I had a route (or controller) file with only commented out code in it. I'd suggest a try catch statement for that loop with a more clear error message.

FYI: I am on Ubuntu 20.04

@FieldMarshallVague
Copy link

Antixrist is correct, it's the same issue (well, solution) as the above linked issue. It can be fixed in my case (on windows, strapi 4.4.0) by changing these lines in the strapi package:

In @strapi/typescript-utils/lib/utils/get-config-path.js. Comment out the return line on line 33 and replace with the following lines (line 33 included for reference).

// return configFilePath.startsWith(dirAbsolutePath) ? configFilePath : undefined;
  const configFileAbsolutePath = path.resolve(configFilePath)
  return configFileAbsolutePath.startsWith(dirAbsolutePath)
    ? configFileAbsolutePath
    : undefined

@sobirjonovdev
Copy link

I cannot find solution to this error
[2022-11-06 07:34:20.782] debug: ⛔️ Server wasn't able to start properly.
[2022-11-06 07:34:20.784] error: Cannot read property 'api' of undefined
TypeError: Cannot read property 'api' of undefined
at Object.getApiDocumentationPath (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/plugin-documentation/server/services/documentation.js:89:40)
at Object.generateFullDoc (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/plugin-documentation/server/services/documentation.js:141:43)
at Object.module.exports [as bootstrap] (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/plugin-documentation/server/bootstrap.js:53:65)
at runMicrotasks ()
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Object.bootstrap (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/strapi/lib/core/domain/module/index.js:40:7)
at async Object.bootstrap (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/strapi/lib/core/registries/modules.js:28:9)
at async Strapi.runLifecyclesFunctions (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/strapi/lib/Strapi.js:468:5)
at async Strapi.bootstrap (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/strapi/lib/Strapi.js:406:5)
at async Strapi.load (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/strapi/lib/Strapi.js:415:5)
at async Strapi.start (/home/user/Desktop/strapi/akbar-rich-strapi/node_modules/@strapi/strapi/lib/Strapi.js:163:9)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! strapi@0.1.0 develop: strapi develop
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the strapi@0.1.0 develop script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /home/user/.npm/_logs/2022-11-06T02_34_20_817Z-debug.log

@roelbeerens
Copy link
Contributor

@roelbeerens the dist folder is something that is generated while you develop a typescript project it doesn't really exist beforehand and the naming can also be changed by the user in the tsconfig which is why we can't really use it to determine the project. As for why we are not using an env to determine if it's TS or not, it's simply because the tsconfig is necessary to run a typescript project. However I am interested to see in what usecase you wouldn't want the tsconfig to be present ?

My bad for not replying earlier, seems like my Github notification settings are all messed up. Anyways, you are right about the dist folder not being present and wouldn't be a correct way to check if it's a TS project or not. Indeed, a tsconfig.json should indicate it's a TS project, as it's a requirement to run a TS project. In our case, we fired the build command in our Dockerfile (which works just fine) and after that, we always tend to cleanup all dev related stuff for production images (stuff we don't need). As we already built the TS project, and had the dist, we kind of assumed that the tsconfig.json wouldn't be necessary anymore as we had already built the project.

It's totally not a big deal to keep the tsconfig.json file, but maybe you guys could discuss to check for multiple paths when starting Strapi (on start check if there's a dist > run from there. If no dist, run from src)

@eubash
Copy link

eubash commented Jan 18, 2023

Resolve this

@Bassel17 I'd like to re-open with"@strapi/plugin-i18n": "4.3.0" (and core 4.3.0):

yarn run v1.22.19
$ strapi start
[2022-08-01 14:21:33.623] debug: ⛔️ Server wasn't able to start properly.
[2022-08-01 14:21:33.626] error: Cannot read properties of undefined (reading 'push')
TypeError: Cannot read properties of undefined (reading 'push')
    at Object.addCreateLocalizationAction (/app/node_modules/@strapi/plugin-i18n/server/services/core-api.js:182:48)
    at /app/node_modules/@strapi/plugin-i18n/server/register.js:78:22
    at Array.forEach (<anonymous>)
    at extendLocalizedContentTypes (/app/node_modules/@strapi/plugin-i18n/server/register.js:56:38)
    at Object.module.exports [as register] (/app/node_modules/@strapi/plugin-i18n/server/register.js:12:3)
    at Object.register (/app/node_modules/@strapi/strapi/lib/core/domain/module/index.js:47:46)
    at Object.register (/app/node_modules/@strapi/strapi/lib/core/registries/modules.js:33:19)
    at async Strapi.runLifecyclesFunctions (/app/node_modules/@strapi/strapi/lib/Strapi.js:510:5)
    at async Strapi.register (/app/node_modules/@strapi/strapi/lib/Strapi.js:379:5)
    at async Strapi.load (/app/node_modules/@strapi/strapi/lib/Strapi.js:456:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

FYI: I migrated this existing project to Typescript

For me issue resolved by passing distDir in options

await Strapi({ distDir: './dist' }).load();

@LukaHedt
Copy link

Can we add this fix in to the testing documentation?

For me issue resolved by passing distDir in options

await Strapi({ distDir: './dist' }).load();

That and the fact that non-default environment config files need to be .js and not .ts.

@shivarajnaidu
Copy link

shivarajnaidu commented Apr 2, 2024

When i tried this method in starpi 4.20.0 (typescript)

I got following error

[2024-04-03 02:18:48.121] error: Cannot read properties of undefined (reading 'push')
TypeError: Cannot read properties of undefined (reading 'push')
    at Object.addCreateLocalizationAction (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/plugin-i18n/dist/server/index.js:3362:48)
    at /Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/plugin-i18n/dist/server/index.js:2787:22
    at Array.forEach (<anonymous>)
    at extendLocalizedContentTypes (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/plugin-i18n/dist/server/index.js:2768:39)
    at Object.register (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/plugin-i18n/dist/server/index.js:2743:3)
    at Object.register (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/strapi/dist/core/domain/module/index.js:46:46)
    at Object.register (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/strapi/dist/core/registries/modules.js:28:19)
    at async Strapi.runLifecyclesFunctions (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/strapi/dist/Strapi.js:440:5)
    at async Strapi.register (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/strapi/dist/Strapi.js:361:5)
    at async Strapi.load (/Users/myuser/oth/myapp/myapp-web-cms/node_modules/@strapi/strapi/dist/Strapi.js:425:5

For me this works !

const strapi = require('@strapi/strapi');
strapi({ distDir: './dist' }).start();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members
Projects
Archived in project
Development

No branches or pull requests