I'm trying to put my application into production. But, the teacher who was teaching put it for me, the lib 'sucrase' to use syntax of "import from" instead of "require". Only now Heroku is complaining that he doesn't understand the syntax "import". Is there anything I can do about line of code and configuration? Or do I have to change all "import from" to "require"? ... Lower the logs ...
logs in Heroku
2020-06-27T20:21:41.876688+00:00 app[web.1]: /app/src/server.js:1
2020-06-27T20:21:41.876689+00:00 app[web.1]: import app from './app';
2020-06-27T20:21:41.876690+00:00 app[web.1]: ^^^^^^
2020-06-27T20:21:41.876690+00:00 app[web.1]:
2020-06-27T20:21:41.876691+00:00 app[web.1]: SyntaxError: Cannot use import statement outside a module
2020-06-27T20:21:41.876691+00:00 app[web.1]: at wrapSafe (internal/modules/cjs/loader.js:1054:16)
2020-06-27T20:21:41.876692+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1102:27)
My package.json
{
"name": "futs",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "nodemon src/server.js",
/ ^ ^ ^ Below at the code is a configuration about the 'nodemon' that the teacher gave me
"dev:debug": "nodemon --inspect src/server.js"
},
"dependencies": {
"aws-sdk": "^2.704.0",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"date-fns": "^2.14.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"multer": "^1.4.2",
"multer-s3": "^2.9.0",
"nodemailer": "^6.4.10",
"pg": "^8.2.1",
"sequelize": "^5.21.13",
"sequelize-cli": "^5.5.1",
"uuid": "^8.1.0",
"yup": "^0.29.1"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.1.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.3",
"nodemon": "^2.0.4",
"prettier": "^2.0.5",
"sucrase": "^3.15.0"
}
}
nodemon.json
{
"execMap":{
"js": "node -r sucrase/register"
}
}
I don't know how to run to deploy, if anyone can help me
As you see in the error log : Cannot use import statement outside a module.
import statements are permitted only in ES modules not CommonJS see more about that here :
However you can make Node treat your file as a ES module you need to :
add "type": "module" to package.json .
Add --experimental-modules flag upon running your app(not necessary with Node 13.2.0+ so check your node version with node --version)
as follow :
// package.json
{
"type": "module"
}
take a look here for more information .
OR :
Alternatively you can use the .mjs instead of .jsextension .
SOURCES :
SyntaxError: Cannot use import statement outside a module
https://nodejs.org/api/esm.html .
https://nodejs.org/api/esm.html#esm_enabling .
https://nodejs.org/api/esm.html#esm_code_import_code_statements
To build my app using sucrase I use:
sucrase ./src -d ./dist --transforms imports
This is my package.json file:
"scripts": {
"dev": "nodemon src/server.js",
"build": "sucrase ./src -d ./dist --transforms imports",
"start": "node dist/server.js"
},
Related
Install react-calendar-datetime-picker and every time I try to launch the project it just goes blank. This is the error it gives me in the console:
[ERROR] [plugin vite:dep-scan] Failed to resolve entry for package "react-calendar-datetime-picker". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "react-calendar-datetime-picker". The package may have incorrect main/module/exports specified in its package.json.
node_modules/vite/dist/node/chunks/dep-611778e0.js:38290:10:
38290 │ throw new Error(`Failed to resolve entry for ...
╵ ^
at packageEntryFailure (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:38290:11)
at resolvePackageEntry (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:38286:9)
at tryNodeResolve (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:38093:20)
at Context.resolveId (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:37901:28)
at Object.resolveId (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:36470:55)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async resolve (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:36682:26)
at async /home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:36839:34
at async callback (/home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:921:28)
at async handleRequest (/home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:701:30)
This error came from the "onResolve" callback registered here:
node_modules/vite/dist/node/chunks/dep-611778e0.js:36829:18:
36829 │ build.onResolve({
╵ ~~~~~~~~~
at setup (/home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:36829:19)
at handlePlugins (/home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:843:23)
at Object.buildOrServe (/home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:1137:7)
at /home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:2079:17
at new Promise (<anonymous>)
at Object.build (/home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:2078:14)
at Object.build (/home/guille/Documentos/pg_4/client/node_modules/esbuild/lib/main.js:1927:51)
at /home/guille/Documentos/pg_4/client/node_modules/vite/dist/node/chunks/dep-611778e0.js:36630:54
at Array.map (<anonymous>)
The plugin "vite:dep-scan" was triggered by this import
src/components/Calendar/Calendar.jsx:2:23:
2 │ import DtPicker from 'react-calendar-datetime-picker'
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Build failed with 1 error:
node_modules/vite/dist/node/chunks/dep-611778e0.js:38290:10: ERROR: [plugin: vite:dep-scan] Failed to resolve entry for package "react-calendar-datetime-picker". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "react-calendar-datetime-picker". The package may have incorrect main/module/exports specified in its package.json.
Failed to resolve entry for package "react-calendar-datetime-picker". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "react-calendar-datetime-picker". The package may have incorrect main/module/exports specified in its package.json.
This is my package.json
{
"name": "client",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"#auth0/auth0-react": "^1.9.0",
"#date-io/moment": "^2.13.1",
"#fortawesome/fontawesome-svg-core": "^6.1.1",
"#fortawesome/free-brands-svg-icons": "^6.1.1",
"#fortawesome/free-solid-svg-icons": "^6.1.1",
"#fortawesome/react-fontawesome": "^0.1.18",
"#headlessui/react": "^1.5.0",
"#mapbox/mapbox-sdk": "^0.13.3",
"#material-tailwind/react": "0.3.4",
"#types/react-transition-group": "^4.4.4",
"axios": "^0.26.1",
"jalaali-js": "^1.2.4",
"mapbox-gl": "^2.7.1",
"moment": "^2.29.2",
"react": "^17.0.2",
"react-calendar-datetime-picker": "^1.6.2",
"react-dom": "^17.0.2",
"react-icons": "^4.3.1",
"react-map-gl": "^7.0.10",
"react-redux": "^7.2.6",
"react-router-dom": "^6.2.2",
"redux-devtools-extension": "^2.13.9",
"redux-thunk": "^2.4.1",
"tw-elements": "^1.0.0-alpha12"
},
"devDependencies": {
"#vitejs/plugin-react": "^1.0.7",
"autoprefixer": "^10.4.4",
"postcss": "^8.4.12",
"tailwindcss": "^3.0.23",
"vite": "^2.8.0"
}
}
And my vite.config.js
import { defineConfig } from 'vite'
import react from '#vitejs/plugin-react'
export default defineConfig({
plugins: [react()]
})
that could be happening? How can I solve that?
I need to use this library since it is the most customizable and with the best documentation
I am working on a register user setup in React JS, installed crypto library from to secure passwords but when I run the program , it gives me an error :
ERROR in ./node_modules/jwa/index.js 5:13-30
Module not found: Error: Can't resolve 'crypto' in 'E:\Node
Tutorial\registerSetup\client\node_modules\jwa'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js
core modules by default. This is no longer the case. Verify if you
need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
add a fallback 'resolve.fallback: { "crypto": require.resolve("crypto-browserify") }'
install 'crypto-browserify' If you don't want to include a polyfill, you can use an empty module like this: resolve.fallback: {
"crypto": false }
Here is my package.json file :
{
"name": "registerSetup",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "NODE_ENV= production node server",
"dev": "nodemon server"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"crypto": "^1.0.1",
"crypto-browserify": "^3.12.0",
"dotenv": "^15.0.0",
"express": "^4.17.2",
"express-jwt": "^6.1.0",
"express-validator": "^6.14.0",
"google-auth-library": "^7.11.0",
"jsonwebtoken": "^8.5.1",
"loadash": "^1.0.0",
"lodash": "^4.17.21",
"mongoose": "^6.1.8",
"morgan": "^1.10.0",
"node-fetch": "^3.2.0",
"nodemailer": "^6.7.2",
"nodemon": "^2.0.15"
}
}
The package.json for my react folder in registerSetup --> https://pastebin.com/hqBs7J4s
I think you should go to your node_modules/react-scripts/config/webpack.config.json and there write the following code in resolve block
fallback: {
"crypto": require.resolve("crypto-browserify")
}
Note: crypto-browserify should be installed
Crypto is a built-in Node module you don't have to install it. Uninstall "crypto": "^1.0.1" and then try:
const crypto = require("crypto")
You can try to downgrade the react-scripts in package.json version to : 4.0.3
I tried to deploy my project developed using React and Node.js to Heroku, but after issuing git push heroku master, I got an error:
2020-06-05T02:12:18.092681+00:00 app[web.1]: import express from "express";
2020-06-05T02:12:18.092682+00:00 app[web.1]: ^^^^^^^
2020-06-05T02:12:18.092682+00:00 app[web.1]:
2020-06-05T02:12:18.092683+00:00 app[web.1]: SyntaxError: Unexpected identifier
2020-06-05T02:12:18.092683+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:718:23)
I googled and it is said that Node.js doesn't support ES6 syntax, and I can solve it by using babel.
But I don't know how to configure it.
Below are my files:
.babelrc.json:
{
"presets": [
[
"#babel/preset-env",
{
"targets": {
"node": "current"
}
}
]
]
}
package.json:
{
"name": "react_e-commerce",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon --watch backend --exec babel-node backend/server.js",
"build": "rimraf dist && babel backend -d dist",
"heroku-postbuild": "npm run build && cd e-commerce && npm install && npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.16"
},
"devDependencies": {
"#babel/cli": "^7.10.1",
"#babel/core": "^7.10.1",
"#babel/node": "^7.10.1",
"#babel/preset-env": "^7.10.1",
"babel-cli": "^6.26.0",
"mkdirp": "^1.0.4",
"nodemon": "^2.0.4",
"rimraf": "^3.0.2"
},
"engines": {
"node": "12.4.0",
"npm": "6.9.0"
}
}
And my whole project is on https://github.com/powerseed/e-commerce-React
The e-commerce folder is for frontend.
Thanks in advance!
You can just use require if you want. Here are the details on why there are two ways to bring in files. The difference between "require(x)" and "import x"
per the Nodejs docs I was able to use the following to run node node-module.js and similar may work in your situation--I'm unfamiliar with the specific node version you're working with currently, but the "type":"module" option would likely allow it--I'm using Nodejs v15 at the moment.
package.json
{
"type": "module"
}
node-module.js
import * as os from 'os';
import defaultstuff, { stuff } from './node-import-stuff.js';
console.log(123 === stuff, defaultstuff, Object.keys(os));
node-import-stuff.js
export const stuff = 123;
export default stuff;
I am developing a WebGL library that I would like to import into an EmberJS project as a dependency. Unless I'm mistaken, I believe that I can do this via the repository directly without having to make an npm package but I am having trouble getting it to work.
I have made a watered down library and ember project in a couple repos here and here respectively to demonstrate my problem.
If you clone the library and run npm run build it'll make a test bundle which can be called by the test html file packageTest.html. It should print out 'Hello World Test Member is: 5'.
In the Ember project I have a component in which I would like to import the 'HelloWorld' class from the library and call one of its member methods.
import Ember from 'ember';
//import HelloWorld from 'npm-package-test';
export default Ember.Component.extend({
isWide: false,
actions: {
toggleImageSize() {
// var h = new HelloWorld();
// console.log(h.print());
this.toggleProperty('isWide');
}
}
});
When I uncomment the import statement I get the console error
Error: Could not find module 'npm-package-test'
I'm still pretty new to npm packaging and how dependencies work (and know next to nothing about Ember) but from my limited understanding I feel like this method should work the way I currently have it.
For the library, I have the source files being babeled into ES5 in its lib folder. As you can see in the package.json for the library below I have the main set to the index file in the lib folder so that the Ember project can pull the babeled modules.
Library: package.json
{
"name": "npm-package-test",
"version": "1.0.0",
"description": "JibJab Render Library for eCards",
"main": "lib/index.js",
"scripts": {
"prepublishOnly": "npm run build",
"build-test": "browserify test.js > demo/testbundle.js",
"build": "babel ./src -d ./lib && npm run build-test",
"lint": "eslint ./src",
"test": "nyc mocha --require babel-core/register"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nhoughto5/NPM_PackageTest.git"
},
"author": "JibJab",
"license": "ISC",
"bugs": {
"url": "https://github.com/nhoughto5/NPM_PackageTest/issues"
},
"homepage": "https://github.com/nhoughto5/NPM_PackageTeste#readme",
"devDependencies": {
"babel-cli": "6.26.0",
"babel-preset-env": "1.6.1",
"eslint": "4.19.0",
"mocha": "5.0.4",
"nyc": "11.6.0"
},
"nyc": {
"reporter": [
"lcov",
"text"
]
},
"dependencies": {
"domready": "^1.0.8"
}
}
For reference, here is the lib/index.js which should be the entry point of my library:
Library: lib/index.js
'use strict';
module.exports = {
TestClass: require('./TestClass'),
HelloWorld: require('./HelloWorld')
};
In the ember project I have the library repository listed as a dependency:
Ember: package.json
{
"name": "test-ember-app",
"version": "0.0.0",
"description": "Small description for test-ember-app goes here",
"license": "MIT",
"author": "",
"directories": {
"doc": "doc",
"test": "tests"
},
"repository": "",
"scripts": {
"build": "ember build",
"start": "ember server",
"test": "ember test"
},
"devDependencies": {
"broccoli-asset-rev": "^2.4.5",
"ember-ajax": "^3.0.0",
"ember-browserify": "1.2.1",
"ember-cli": "2.13.1",
"ember-cli-app-version": "^3.0.0",
"ember-cli-babel": "^6.0.0",
"ember-cli-dependency-checker": "^1.3.0",
"ember-cli-eslint": "^3.0.0",
"ember-cli-htmlbars": "^1.1.1",
"ember-cli-htmlbars-inline-precompile": "^0.4.0",
"ember-cli-inject-live-reload": "^1.4.1",
"ember-cli-mirage": "0.4.3",
"ember-cli-qunit": "^4.0.0",
"ember-cli-shims": "^1.1.0",
"ember-cli-sri": "^2.1.0",
"ember-cli-tutorial-style": "2.0.0",
"ember-cli-uglify": "^1.2.0",
"ember-data": "^2.13.0",
"ember-export-application-global": "^2.0.0",
"ember-load-initializers": "^1.0.0",
"ember-resolver": "^4.0.0",
"ember-source": "~2.13.0",
"ember-welcome-page": "^3.0.0",
"loader.js": "^4.2.3"
},
"engines": {
"node": ">= 4"
},
"private": true,
"dependencies": {
"npm-package-test": "git+https://github.com/nhoughto5/NPM_PackageTest.git"
}
}
When I run npm install in the ember project I can see that the folder structure from the library appears in the node_modules folder. To my limited experience, everything seems correct but for some reason I am still getting this undefined module error.
Is there a step I've missed or some crucial detail I'm missing?
Yes, there’s one step you are still missing. For Ember-CLI to understand that you want to include your npm package in your app’s vendor files, you’ll need to use app.import as outlined here: https://guides.emberjs.com/v3.0.0/addons-and-dependencies/managing-dependencies/
That approach with app.import has existed since Ember-CLI 2.15, but if you are on an older version you’ll need to upgrade first.
I´m setting up a shared module environment using node. Here is my directory structure:
project
|--common
| |--package.json
| |--graphql
| |----schema.js
|
|--server
|--package.json
|--server.js
Linking both projects:
$ cd project\common
$ npm link
Then:
$ cd ../server
$ npm link common
common Package.json file:
{
"name": "common",
"private": true,
"version": "3.0.0",
"description": "Common code for all projects",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "MIT"
}
server package.json file:
{
"name": "server",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "concurrently \"babel-node start-server\" \"babel-node start-client\"",
"server": "nodemon --exec \"babel-node start-server.js\"",
"client": "nodemon --exec \"babel-node start-client.js\"",
"lint": "eslint ."
},
"dependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.17.2",
"common": "file:../common",
"connect-mongo": "^2.0.0",
"crypto": "^1.0.1",
"express": "^4.15.3",
"express-graphql": "^0.6.12",
"graphql": "^0.13.1",
"graphql-relay": "^0.5.4",
"jwt-simple": "^0.5.1",
"mongoose": "^5.0.10",
"morgan": "^1.8.2",
"nodemailer": "^4.6.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"path": "^0.12.7",
"validator": "^9.1.1"
},
"devDependencies": {
"concurrently": "3.5.1",
"eslint": "^4.18.2",
"eslint-config-airbnb": "16.1.0",
"eslint-plugin-import": "2.9.0",
"eslint-plugin-jsx-a11y": "6.0.3",
"eslint-plugin-react": "7.7.0",
"fs-extra": "^5.0.0",
"node-fetch": "^2.1.1",
"nodemon": "^1.11.0"
},
"babel": {
"presets": [
"es2015",
"stage-0",
"es2017"
],
"plugins": [
"transform-runtime"
]
}
}
Server.js code:
import schema from "common/graphql/schema";
...
Running server application:
$ npm run server
import { GraphQLSchema } from 'graphql';
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at loader (D:\project\server\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\project\server\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:/9. DEV/WORKSPACE/amplifactory/server/routes.js:25:1)
at Module._compile (module.js:570:32)
at loader (D:\project\server\node_modules\babel-register\lib\node.js:144:5)
at Object.require.extensions.(anonymous function) [as .js] (D:\project\server\node_modules\babel-register\lib\node.js:154:7)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
[nodemon] app crashed - waiting for file changes before starting...
From what I´ve seen that is not transpiling code outside server directory, but without putting common ouside server directory I cannot build my shared code.
How to solve this babel issue and make it transpile correctly ?
You aren't going to like it, and maybe there is a better answer in spring 2018, but you may need to have a separate build step for your common code. I have a similar project where the package.json file for the common code looks something like this:
{
"name": "stripmall_gcloud_services",
"version": "1.0.0",
"description": "wraps up some commonly used google helpers",
"main": "./dist/index.js",
"scripts": {
"test": "standard --fix && mocha -r babel-register",
"build": "babel lib -d dist"
}...}
Notice the npm build step that transpiles the common code, and notice that the main key points to the index.js file in the transpiled directory. You just run npm run build whenever you update your common code and all your linking will work as you would expect.