Path alias import fails on production
See original GitHub issueHello! Recently I’ve started being in touch with a strange bug. It seems that the module-alias does not work on my production environment (does not matter if I run the same commands to build and start the app, it only works on development environment but not on production) 🤔
I’m currently using lerna to run all project commands because it is a project inside a monorepository. So, in practice I build the app using the following Dockerfile:
FROM node:current-stretch
RUN mkdir -p /uno
WORKDIR /uno
COPY . /uno
RUN npm run ci:unapy
ENV PORT 80
ENV NODE_ENV production
EXPOSE 80 3667
CMD [ "npm", "run", "dev:unapy" ]
So, after building this app, I try to run it with the following command:
# 'npm run dev:unapy' is the command below
node -r module-alias/register dist/index.js
Then, I receive the following logs depending on the environment I’m currently in:
- Production Environment log:
> uno-game@0.0.0 start:unapy
> lerna run start --scope @uno-game/unapy --stream
lerna notice cli v3.22.1
lerna notice filter including "@uno-game/unapy"
lerna info filter [ '@uno-game/unapy' ]
lerna info Executing command in 1 package: "npm run start"
> @uno-game/unapy@0.0.0 start
> node -r module-alias/register dist/index.js
node:internal/modules/cjs/loader:928
throw err;
^
Error: Cannot find module '@/Core'
Require stack:
- /uno/packages/unapy/dist/index.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._resolveFilename (/uno/node_modules/module-alias/index.js:49:29)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/uno/packages/unapy/dist/index.js:7:32)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/uno/packages/unapy/dist/index.js' ]
}
npm ERR! code 1
npm ERR! path /uno/packages/unapy
npm ERR! command failed
npm ERR! command sh -c node -r module-alias/register dist/index.js
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-01-19T17_05_14_611Z-debug.log
at Function.Module._resolveFilename (/uno/node_modules/module-alias/index.js:49:29)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Module.require (node:internal/modules/cjs/loader:997:19)
at require (node:internal/modules/cjs/helpers:92:18)
at Object.<anonymous> (/uno/packages/unapy/dist/index.js:7:32)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/uno/packages/unapy/dist/index.js' ]
}
npm ERR! code 1
npm ERR! path /uno/packages/unapy
npm ERR! command failed
npm ERR! command sh -c node -r module-alias/register dist/index.js
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2021-01-19T17_05_29_914Z-debug.log
lerna ERR! npm run start exited 1 in '@uno-game/unapy'
- Development Environment log:
> uno-game@0.0.0 start:unapy /ssd/Github/uno-game
> lerna run start --scope @uno-game/unapy --stream
lerna notice cli v3.22.1
lerna notice filter including "@uno-game/unapy"
lerna info filter [ '@uno-game/unapy' ]
lerna info Executing command in 1 package: "npm run start"
@uno-game/unapy: > @uno-game/unapy@0.0.0 start /ssd/Github/uno-game/packages/unapy
@uno-game/unapy: > node -r module-alias/register dist/index.js
@uno-game/unapy: Server is running... [PORT 5000]
You can see some project configs below:
- Lerna package.json:
{
"name": "uno-game",
"version": "0.0.0",
"private": true,
"author": "Guilherme Mota <guilhermebromonschenkel@gmail.com>",
"license": "MIT",
"scripts": {
"setup": "npm run setup:app && npm run setup:env",
"setup:app": "npm install && npm run install:unapy && npm run install:unoenty && npm run bootstrap",
"setup:env": "npm run env:unapy && npm run env:unoenty",
"bootstrap": "lerna bootstrap --use-workspaces --hoist && lerna link",
"list": "lerna ls",
"deploy": "lerna version",
"install:unapy": "lerna exec --stream --scope @uno-game/unapy -- npm install",
"build:unapy": "lerna run build --scope @uno-game/unapy --stream",
"start:unapy": "lerna run start --scope @uno-game/unapy --stream",
"dev:unapy": "lerna run dev --scope @uno-game/unapy --stream",
"env:unapy": "cp packages/unapy/.env.example packages/unapy/.env",
"install:unoenty": "lerna exec --stream --scope @uno-game/unoenty -- npm install",
"build:unoenty": "lerna run build --scope @uno-game/unoenty --stream",
"env:unoenty": "cp packages/unoenty/.env.example packages/unoenty/.env",
"ci:unapy": "npm install && npm run install:unapy && npm run bootstrap && npm run build:unapy",
"ci:unoenty": "npm install && npm run install:unoenty && npm run bootstrap && npm run build:unoenty"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"cross-env": "^7.0.2",
"eslint": "^6.6.0",
"eslint-config-standard": "^14.1.1",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-flowtype": "^5.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-react": "^7.20.3",
"eslint-plugin-react-hooks": "^4.0.8",
"eslint-plugin-standard": "^4.0.1",
"git-commit-msg-linter": "^2.4.3",
"husky": "^4.2.5",
"lerna": "^3.20.2",
"lint-staged": "^10.2.10",
"typescript": "^3.9.2"
},
"workspaces": [
"packages/*",
"packages/shared/*"
]
}
- App package.json:
{
"name": "@uno-game/unapy",
"version": "0.0.0",
"description": "The UNO Game API",
"main": "dist/index.js",
"scripts": {
"start": "node -r module-alias/register dist/index.js",
"dev": "nodemon src/index.ts",
"dev:resources": "docker-compose up",
"build": "rm -rf dist && tsc && cp -r src/Assets dist/Assets",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@types/cors": "^2.8.6",
"@types/express": "^4.17.6",
"@types/node": "^14.0.1",
"@types/socket.io": "^2.1.8",
"@types/uuid": "^8.0.0",
"nodemon": "^2.0.4",
"sucrase": "^3.15.0",
"tsconfig-paths": "^3.9.0",
"@uno-game/protocols": "*"
},
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"module-alias": "^2.2.2",
"socket.io": "^2.3.0",
"socket.io-msgpack-parser": "^2.2.0",
"@uno-game/error-handler": "*"
},
"optionalDependencies": {
"@uno-game/protocols": "*",
"@uno-game/error-handler": "*"
},
"_moduleAliases": {
"@": "dist"
}
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:7
Top Results From Across the Web
Typescript - Import class with path alias failed - Stack Overflow
– Aleksey L. yes it is. i've just found the problem, need to add tsconfig-paths into run script so that custom paths get...
Read more >How to handle tsconfig path aliases : r/typescript - Reddit
To be clear: The autoimport in vscode works fine, it's just the browser that doesn't know what to do with the aliases, so...
Read more >Advanced Features: Absolute Imports and Module Path Aliases
Configure module path aliases that allow you to remap certain import paths.
Read more >Autoimports don't use webpack alias if the alias is resolved to ...
You want to add imports with aliases instead of relative (or root) paths. This behaviour was implemented for 27658. Now autogenerated imports use...
Read more >Setting up Path Alias in TypeScript and tsc build without error
When using TypeScript, there are an option for developers to easily import their modules without typing so many ../../../ or absolute path.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I fixed it. I had this in my package.json:
I changed it to this:
Hello guys! Thanks for giving a feedback about my error!
At first, @randal923 I had already used these configurations and it didn’t work 😔, I would like to give you a huge thanks for offering a help on discord to me, but before I tried to get in touch with you, I’ve tried the tip that @danielrhodes has given in the last comment and it worked like a charm! It really looks like an issue of this module behavior when dealing with workspaces (so adding the module alias config in the root package.json really works).
A huge thanks for all the help!