Can't import object from local types package? - node.js

I have an issue where I am trying to import an object from a types package in my project, built with yarn work spaces and Turbo-repo. But I am unable to because I get this error.
import {Injectable} from "#nestjs/common"
import {SAMPLE_SONG_INFO, Song} from "types"
#Injectable()
export class CurrentSongService {
private currentSong: Song = SAMPLE_SONG_INFO
get() {
return this.currentSong
}
set(song: Song) {
this.currentSong = song
}
}
I import it with "types": "*", in package.json. I have no project references setup in tsconfig.json
A brief overview of types file:
const SAMPLE_SONG_INFO: Song = {
name: "While My Guitar Gently Weeps",
album: "The Beatles",
artist: "The Beatles",
imageUrl: "sample-album-cover.jpeg"
}
interface Song {
name: string
album: string
artist: string
imageUrl: string
}
export type {Song}
export {SAMPLE_SONG_INFO}
When I run the application the error I get is:
const SAMPLE_SONG_INFO: Song = {
^^^^^^^^^^^^^^^^
SyntaxError: Missing initializer in const declaration
at compileFunction (<anonymous>)
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/media/joshua/379a09fe-3da5-4787-9a8c-cb522be8375d/code/personal/music-circle/apps/api/src/websockets/current-song/current-song.service.ts:2:1)
I run the program with NestJS run dev.
In NextJS I fixed this with next-transpile-modules.
Any help would be appreciated.
Thanks.

Related

How to use nanoid without import

I am stuck with an issue,
I have to generate a 6 digit alphanumeric CODE which should be unique and for that i am using nanoid,
Now when i code this:
const {nanoid} = require("nanoid");
const ID = nanoid();
I got error:
const {nanoid} = require("nanoid");
^
Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\HP\Desktop\test\node_modules\nanoid\index.js from C:\Users\HP\Desktop\test\server.js not supported.
Instead change the require of index.js in C:\Users\HP\Desktop\test\server.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\HP\Desktop\test\server.js:1:18) {
code: ←[32m'ERR_REQUIRE_ESM'←[39m
}
if I code this:
import { nanoid } from 'nanoid'
const id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"
i got error:
(node:4636) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
C:\Users\HP\Desktop\test\server.js:4
import { nanoid } from 'nanoid'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1033:15)
at Module._compile (node:internal/modules/cjs/loader:1069:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Now I have tried changing package.json file
"type":"module"
but my project use babel and it will automatically convert import to require and as result first error come again.
could you please tell me how to do nanoid with require'
Thank you
This is a feature, not a bug. See the changelog for details of the breaking change in version 4.0
https://github.com/ai/nanoid/issues/365

Nuxt - Node error when I want to import some packages

I use Nuxt, and sometimes, when I want to use some npm packages, I have this error:
SyntaxError
Unexpected token '<'
The stack:
vm.js:102:7
new Script
internal/modules/cjs/loader.js:1114:10
Module._extensions..js
internal/modules/cjs/loader.js:950:32
Module.load
internal/modules/cjs/loader.js:790:14
Module._load
internal/modules/cjs/loader.js:974:19
Module.require
webpack:/external "vue-typeahead-bootstrap":1:
Object.vue-typeahead-bootstrap
webpack/bootstrap:25:
__webpack_require__
pages/account/tabs/addresses.js:693:81
Module../node_modules/babel-loader/lib/index.js?!./node_modules/vue-loader/lib/index.js?!./components/address/form/country.vue?vue&type=script&lang=js&
webpack/bootstrap:25:
I have this error for example on this package: vue-typeahead-bootstrap
If I import the package:
import VueTypeaheadBootstrap from ['vue-typeahead-bootstrap'](https://github.com/mattzollinhofer/vue-typeahead-bootstrap)
export default {
components: { VueTypeaheadBootstrap },
}
It throws the error.
Is it because the package is not supported or something ?
You may try to transpile it. https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-build#transpile
Add the package name like this
{
build: {
transpile: [
({ isServer }) => 'vue-typeahead-bootstrap'
]
}
}
As answered here: https://github.com/mattzollinhofer/vue-typeahead-bootstrap/issues/19#issuecomment-645510809

The "amdefine" can not load a custom module

I installed the requirejs package to optimize the work with paths to files. In general, I do the next steps:
I have an object with paths to file, that have next values:
"app/database": "/home/dmitry/projects/information-platform-api/app/utils/database"
After I initialize the requirejs mapping in app.js (main file):
requirejs.config({
baseUrl: __dirname,
nodeRequire: require,
paths: paths,
map: {
'*': paths
}
});
After, I need to initialize base models, and with help for I called all files from paths object. This looks like:
for (let file in paths) {
requirejs(paths[file]);
}
When I initializing the file, which has the dependency of the above "app/database", I have a message error: Error: Cannot find module 'app/database'.
Maybe, I don't understand "How works the amdefine", but I have opinion, what a requirejs and amdefine don`t link. Do you have any ideas, why it doesn't work?
A part of the file, which need to investigate:
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define([
'sequelize',
'app/database',
'model/student'
], (
Sequelize,
sequelize,
Student
) => { });
Result of my investigation:
The amdefine doesn't know about requirejs mapping.
Full error message:
Error: Tried loading "/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company" at /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company then tried node's require("/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company") and it failed with error: Error: Cannot find module 'app/database'
Require stack:
- /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js
- /home/dmitry/projects/information-platform-api/app.js
- /home/dmitry/projects/information-platform-api/bin/www
at /home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2600:27
at Object.execCb (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1946:33)
at Module.check (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1133:51)
at Module.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1426:22)
at Object.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1807:39)
at Function.req.get (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2524:33)
at Object.localRequire [as require] (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1678:40)
at requirejs (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2047:24)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/app.js:142:5)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/bin/www:7:13)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
at internal/main/run_main_module.js:18:47 {
originalError: Error: Cannot find module 'app/database'
Require stack:
- /home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js
- /home/dmitry/projects/information-platform-api/app.js
- /home/dmitry/projects/information-platform-api/bin/www
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at req (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:144:31)
at stringRequire (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:220:28)
at amdRequire (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:113:24)
at /home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:177:24
at Array.map (<anonymous>)
at runFactory (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:176:25)
at define (/home/dmitry/projects/information-platform-api/node_modules/amdefine/amdefine.js:277:13)
at Object.<anonymous> (/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js:5:1)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
at Module.load (internal/modules/cjs/loader.js:1000:32)
at Function.Module._load (internal/modules/cjs/loader.js:899:14)
at Module.require (internal/modules/cjs/loader.js:1042:19)
at require (internal/modules/cjs/helpers.js:77:18)
at /home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:2598:75
at Object.execCb (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1946:33)
at Module.check (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1133:51)
at Module.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1426:22)
at Object.enable (/home/dmitry/projects/information-platform-api/node_modules/requirejs/bin/r.js:1807:39) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company.js',
'/home/dmitry/projects/information-platform-api/app.js',
'/home/dmitry/projects/information-platform-api/bin/www'
]
},
moduleName: '/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company',
requireModules: [
'/home/dmitry/projects/information-platform-api/app/code/IndividualScheduling/model/entity/Company'
]
}
Process finished with exit code 1
The solution is here guys. The amdefine have two params, first param it's a global variable module, and the second it's require function for calling the local files. By default, it was just require not requirejs which contains custom mapping.
if (typeof define !== 'function') {
var define = require('amdefine')(module, require('requirejs'));
}

Can't import multiple modules using 'import' [duplicate]

This question already has an answer here:
Node.js plans to support import/export ES6 (ECMAScript 2015) modules
(1 answer)
Closed 4 years ago.
I'm trying to import classes from a .js file, but when I run the program it seems to have a Syntax error. Here's my code:
import { RTUser, RTEvent, RTPurchase, RTMedia } from "./defs";
and here's the error that's being shown in the console.
/Users/williamg/retake/api/RTKit/index.js:34
import { RTUser, RTEvent, RTPurchase, RTMedia } from "./defs";
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:657:28)
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)
at Function.wrappedLoad [as _load] (/Users/williamg/retake/api/node_modules/newrelic/lib/shimmer.js:372:38)
at Module.require (internal/modules/cjs/loader.js:637:17)
An example of an export in the defs.js file is:
export class RTPurchase extends RTClass { }
The files are built from .ts files using tsc.
Any help would be appreciated!
Try Using var esLint = require('./ejbs');

SyntaxError when declaring a class in TypeScript

I'm getting an error when declaring a class:
// This is all on my test1.ts file content
class WDesign {
wModel: string;
wQuer: string;
}
let logWDesign = (wd : WDesign) => {
console.log(wd.wModel + " " + wd.wQuer);
}
let wd1 : WDesign;
wd1.wModel = "SDS-9985";
wd1.wQuer = "escarni";
logWDesign(wd1);
Then, to show on Node command prompt:
>node test1.ts
I get the following:
[filePath].test1.ts:3
wModel: string;
^
SyntaxError: Unexpected token :
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3
Typescript ver 3.1.3
Node ver. 8.12.0
Visual Studio Code 1.28.2
You firstly have to transpile the .ts file .js by simply running the command
tsc
node test1.js
This will generate javascript corresponding to your typescript files. Node cannot directly understand typescript. You have to give it javascript code.
You can use typescript-node-starter code provided by Typescript here to save configuration hustle.
Post comments if further help is required.

Resources