I am reading the book “introducing rescript” and having little bit difficulty in organizing my code.
currently my structure looks like
src
|-->Client.res
|-->Server.res
bsconfig.json
package.json
Here my package.json looks like
{
"name": "example1",
"version": "0.1.0",
"type":"module",
"scripts": {
"clean": "rescript clean -with-deps",
"build": "rescript build && webpack",
"watch": "rescript build -w",
"serve": "node src/Server.bs.js"
},
"keywords": [
"ReScript"
],
"author": "",
"license": "MIT",
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.7.3",
"html-webpack-plugin": "^5.5.0",
"rescript": "^10.0.1",
"style-loader": "^3.3.1",
"webpack": "^5.75.0",
"webpack-cli": "^5.0.1",
"webpack-html-plugin": "^0.1.1"
},
"dependencies": {
"@rescript/react": "^0.11.0",
"express": "^4.18.2",
"rescript-express": "^0.4.1",
"rescript-nodejs": "^14.3.1",
"rescript-webapi": "^0.7.0"
}
}
With this code when I do npm run serve
it runs my server successfully. But now when I do npm run build
it gives an error message
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js'
file extension and '/Users/foo/code/Rescript/example1/package.json'
contains "type": "module". To treat it as a CommonJS script, rename
it to use the '.cjs' file extension.
at file:///Users/foo/code/Rescript/example1/webpack.config.js:1:14
at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
Now if I take out the like "type":"module"
from the package.json. Now I can successfully do npm run build
but now the command npm run serve
it throws error
(node:99699) Warning: To load an ES module, set "type": "module" in
the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was
created)
/Users/foo/code/Rescript/example1/src/Server.bs.js:3
import * as Url from "url";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1195:20)
at Module._compile (node:internal/modules/cjs/loader:1239:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
at Module.load (node:internal/modules/cjs/loader:1133:32)
at Module._load (node:internal/modules/cjs/loader:972:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47
How can I structure my package.json file in a way that I can run both the server as well as build my client?