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??
Related
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
I am trying to use axios for a http request in my app.js file, but I always get the error message:
Uncaught SyntaxError: Cannot use import statement outside a module.
I created the js folder, files and other js packages using parcel-bundler and run browser-sync to start the server.
What am I missing? Please help I am new to JavaScript. I am on macOS Big Sur 11.5.1, node v14.17.4
import axios from 'axios'
document.addEventListener('DOMContentLoaded', () => {
const form = document.querySelector('form')
form.addEventListener('submit', async (event) => {
event.preventDefault()
const username = document.querySelector('input').value
const response =
await axios.get(`https://api.github.com/users${username}`)
console.log(response.data)
})
})
In the closest (from the root of project directory) package.json file, add or update "type" field with a value of "module". It will ensure that all your .js, .ts and .mjs files are interpreted as ES modules.
...
{
...
"type": "module"
...
}
...
Where can I add it in this file?
{
"name": "githubUser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}
Add "type": "module" to your package.json
{
// ...
"type": "module",
// ...
}
Update:
It should looks like this:
{
"name": "githubUser",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}
I am trying to implement local modules in my application
1.Project root folder i have created folder named test with a file named index.js
module.exports = {
myFunction:function(){
console.log('ok');
}
}
2.Added the following in package.json in the root folder
"dependencies": {
"test-module": "file:test"
}
3.When i try to import var module = require('test-module'); in app.js i got this error
Cannot find module 'test-module'
you can provide a path to a local directory that contains a package
{
"name": "baz",
"dependencies": {
"bar": "file:../foo/bar"
}
}
and perform npm install -s or npm install --save reference
To add on #Blaze answer, if you follow the steps (Local Paths) to install a local module, it will sort out for you the local dependency in your package.json:
npm i ./test --save
That will produce the correct local dependency entry in your dependencies in the root package.json:
"test-module": "file:test"
assuming test-module is the name in the local dep package.json.
This is how it should look like:
Make sure your test folder has a package.json.
test/package.json should have a "name" field with the value "test-module" (ie, same name as the dependency in your root package.json.
My files:
test/package.json
{
"name": "test-module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
test/index.js
module.exports = {
t:() => console.log('t')
};
package.json
{
"name": "t",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"test-module": "file:test"
}
}
app.js
t = require('test-module');
t.t();
This is working for me.
I am using babel to transpile my es6 code to es5 in node application.
I have used below babel node modules for this
"babel-cli": "6.24.0"
"babel-preset-es2015": "6.24.0"
"babel-preset-stage-2": "6.22.0"
And below is related configuration in package.json
{
"name": "twinconsole",
"version": "1.1.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prebuild": "rimraf dist",
"build": "babel --out-dir dist src"
},
"author": "'test#test.com'>",
"license": "MIT",
"devDependencies": {
"babel-cli": "6.24.0",
"babel-preset-es2015": "6.24.0",
"babel-preset-stage-2": "6.22.0",
"rimraf": "2.6.1"
},
"config": {
"babel": {
"presets": ["es2015" , "stage-2"]
}
}
}
I was expecting below es6 code which uses Arrow function
module.exports.print = msg => {
console.log(msg);
}
to be transpiled to
module.exports.print = function(msg) {
console.log(msg);
}
Instead the transpiled code still has arrow function.
Any idea what could be the issue.
Babel doesn't find your configuration because you didn't setup package.json correctly. From the docs:
You can alternatively choose to specify your .babelrc config from within package.json like so:
{
"name": "my-package",
"version": "1.0.0",
"babel": {
// my babel config here
}
}
Note that babel is at the top level, not inside config.
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');