Webstorm Unexpected Token export - node.js

I am having an "Unexpected token export" issue in Webstorm that has not been solved by the other StackOverflow posts. Essentially I am trying to use the import/export module functionality with the package.json and bar.js code below. I am using Node.js 5x, Babel 6, and I have a File Watcher setup to do the Babel transforms on the fly.
The code should speak for itself, and I appreciate any thoughts on how to resolve it. Again, I have tried the other StackOverflow suggestions with no luck at this point.
//bar.js
'use strict';
export class Bar{
constructor(){
this.tempish = 'allo';
}
}
//bar-compiled.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Bar = exports.Bar = function Bar() {
_classCallCheck(this, Bar);
this.tempish = 'allo';
};
//# sourceMappingURL=bar-compiled.js.map
//index.js
'use strict';
import {Bar} from './bar'
console.log('This is a test.');
//index-compiled.js
'use strict';
var _bar = require('./bar');
console.log('This is a test.');
//# sourceMappingURL=index-compiled.js.map
//package.json
{
"name": "npt-test",
"version": "1.0.0",
"description": "",
"main": "index-compiled.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.3.26",
"babel-cli": "^6.4.5",
"babel-core": "^6.4.5",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-stage-0": "^6.3.13"
}
}
//.babelrc
{
"presets": ["es2015", "stage-0", "stage-1"],
"plugins": ["babel-plugin-transform-decorators-legacy"]
}
//Error on debug, I am running against the index-compiled.js during debug
C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe"
"C:\Program Files\nodejs\node.exe" --debug-brk=45287 --nolazy index-compiled.js
Debugger listening on port 45287
[MYPROJECTDIR]\bar.js:3
export class Bar{
^^^^^^
SyntaxError: Unexpected token export
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> ([MYPROJECTDIR]\index-compiled.js:3:12)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
Process finished with exit code 1

what does your index-compiled.js code look like? seems it requires original bar.js rather than bar-compiled.js. Node.js can't natively execute ES6 code, thus the errors.
I'd recommend configuring the compiler to output transpiled code into a separate directory to avoid using 'compiled' postfix. Please see the following link for instructions on setting up Babel 6 with WebStorm: http://mcculloughwebservices.com/2015/12/10/webstorm-babel-6-plugin/

Related

Cannot import csv-parse on lambda in nodejs 12

I am writing a code which requires csv-parse on lambda.
Runtime is node 12.x.
I installed following modules via npm 6.14.10 (node v12.20.1)
{
"name": "data_analysis_node",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"aws-sdk": "^2.1282.0",
"csv-parse": "^5.3.3",
"fs": "0.0.1-security"
}
}
Then I zipped a node_modules directory, upload it, attached it to a layer, applyed the layer to the function and wrote a following code.
index.js
const AWS = require('aws-sdk');
const parse = require('csv-parse/lib/sync');
const fs = require('fs');
exports.handler = async (event) => {
console.log('fine')
};
but it has not been working and showing following errors.
2022-12-26T14:28:45.472Z undefined ERROR Uncaught Exception {
"errorType":"Runtime.ImportModuleError"
,"errorMessage":"Error: Cannot find module 'csv-parse/lib/sync'\nRequire
stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'csv-parse/lib/sync'","Require stack:","- /var/task/index.js","- /var/runtime/UserFunction.js","-
/var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:999:30)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)"," at Module.load (internal/modules/cjs/loader.js:863:32)"," at Function.Module._load (internal/modules/cjs/loader.js:708:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)"," at internal/main/run_main_module.js:17:47"]}
I re-checked the inside the node_modules, and I confirmed the path to csv-parse is correct.
I also commented out the line imports csv-parse, and it didn't output any errors and seemed importing other modules fine.
const AWS = require('aws-sdk');
//const parse = require('csv-parse/lib/sync');
const fs = require('fs');
exports.handler = async (event) => {
console.log('fine')
};
I have been stacked 3 to 4 hours, so any suggestion are helpful.
When creating a Lambda layer for NodeJS, structure in your zip file should be nodejs/node_modules/ and not just node_modules/
Optionally, for multiple versions, you can put nodejs/nodeXY/node_modules/
You can read more about Lambda layers in the documentation
Would something like this work?
const csvParse = require('csv-parse');
// do whatever with csvParse
It may be that there is a naming collision with the csv-parse API
EDIT
According to the docs you need to require the parse module from csv-parse/sync, not csv-parse/lib/sync
The following should work:
const parse = require('csv-parse/sync');
I received the same error you did when requiring from csv-parse/lib/sync and it worked fine when required from csv-parse/sync

TypeError: firestoreService.initializeApp is not a function

I am trying to write a TS script that would take a JSON file and import it into Firebase collection as a document. I am getting inspired by this tutorial that uses firestore-import-export package. However, even though I have installed the package, I cannot initialize it.
My scripts/test_import_data.ts script looks like this:
const firestoreService = require('firestore-export-import');
const serviceAccount = require(__dirname + '/../serviceAccountStaging.json');
const firebaseConfig = require(__dirname + '/../config.js');
async function main() {
await firestoreService.initializeApp(serviceAccount, firebaseConfig.databaseURL);
}
main()
.then(() => {
process.exit(0)
})
.catch((error) => {
console.error(error)
process.exit(1)
})
My package.json looks like this:
{
...
"engines": {
"node": "~v16.14.2",
"npm": "~8.5.0"
},
"scripts": {
"script": "ts-node"
},
"devDependencies": {
"#types/yargs": "^17.0.10",
"dotenv": "^16.0.0",
"firestore-export-import": "^1.1.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.4",
"yargs": "^17.4.1"
}
}
In terminal, I am running the script like this with the following error:
my#mac % npm run script scripts/test_import_data.ts
TypeError: firestoreService.initializeApp is not a function
at main (/Users/mymac/projects/my-project/scripts/test_import_data.ts:6:26)
at Object.<anonymous> (/Users/mymac/projects/my-project/scripts/test_import_data.ts:9:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module.m._compile (/Users/mymac/projects/my-project/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Object.require.extensions.<computed> [as .ts] (/Users/mymac/projects/my-project/node_modules/ts-node/src/index.ts:1458:12)
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 phase4 (/Users/mymac/projects/my-project/node_modules/ts-node/src/bin.ts:567:12)
After Google/StackOverflow research, I am lost, now. Have you ever faced similar issue? Do you know what I am doing wrong, please? Any help is appreciated! ♥️
It looks like you might be using an old API for that npm library that was in use before the Firebase SDKs went modular. Take a look at the documentation on npm very carefully that you linked to. The example code looks like this:
const { initializeFirebaseApp } = require('firestore-export-import')
initializeFirebaseApp(serviceAccount, appName, options)
Notice that the initializeFirebaseApp function is a direct import. There is no "service" object export to call methods on.

ts-node cannot find npm module discord-api-types

When attempting to run discord.js file deploy-commands.ts with the command ts-node deploy-commands.ts ts-node produces the following error:
Error: Cannot find module 'discord.js/node_modules/discord-api-types'
Require stack:
- /Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/usr/local/lib/node_modules/ts-node/node_modules/#cspotcode/source-map-support/source-map-support.js:679:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts:4:1)
at Module._compile (node:internal/modules/cjs/loader:1099:14)
at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:1455:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:1458:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/Cameron/discord.js/discordjs-guides/deploy-commands.ts' ]
}
Despite discord-api-types appearing both in the node-modules folder and in package.json. The error is consistently reproduced after running npm install discord-api-types and npm resoding discord-api-types is up-to-date.
Package.json dependencies:
"dependencies": {
"#discordjs/builders": "^0.12.0",
"#discordjs/rest": "^0.3.0",
"discord-api-types": "^0.29.0",
"discord.js": "^13.6.0",
"dotenv": "^16.0.0",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0"
}
deploy-commands.ts:
import { SlashCommandBuilder } from "#discordjs/builders";
import { REST } from "#discordjs/rest";
import { version } from "discord.js";
import { Routes } from "discord.js/node_modules/discord-api-types";
const { token, guildID, clientID } = require('./process.json')
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong.'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info.'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info.'),
]
.map(commands => JSON)
const rest = new REST({version: '9'}).setToken(token)
rest.put(Routes.applicationCommand(clientID, guildID), {body: { commands }})
.then(() => {
console.log('Successfully registered application commands')
})
.catch(console.error)
I think you need to change this code line:
import { Routes } from "discord.js/node_modules/discord-api-types";
To its version using because of this:
const rest = new REST({version: '9'}).setToken(token)
So you need to define the version you use, try changing your import to
import { Routes } from "discord-api-types/v9";
As I tried to my testing slash command files, you can do this kind of line too:
import { Routes } from "discord.js/node_modules/discord-api-types/v9";
I'm not using .ts so might not work but give it a shot

I'm attempting to make a discord bot but I'm having trouble running it

Hi so I'm pretty new to code and I'm trying my best with just creating a simple discord bot, but I can't even run it or turn it on to begin with. When I use "node ." or "node index.js" it gives me this.
internal/modules/cjs/loader.js:968
throw err;
^
Error: Cannot find module 'discord.js)'
Require stack:
- C:\Users\Madison\Desktop\BeepBoop\index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
at Function.Module._load (internal/modules/cjs/loader.js:841:27)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (C:\Users\Madison\Desktop\BeepBoop\index.js:1:17)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
{
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\\Users\\Madison\\Desktop\\BeepBoop\\index.js' ]
Someone mentioned installing discord.js but that still isn't working either. Its very simple so far here is the bot
const Discord = require('discord.js)');
const bot = new Discord.Client();
const token = 'NzQ1NzY2OTQ2NDQzNjI0NDg4.Xz2jlw.sKIp-VYeOdTHTrBWmu2DOnHlgq4';
bot.on('ready', () =>{
console.log('This bot is online!');
})
bot.login(token);
and here are the .json package contents
{
"name": "beep-boop",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "Upsetti",
"license": "ISC",
"dependencies": {
"discord.js": "^12.3.1",
"dotenv": "^8.2.0",
"express": "^4.17.1"
}
}
pls help :'>
You have a typo. You are currently saying:
require('discord.js)')
It should be:
require('discord.js') // Note the ")" inside the quotes has been removed
I am not too familiar with discord, but you may have shared a private token in your question. If this is the case you may want to obtain a new token, and keep this new one private.
This isn't the answer but please do not add your token online, otherwise generate a new one because there are people on the website looking for Discord tokens to hack and destroy your bot.

Failed loading config.ts due to import protractor

I am trying to start a new protractor project to test an angular site. I installed node.js, typescript, protractor globally and jasmine. I go to the project folder and do webdriver-manager update. Then I do webdriver-manager start. I also build the config.ts using tsc config.ts. Everything works fine until i try protractor config.ts. Here i will provide my config.ts and my package.json.
{
"name": "protractortests",
"version": "1.0.0",
"description": "Automated tests for a game platform",
"main": "index.js",
"dependencies": {
"#types/jasmine": "^3.3.12",
"#types/node": "^12.0.2",
"jasmine": "^3.4.0",
"protractor": "^5.4.2"
},
"devDependencies": {},
"scripts": {
"test": "protractor config.ts"
}
and my config.ts:
import { ProtractorBrowser, Config } from "protractor";
export let config: Config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
framework: 'jasmine',
specs: ['./FirstSpec.ts'],
jasmineNodeOpts: {
defaultTimeoutInterval: 90000
},
onPrepare: () => {
let globals = require('protractor/built');
let browser = globals.browser;
browser.manage().window().maximize();
browser.manage().timeouts().implicitlyWait(5000);
}
}
E/configParser - Error code: 105
[11:40:53] E/configParser - Error message: failed loading configuration file config.ts
[11:40:53] E/configParser - C:\Users\Victor\Documents\ProtractorTests\config.ts:1
(function (exports, require, module, __filename, __dirname) { import { ProtractorBrowser, Config } from "protractor";
^
SyntaxError: Unexpected token {
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)
npm ERR! Test failed. See above for more details.
By referring to example at link https://github.com/angular/protractor/tree/5.4.1/exampleTypescript
You don't need to import ProtractorBrowser. You can work with browser directly with object Browser.
Commenters pointed out that you can't give Protractor a config file in native Typescript, and need to compile it to config.js then pass that. There's really no point in writing the file in Typescript at all, it just adds an extra step that provides no value to you. If you want editor autocomplete, you can decorate your JS with type annotations:
const { join } = require("path");
const { register } = require("ts-node");
const { SpecReporter, StacktraceOption } = require("jasmine-spec-reporter");
/** #type {import("protractor").Config} */
const config = {
directConnect: true,
baseUrl: "http://localhost:8080",
framework: "jasmine",
noGlobals: true,
specs: [ "./src/**/*.e2e.ts" ],
onPrepare() {
register({
project: join(__dirname, "./tsconfig.json")
});
jasmine.getEnv().addReporter(new SpecReporter({
spec: {
displayStacktrace: StacktraceOption.PRETTY
}
}));
}
};
module.exports = { config };
I adapted my config from this excellent example.

Resources