Why Can't I set moment locale in node? - javascript

I'm running a node-app, this is all I have in app.js:
var moment = require('moment');
moment().locale('fr');
console.log(moment.locale())
I expect this to output 'fr' but it outputs 'en' instead, when I
runjs app.js
There is a fr-folder in node_modules/moment/locale.
Here are my packages:
{
"name": "zenqa",
"version": "1.0.0",
"description": "Faq for Zenconomy.se",
"main": "index.js",
"dependencies": {
"body-parser": "^1.14.2",
"concat-files": "^0.1.0",
"express": "^4.13.3",
"marked": "^0.3.5",
"moment": "^2.11.2",
"mustache-express": "^1.2.2",
"node-sass-middleware": "^0.9.7",
"php-unserialize": "0.0.1",
"requestify": "^0.1.17",
"validator": "^4.8.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Kristoffer Nolgren",
"license": "ISC",
"repository": {
"type": "git",
"url": "git#github.com:kristoffernolgren/ZenqaMiddleware.git"
}
}

According to the documentation there is a bug that prevents moment.locale from being loaded. So you can use the following code.
var moment = require('moment');
require('moment/locale/fr');
console.log(moment.locale());

You can use:
import moment from "moment";
import "moment/locale/es";

Spent hours on this just to realise I had to import the locale from /dist dir instead. Maybe it's just caused by my setup but might help someone
import moment from 'moment';
import 'moment/dist/locale/fr';
moment.locale('fr');
Test
import fr1 from 'moment/dist/locale/fr';
import fr2 from 'moment/locale/fr';
console.log(fr1); // Locale {_calendar: {…}, ... }
console.log(fr2); // {}

According to the current documentation it is possible:
var moment = require('moment');
moment.locale('fr');
console.log(moment().format());

For node you can use:
moment.updateLocale('es');
I have tested it.

Current document is not correct. In Node.JS you still use this code:
moment.lang('fr');

Related

Google lens serpapi is giving me an abortsignal error. The code was the same the other day and now this error popped up?

My nodejs script was working perfectly fine the other day and now I'm getting this abort signal error below. The code is literally copied verbatim from the serpapi site. I had my own code that's slightly different but I get the same error even when just using the site's sample code. https://serpapi.com/google-lens-api. It seems to be a local issue because when I ran it standalone on Replit it seemed to be fine. My package json is below as well.
import { getJson } from "serpapi";
const params = {
url: "https://i.imgur.com/HBrB8p0.png",
api_key: "{xxx}"
};
// Show result as JSON
const response = await getJson("google_lens", params);
console.log(response["visual_matches"]);
"name": "cheerio",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"#google-cloud/vision": "^3.1.1",
"axios": "^1.3.2",
"cheerio": "^1.0.0-rc.12",
"jimp": "^0.22.4",
"node-fetch": "^3.3.0",
"openai": "^3.1.0",
"serpapi": "^1.1.0"
signal: AbortSignal.timeout(timeout),
^
ReferenceError: AbortSignal is not defined

Jest Setup with Next.js fails after it seems to add a react import to index.test.js

Hi I have been stewing over this for the last couple of days. I realise this is a common error with various solutions.
I have the dreaded:
Jest encountered an unexpected token
/__tests__/index.test.js:16
import React from "react";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)
I have read a few posts and the docs in next and jest.
package.json:
{
"name": "some-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "jest --watch"
},
"dependencies": {
"next": "12.1.6",
"react": "18.1.0",
"react-dom": "18.1.0"
},
"devDependencies": {
"#babel/plugin-syntax-jsx": "^7.17.12",
"#babel/preset-env": "^7.18.2",
"#babel/preset-react": "^7.17.12",
"#testing-library/jest-dom": "^5.16.4",
"#testing-library/react": "^13.3.0",
"#types/jest": "^28.1.3",
"#types/node-fetch": "^2.6.2",
"babel-jest": "^28.1.1",
"eslint": "8.16.0",
"eslint-config-next": "12.1.6",
"identity-obj-proxy": "^3.0.0",
"jest": "^28.1.1",
"jest-environment-jsdom": "^28.1.1",
"node-fetch": "^3.2.6",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
},
"description": "This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).",
"main": "jest.config.js",
"repository": {
"type": "git",
},
"author": "",
"license": "ISC",
"bugs": {
},
}
index.test.js:
import { render, screen } from '#testing-library/react'
import Home from '../pages/index'
import '#testing-library/jest-dom'
import React from "react";
describe('Home', () => {
it('renders a h', () => {
render(<Home />)
const link = screen.getByRole('link', {
name: "Library",
})
expect(link).toHaveAttribute('href', '/storage-area')
})
});
jest.config.js:
module.exports = {
collectCoverage: true,
// on node 14.x coverage provider v8 offers good speed and more or less good report
coverageProvider: 'v8',
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx}',
'!**/*.d.ts',
'!**/node_modules/**',
'!<rootDir>/out/**',
'!<rootDir>/.next/**',
'!<rootDir>/*.config.js',
'!<rootDir>/coverage/**',
],
moduleNameMapper: {
// Handle CSS imports (with CSS modules)
// https://jestjs.io/docs/webpack#mocking-css-modules
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
// Handle CSS imports (without CSS modules)
'^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',
// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,
// Handle module aliases
'^#/components/(.*)$': '<rootDir>/components/$1',
},
// Add more setup options before each test is run
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
testEnvironment: 'jsdom',
transform: {
// Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel', '#babel/preset-react',"#babel/preset-env" ]}],
},
transformIgnorePatterns: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],
}
As I mentioned in the title the it seems that the error message suggests that the import is added by Next.js to the end of the index.test.js.
This was a problem due to the tests folder being nested in the directory above the root directory of the project. By the time I figured this out I had manipulated the jest.config.js transform: presets array property to contain imports that it didn't require in the standard jest/next setup.

#parcel/core: Failed to resolve 'index.html' Getting this error

My package.json file
{
"name": "aiky",
"version": "1.0.0",
"description": "Recipe application",
"default": "index.html",
"scripts": {
"start": "parcel index.html",
"build": "parcel build index.html --dist-dir ./dist"
},
"author": "Akash Kesharwani",
"license": "ISC",
"devDependencies": {
"#parcel/resolver-glob": "^2.0.0-rc.0",
"#parcel/transformer-image": "^2.0.0-rc.0",
"#parcel/transformer-sass": "^2.0.0-rc.0",
"parcel": "^2.0.0-rc.0",
"sass": "^1.26.10"
},
"dependencies": {
"core-js": "^3.6.5",
"fractional": "^1.0.0",
"regenerator-runtime": "^0.13.7"
},
"main": "index.js"
}
I can't find the solution over the internet.
Before this, I was getting this error
#parcel/core: Failed to resolve 'src/img/test-1.jpg' from './index.html'
#parcel/resolver-default: Cannot load file './src/img/test-1.jpg' in './'.
So, I installed #parcel/resolver-glob and also added .parcelrc in root with this text
{
"extends": "#parcel/config-default",
"resolvers": ["#parcel/resolver-glob"]
}
It's not very obvious from the Parcel documentation, but you will need to include the default resolvers by adding the literal string "..." to the resolvers array.
The "..." syntax can be used to extend the default resolvers. This allows you to override the resolution for certain dependencies, but fall back to the default for others. Generally, you'll want to add your custom resolvers before running the default ones.
{
"extends": "#parcel/config-default",
"resolvers": ["#parcel/resolver-glob", "..."]
^^^^^
}

Babel is outputting a file which node failes to run citing "require is not defined"

I am encountering an error as displayed below. The error arrises when I run the following command in an attempt to build and run a node project with babel. The issue arose on a larger project, I recreated the basic components in order to better diagnose the issue, but I am stumped now.
When running
babel --extensions ".ts" index.ts -o dist/index.js;node dist/index.js
node returns
file:///Users/sadeksyed/Documents/Programming%20Projects/HC/test-babel-proj/dist/server.js:3
var _path = _interopRequireDefault(require("path"));
^
ReferenceError: require is not defined
at file:///Users/sadeksyed/Documents/test-babel-proj/dist/server.js:3:13
at ModuleJob.run (internal/modules/esm/module_job.js:110:37)
at async Loader.import (internal/modules/esm/loader.js:179:24)
This file index.ts looks like:
import path from "path";
var __dirname = path.resolve();
import http from "http";
import dotenv from "dotenv";
dotenv.config({ path: __dirname + "/test/.env" });
console.log(process.env.PORT);
.babelrc
{
"presets": [
[
"#babel/preset-env",
{
"targets": {
"esmodules": true
}
}
],
"#babel/preset-typescript"
],
"plugins": ["#babel/plugin-transform-runtime"]
}
package.json
{
"name": "test-babel-proj",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"type": "module",
"author": "",
"license": "ISC",
"devDependencies": {
"#babel/core": "^7.10.2",
"#babel/plugin-transform-runtime": "^7.10.1",
"#babel/plugin-transform-typescript": "^7.10.1",
"#babel/preset-env": "^7.10.2",
"#babel/preset-typescript": "^7.10.1",
"#types/node": "^14.0.13"
},
"dependencies": {
"dotenv": "^8.2.0"
}
}
I've been attempting to resolve this problem all day yesterday and today. Any help would be appreciated!

error: can not use import outside a module?

I never used a module outside a boilerplate created by for example create-react-app or angular-cli, now i am trying to mimic modularity without using them, and i get an error: cannot use import outside a module
this is my package.json file:
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"babel-core": "^6.26.3",
"babel-preset-env": "^1.7.0"
}
}
I am using a live-server to create a server for my project, I am failing to do that:
App.js:
const x = 10;
export default x;
index.js:
import x from './App' // can not use import outside a module
The question is what i missing here, what do i really need to create a module??

Categories