Friday, 5 June 2020

How to import alias in node?

Hi,I have a node/express server that import files thanks to aliases (@myfolder/foo instead of ../../myfolder/foo). The aliases are defined in a typescript config file and a babel file. Unfortunately, they don't work. How to fix this?Here is my package json: { "name": "my-server", "license": "MIT", "version": "1.0.0", "main": "src/index.ts", "scripts": { "start": "nodemon", "tsc": "tsc --noEmit" }, "dependencies": { "body-parser": "1.19.0", "chance": "1.1.4", "express": "4.17.1" }, "devDependencies": { "@types/chance": "1.0.8", "@types/express": "4.17.2", "nodemon": "2.0.2", "rimraf": "3.0.2", "ts-node": "8.6.2", "typescript": "3.8.3" }, "nodemonConfig": { "watch": [ "src" ], "ext": "ts", "ignore": [ "src/**/*.spec.ts" ], "exec": "ts-node ./src/index.ts" } } My main index.js file: import bodyParser, { OptionsJson } from "body-parser"; import express from "express"; import myroute from "./routes/myroute"; const app = express(); app.use((req, res, next) => { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE"); res.header( "Access-Control-Allow-Headers", "authorization,cache-control,content-type,if-modified-since,pragma" ); next(); }); const options: OptionsJson = {}; app.use(bodyParser.json(options)); app.post("/myroute", myroute); app.listen(3000, () => console.info("listening on port 3000!")); export default app; My route where the alias imports fail: import { Request, Response } from "express"; import { generateMock } from "@myfolder/mock"; import * as API from "@myfolder/models/api"; const getResponse = (query: API.Query): API.QueryResponse => { return generateMock(query.name ); }; const main = (req: Request, res: Response) => { res.send(getResponse(req.body)); }; export default main; The error is: Error: \Cannot find module '@myfolder/mock'``The tsconfig file in the server: { "extends": "../../tsconfig.json", "compilerOptions": { "lib": ["dom", "es2015"], "outDir": "./dist/", "types": ["express", "chance"] }, "include": ["./src/**/*"], "exclude": ["./dist"] } the original tsconfig file which is at the root of the global app: { "files": ["custom.d.ts"], "include": ["myfolder"], "exclude": ["node_modules", "dist", "build", "public"], "compilerOptions": { "jsx": "react", "module": "commonjs", "resolveJsonModule": true, "esModuleInterop": true, "alwaysStrict": true, "target": "es6", "pretty": true, "noImplicitAny": false, "removeComments": true, "moduleResolution": "node", "sourceMap": true, "lib": ["es2015", "dom"], "allowSyntheticDefaultImports": true, "typeRoots": ["./myfolder/models/*", "node_modules/@types"], "rootDirs": ["./src", "../../"], "baseUrl": "./", "paths": { "@/*": [".//src/*"], "@myfolder/mock": ["./myfolder/mock/src/index.ts"], "@myfolder/mock/*": ["./myfolder/mock/src/*"], "@myfolder/models/*": ["./myfolder/models/src/*"], } } } And the babel file, at the root of the global app: module.exports = { ignore: ["node_modules"], babelrcRoots: ["."], presets: [ "@babel/preset-typescript", [ "@babel/preset-env", { targets: { browsers: ["defaults"] } } ], "@babel/preset-react" ], env: { test: { plugins: [ [ "babel-plugin-react-css-modules", { generateScopedName: "[local]", filetypes: { ".less": { syntax: "postcss-less" } } } ] // ] }, development: { plugins: [ [ "babel-plugin-react-css-modules", { webpackHotModuleReloading: true, generateScopedName: "[local]___[hash:base64:5]", handleMissingStyleName: "warn", filetypes: { ".less": { syntax: "postcss-less" } } } ] // "react-hot-loader/babel" ] }, production: { plugins: [ [ "babel-plugin-react-css-modules", { webpackHotModuleReloading: true, generateScopedName: "[hash:base64]", filetypes: { ".less": { syntax: "postcss-less" } } } ] ] } }, plugins: [ "@babel/plugin-transform-object-assign", "@babel/plugin-transform-regenerator", "@babel/plugin-transform-runtime", "@babel/plugin-syntax-dynamic-import", "@babel/plugin-proposal-class-properties", [ "module-resolver", { cwd: "babelrc", root: "./", alias: { "@myfolder/mock": "./myfolder/mock/src", "@myfolder/models": "./myfolder/models/src", } } ] ] }; How to fix this? Thanks!

Submitted June 05, 2020 at 12:59PM by MonsieurLeland

No comments:

Post a Comment