I am aware that questions on this topic have already been answered, but since they are a little old and none of the solutions apply to my problem (which is bugging me for days), I decided to come forward to ask the community.
I am trying to build a web component for diagramming using Lit and JointJS.
Basically I created a new Node.js project using WebStorm and installed all the dependencies I think I need. My package.json looks like this:
{
"name": "project-name",
"version": "1.0.0",
"description": "",
"main": "project-name.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "web-dev-server --node-resolve --open --watch"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"#web/dev-server": "^0.1.34",
"#types/backbone": "~1.4.15",
"#types/jquery": "~3.5.13",
"#types/lodash": "~4.14.178"
},
"dependencies": {
"jointjs": "^3.6.1",
"lit": "^2.4.0",
"backbone": "~1.4.1",
"jquery": "~3.6.1",
"lodash": "~4.17.21"
}
}
To test if this setup is working I used the 'Hello World'-example from the lit.dev website:
index.html:
<!DOCTYPE html>
<head>
<script type="module" src="project-name.js"></script>
</head>
<body>
<simple-greeting name="World"></simple-greeting>
</body>
project-name.js:
import {html, css, LitElement} from 'lit';
import * as joint from 'jointjs'; // causes problems
export class SimpleGreeting extends LitElement {
static styles = css`p { color: blue }`;
static properties = {
name: {type: String},
};
constructor() {
super();
this.name = 'Somebody';
console.log(joint); // would like to test-print the object
}
render() {
return html`<p>Hello, ${this.name}!</p>`;
}
}
customElements.define('simple-greeting', SimpleGreeting);
When starting up the web-dev-server using npm start and looking at the console output of the site, I get the following error when importing jointJS the way I did:
Uncaught SyntaxError: The requested module './../../../jquery/dist/jquery.js' does not provide an export named 'default' (at util.mjs:2:8)
I already looked up this error but it seems odd to me that this occurs on installed libraries. I really don't want to touch the installed libraries code.
How do I get this JointJS import working properly with this setup?
I am not familiar with configuration for "#web/dev-server", but I tried your code using vite and everything is working fine.
I just installed vite via npm, and added "dev": "vite", to my package.json.
Just from looking at the documentation from "#web/dev-server" quickly, it seems like it tries to use es modules for everything, so maybe some extra configuration is needed via "#web/dev-server" or rollup to transform packages in node_modules.
Just as an aside, the JointJS dependencies will be installed via npm when you add JointJS, so you don't need to add lodash, etc separately.
Related
I am currently developing my own npm package and I created a separate project to download this package from npm for an independent test. The package is being developed in typescript and I have a main file with several additional module files. In my main file, I am importing all of the classes from the other modules, then exporting all of them under the main file. I don't know if this is good practice but when I run the main file on the test project, it says it can't find the module when the path it specifies exists in the working directory.
Code Snippets:
Main file:
import { EventBus } from "./modules/eventbus/eventbus";
import { EventHandler } from "./modules/eventbus/eventhandler";
import { EventType } from "./modules/eventbus/eventtype";
import { Event } from "./modules/eventbus/event";
import { SemVer } from "./modules/semver";
export { SemVer, Event, EventBus, EventHandler, EventType };
Error:
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/workspaces/epic-engine-testing/node_modules/epic-engine/lib/modules/eventbus/eventbus' imported from /workspaces/epic-engine-testing/node_modules/epic-engine/lib/index.js
Working directory:
Testing file:
import { EventBus, EventHandler, EventType, Event } from "epic-engine";
class SomeType extends EventType {
constructor() {
super();
}
}
const eventbus = new EventBus();
const handler = new EventHandler<SomeType>(eventbus, "type", () => {});
eventbus.createHandler(handler);
const event = new Event<SomeType>(eventbus, new SomeType(), "type");
package.json:
{
"devDependencies": {
"#tsconfig/esm": "^1.0.2",
"#types/jest": "^29.2.3",
"jest": "^29.3.1",
"ts-jest": "^29.0.3",
"tslint": "^6.1.3",
"typescript": "^4.9.3"
},
"name": "epic-engine",
"description": "Pure TS engine developed by EpicPuppy613",
"version": "0.1.0-dev.5",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"type": "module",
"scripts": {
"test": "jest --config jestconfig.json",
"build": "tsc",
"prepare": "npm run build",
"lint": "tslint -p tsconfig.json",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",
"version": "git add -A src",
"postversion": "git push && git push --tags"
},
"repository": {
"type": "git",
"url": "git+https://github.com/EpicPuppy613/epic-engine.git"
},
"author": "EpicPuppy613",
"license": "MIT",
"bugs": {
"url": "https://github.com/EpicPuppy613/epic-engine/issues"
},
"homepage": "https://github.com/EpicPuppy613/epic-engine#readme",
"files": [
"lib/**/*"
]
}
I tried a bunch of things including changing the references to use .js, using absolute paths instead, and changing some settings in tsconfig.json.
Why is Node.js not finding the submodules or would it be better to export the modules in a different way?
Save time with npm link command
I created a separate project to download this package from npm for an independent test.
First, you can use the handy npm link command to save yourself the trouble of uploading your package just so you can test. As per the docs the npm link command:
...is handy for installing your own stuff, so that you can work on it and test iteratively without having to continually rebuild.
Install your package as a dependency
With that out of the way, I think the hint is in the error message. Note it says:
Cannot find module /workspaces/epic-engine-testing/node_modules/...
Here it seems Node is looking for the file in the epic-engine-testing project, so you must have the package.json file for the test project reference your package (i.e. the one you want to test). So go into your epic-engine-testing project folder and at the terminal type npm install epic-engine#0.1.0-dev.5. That should install your package so it can be found. If that doesn't resolve it, you'll need to share the package.json file for the epic-engine-testing to help us see what's going on.
Using the export { ... } from '...' syntax
Your main file can use the re-exports synax and be simplified to this when exporting:
export { EventBus } from "./modules/eventbus/eventbus";
export { EventHandler } from "./modules/eventbus/eventhandler";
export { EventType } from "./modules/eventbus/eventtype";
export { Event } from "./modules/eventbus/event";
export { SemVer } from "./modules/semver";
// the line below is not necessary when using above syntax.
// export { SemVer, Event, EventBus, EventHandler, EventType };
After compiling the ts files into js files, wherever you have import syntax, Node.js looks for .js files to resolve them. So it needs to be explicitly given a module name with .js extension in import.
You may need to read this doc on how the import mechanism works in Node.js.
To fix this issue, you have multiple choices(since the target you've defined is ES6):
change moduleResolution to nodeNext and add .js extension whenever you would importing modules in typescript:
import { EventBus } from "./modules/eventbus/eventbus.js";
import { EventHandler } from "./modules/eventbus/eventhandler.js";
import { EventType } from "./modules/eventbus/eventtype.js";
...
You don't need to be worried about it, typescript is well smart. Based on this comment from one of typescript contributor:
.js file extensions are now allowed
Using rollup package. The rollup package won't manipulate your files. Instead, it bundles your output files.
I'm trying to call the example of three.js from fireship-io (Youtube channel) to make an animated background on my Python dash app based on the scrolling of the user. The Js script continuously calls a function that updates the rotation of objects and the position of a camera.
I installed node and I can run the js on its own but when I call it from my python scripts it throws an error at line 1. Uncaught SyntaxError: Cannot use import statement outside a module (at main.js?m=1658899110.9005847:1:1). It cannot import style.css, but the dash app can see the css because the font is the one defined in it.
How can I solve this error? Is there a way to specify "type":"module" in Dash when running a js?
Note: The js is called automatically by dash since it's in the assets directory. I added "type":"module" in package.json but it still gives me an error.
main.js:
import './style.css';
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
...
app.py:
app = Dash(__name__)
server = app.server
app.layout = html.Div(children=[
...
])
if __name__ == '__main__':
app.run_server(debug=False)
package.json:
{
"name": "three-demo",
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"serve": "vite preview"
},
"devDependencies": {
"vite": "^2.3.0"
},
"dependencies": {
"three": "^0.128.0"
}
}
Root folder:
app.py
assets/
- main.js
- package.json
- style.css
If this link: Adding Your Own CSS and JavaScript to Dash Apps
is anything to go by then your css would be included automatically by Dash if it's in the correct folder.
So you can probably skip the import in the main.js file
If you look at the link above again and scroll to 'Adding External CSS/JavaScript'.
You could try to skip the import in the main.js file and add this to your python file:
external_scripts = [
'https://threejs.org/build/three.js'
]
Something similar would probably have to be done for the 'OrbitControls'.
Maintainer of multiple npm packages here. Been using mocha with the require syntax and wanting to migrate to the import syntax.
The error I am getting is
Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js
Steps to Reproduce
With the following three files
src/index.js
export const sum = (a, b) => a + b;
test/index.spec.js
import { sum } from '../src/index';
const expect = require('chai').expect;
describe('Testing Index', () => {
it('Testing sum', () => {
expect(sum(7, 13)).to.equal(20);
});
});
package.json
{
"name": "mocha-debug",
"type": "module",
"main": "index.js",
"scripts": {
"test": "mocha \"./test/**/*.spec.js\""
},
"license": "ISC",
"devDependencies": {
"chai": "4.3.4",
"mocha": "9.1.4"
}
}
and using node v14.18.2, run yarn install and
yarn test
> `Cannot find module '<project>/src/index' imported from <project>/test/index.spec.js`
Notes
I've found a related issue that recommends using babel with --require #babel/register, but wasn't able to get over the error.
I've set up a test repo to make it easy to reproduce the issue
https://github.com/simlu/mocha-debug
Question
What am I doing wrong here? How do I get the tests to run successfully?
I solved it by just adding the file extension in my case I was importing my mongodb model so I imported with the file extension .ts
var Employee = require('../models/Employee.ts')
Which solved the issue
When I use "await" on top-level like this:
const LuckyDrawInstance=await new web3.eth.Contract(abi)
I got a warning on the terminal: "set experiments.topLevelAwait true". When I tried to add this to "tsconfig.json", it still does not work. it says "experiments" property does not exist.
I could wrap it inside an async function but I want to set it without a wrapped function.
It is nothing to do with the tsconfig.json. You have to set it inside next.config.js. New version of next.js uses webpack5 and webpack5 supports top level await.
module.exports = {
webpack: (config) => {
// this will override the experiments
config.experiments = { ...config.experiments, topLevelAwait: true };
// this will just update topLevelAwait property of config.experiments
// config.experiments.topLevelAwait = true
return config;
},
};
NOTE
You have to use it outside the functional component:
export default function Navbar() {
// this will throw error
// Syntax error: Unexpected reserved word 'await'.
const provider=await customFunction()
return (
<section>
</section>
);
}
Warning
Since it is experimental, it might be broken in some versions
The latest solution as of writing this post that worked for me is using Babel instead of SWC since Next.js does not allow custom SWC configuration, therefore, you cannot allow topLevelAwait through .swcrc file.
Add Babel plugin called #babel/plugin-syntax-top-level-await into your package.json.
eg.
{
"devDependencies": {
"#babel/plugin-syntax-top-level-await": "^7.14.5"
}
}
Create .babelrc file in the root directory of your project where package.json lives.
Inside .babelrc make sure to include next/babel preset and the topLevelAwait plugin.
eg.
{
"presets": ["next/babel"],
"plugins": [
"#babel/plugin-syntax-top-level-await"
]
}
This is the easiest solution until Next.js team allows us to include SWC configuration. Note that by doing this you will not have SWC performance benefit since it will be disabled in favor of Babel.
I have been struggling with this for 2-3 days. Here is a solution that works. Please follow the following steps.
1. Copy paste the following in your package.json
{
"name": "projectname",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"dev": "next dev"
},
"author": "",
"license": "ISC",
"dependencies": {
"#truffle/hdwallet-provider": "^2.0.1",
"fs-extra": "^10.0.0",
"ganache-cli": "^6.12.2",
"mocha": "^9.1.4",
"next": "^12.0.8",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"solc": "^0.8.9",
"web3": "^1.7.0",
"#babel/plugin-syntax-top-level-await": "^7.14.5"
},
"devDependencies": {
"#babel/plugin-syntax-top-level-await": "^7.14.5"
}
}
2. Delete your node_modules folder
3. Goto your project's root directory and reinstall all the packages using npm install command
4. Create a new file in your project's root directory and call it "next.config.js"
5. Copy paste following code in next.config.js file and save.
module.exports = {
// target: 'experimental-serverless-trace',
webpack: (config) => {
config.experiments = config.experiments || {};
config.experiments.topLevelAwait = true;
return config;
},
};
I'm getting error with every solution I can find, my scope is just declare a DataTables() object in my index.js.
I ended up the first time initialization with the basic setup in their getting-started page: https://webpack.js.org/guides/getting-started/
After this i run npm run build and it worked.
Done that, I followed the instruction on this git repo: https://gist.github.com/marcstober/c34bb4bdf7ef622cb24d6675723749bd#file-jquery-datatables-webpack
I don't understand what can I have wrong, I just followed as showed in the linked resources.
Just to completeness I paste my files.
webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
},
mode: 'development',
};
package.json
{
"name": "spotz-fe-dev",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^5.23.0",
"webpack-cli": "^4.5.0"
},
"dependencies": {
"datatables.net": "^1.10.23",
"datatables.net-dt": "^1.10.23",
"lodash": "^4.17.21"
}
}
src/index.js
import _ from 'lodash';
import $ from 'jquery';
import 'datatables.net';
import 'datatables.net-dt/css/jquery.datatables.css';
function component() {
const element = document.createElement('div');
// Lodash, now imported by this script
element.innerHTML = _.join(['Hello', 'webpack'], ' ');
$('table[data-table]').DataTable();
return element;
}
document.body.appendChild(component());
dist/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Getting Started</title>
</head>
<body>
<script src="main.js"></script>
</body>
</html>
As you can see is a very basic setup, my attempt is create an empty table with an npm run build.
Once is working I will begin the porting of my code, I already have a working project with simple jquery jquery-ui and datatables.
I thought to use this to simplify the developing process.
Regards.
Ok I solved, basically webpack do not recognize css code so I had to install css-loader, after this webpack had issues with the image file so I had to install style-color and file-loader. The last was with jQuery and I had to explicity install jQuery and finally npm update.
I followed then this links.
1) webpack https://webpack.js.org/guides/getting-started/
2) webpack https://www.taniarascia.com/how-to-use-webpack/
3) options https://webpack.js.org/loaders/css-loader/
4) options https://stackoverflow.com/questions/35568114/cannot-load-png-files-with-webpack-unexpected-character
5) options https://www.npmjs.com/package/file-loader
6) dt https://datatables.net/forums/discussion/32542/datatables-and-webpack
7) dt https://gist.github.com/marcstober/c34bb4bdf7ef622cb24d6675723749bd
Regards.